repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
52f603d
parent
975f3fb
author
Eric Bower
date
2023-09-05 16:07:20 +0000 UTC
fix(pgs): new `/check` handler for custom domains
1 files changed,  +44, -2
M pgs/api.go
+44, -2
 1@@ -33,6 +33,48 @@ type AssetHandler struct {
 2 	UserID     string
 3 }
 4 
 5+func checkHandler(w http.ResponseWriter, r *http.Request) {
 6+	dbpool := shared.GetDB(r)
 7+	cfg := shared.GetCfg(r)
 8+	logger := shared.GetLogger(r)
 9+
10+	if cfg.IsCustomdomains() {
11+		hostDomain := r.URL.Query().Get("domain")
12+		appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
13+
14+		if !strings.Contains(hostDomain, appDomain) {
15+			subdomain := shared.GetCustomDomain(logger, hostDomain, cfg.Space)
16+			props, err := getProjectFromSubdomain(subdomain)
17+			if err != nil {
18+				logger.Error(err)
19+				w.WriteHeader(http.StatusUnprocessableEntity)
20+				return
21+			}
22+
23+			u, err := dbpool.FindUserForName(props.Username)
24+			if err != nil {
25+				logger.Error(err)
26+				w.WriteHeader(http.StatusUnprocessableEntity)
27+				return
28+			}
29+
30+			p, err := dbpool.FindProjectByName(u.ID, props.ProjectName)
31+			if err != nil {
32+				logger.Error(err)
33+				w.WriteHeader(http.StatusUnprocessableEntity)
34+				return
35+			}
36+
37+			if u != nil && p != nil {
38+				w.WriteHeader(http.StatusOK)
39+				return
40+			}
41+		}
42+	}
43+
44+	w.WriteHeader(http.StatusNotFound)
45+}
46+
47 func calcPossibleRoutes(projectName, fp string) []string {
48 	fname := filepath.Base(fp)
49 	fdir := filepath.Dir(fp)
50@@ -107,7 +149,7 @@ func getProjectFromSubdomain(subdomain string) (*SubdomainProps, error) {
51 	props := &SubdomainProps{}
52 	strs := strings.SplitN(subdomain, "-", 2)
53 	if len(strs) < 2 {
54-		return nil, fmt.Errorf("subdomain incorrect format, must have period: %s", subdomain)
55+		return nil, fmt.Errorf("subdomain incorrect format, must have `-` separating username and project: %s", subdomain)
56 	}
57 	props.Username = strs[0]
58 	props.ProjectName = strs[1]
59@@ -191,7 +233,7 @@ func StartApiServer() {
60 
61 	mainRoutes := []shared.Route{
62 		shared.NewRoute("GET", "/", marketingRequest),
63-		shared.NewRoute("GET", "/check", shared.CheckHandler),
64+		shared.NewRoute("GET", "/check", checkHandler),
65 		shared.NewRoute("GET", "/(.+)", marketingRequest),
66 	}
67 	subdomainRoutes := []shared.Route{