repos / pico

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

commit
23ece74
parent
dea5358
author
Eric Bower
date
2024-10-28 23:06:23 +0000 UTC
chore(pgs): move fns around
1 files changed,  +67, -67
M pgs/web.go
+67, -67
  1@@ -20,21 +20,42 @@ import (
  2 	sst "github.com/picosh/pobj/storage"
  3 )
  4 
  5-type SubdomainProps struct {
  6-	ProjectName string
  7-	Username    string
  8-}
  9+func StartApiServer() {
 10+	cfg := NewConfigSite()
 11+	logger := cfg.Logger
 12 
 13-func getProjectFromSubdomain(subdomain string) (*SubdomainProps, error) {
 14-	props := &SubdomainProps{}
 15-	strs := strings.SplitN(subdomain, "-", 2)
 16-	props.Username = strs[0]
 17-	if len(strs) == 2 {
 18-		props.ProjectName = strs[1]
 19+	dbpool := postgres.NewDB(cfg.DbURL, cfg.Logger)
 20+	defer dbpool.Close()
 21+
 22+	var st storage.StorageServe
 23+	var err error
 24+	if cfg.MinioURL == "" {
 25+		st, err = storage.NewStorageFS(cfg.StorageDir)
 26 	} else {
 27-		props.ProjectName = props.Username
 28+		st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
 29 	}
 30-	return props, nil
 31+
 32+	if err != nil {
 33+		logger.Error("could not connect to object storage", "err", err.Error())
 34+		return
 35+	}
 36+
 37+	ch := make(chan *db.AnalyticsVisits)
 38+	go shared.AnalyticsCollect(ch, dbpool, logger)
 39+
 40+	routes := NewWebRouter(cfg, logger, dbpool, st, ch)
 41+
 42+	portStr := fmt.Sprintf(":%s", cfg.Port)
 43+	logger.Info(
 44+		"starting server on port",
 45+		"port", cfg.Port,
 46+		"domain", cfg.Domain,
 47+	)
 48+	err = http.ListenAndServe(portStr, routes)
 49+	logger.Error(
 50+		"listen and serve",
 51+		"err", err.Error(),
 52+	)
 53 }
 54 
 55 type HasPerm = func(proj *db.Project) bool
 56@@ -61,6 +82,28 @@ func NewWebRouter(cfg *shared.ConfigSite, logger *slog.Logger, dbpool db.DB, st
 57 	return router
 58 }
 59 
 60+func (web *WebRouter) initRouters() {
 61+	// root domain
 62+	rootRouter := http.NewServeMux()
 63+	rootRouter.HandleFunc("GET /check", web.checkHandler)
 64+	rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css"))
 65+	rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png"))
 66+	rootRouter.Handle("GET /apple-touch-icon.png", web.serveFile("apple-touch-icon.png", "image/png"))
 67+	rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon"))
 68+	rootRouter.Handle("GET /robots.txt", web.serveFile("robots.txt", "text/plain"))
 69+
 70+	rootRouter.Handle("GET /rss/updated", web.createRssHandler("updated_at"))
 71+	rootRouter.Handle("GET /rss", web.createRssHandler("created_at"))
 72+	rootRouter.Handle("GET /{$}", web.createPageHandler("html/marketing.page.tmpl"))
 73+	web.RootRouter = rootRouter
 74+
 75+	// subdomain or custom domains
 76+	userRouter := http.NewServeMux()
 77+	userRouter.HandleFunc("GET /{fname...}", web.AssetRequest)
 78+	userRouter.HandleFunc("GET /{$}", web.AssetRequest)
 79+	web.UserRouter = userRouter
 80+}
 81+
 82 func (web *WebRouter) serveFile(file string, contentType string) http.HandlerFunc {
 83 	return func(w http.ResponseWriter, r *http.Request) {
 84 		logger := web.Logger
 85@@ -120,28 +163,6 @@ func (web *WebRouter) createPageHandler(fname string) http.HandlerFunc {
 86 	}
 87 }
 88 
 89-func (web *WebRouter) initRouters() {
 90-	// root domain
 91-	rootRouter := http.NewServeMux()
 92-	rootRouter.HandleFunc("GET /check", web.checkHandler)
 93-	rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css"))
 94-	rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png"))
 95-	rootRouter.Handle("GET /apple-touch-icon.png", web.serveFile("apple-touch-icon.png", "image/png"))
 96-	rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon"))
 97-	rootRouter.Handle("GET /robots.txt", web.serveFile("robots.txt", "text/plain"))
 98-
 99-	rootRouter.Handle("GET /rss/updated", web.createRssHandler("updated_at"))
100-	rootRouter.Handle("GET /rss", web.createRssHandler("created_at"))
101-	rootRouter.Handle("GET /{$}", web.createPageHandler("html/marketing.page.tmpl"))
102-	web.RootRouter = rootRouter
103-
104-	// subdomain or custom domains
105-	userRouter := http.NewServeMux()
106-	userRouter.HandleFunc("GET /{fname...}", web.AssetRequest)
107-	userRouter.HandleFunc("GET /{$}", web.AssetRequest)
108-	web.UserRouter = userRouter
109-}
110-
111 func (web *WebRouter) checkHandler(w http.ResponseWriter, r *http.Request) {
112 	dbpool := web.Dbpool
113 	cfg := web.Cfg
114@@ -427,40 +448,19 @@ func (web *WebRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
115 	router.ServeHTTP(w, r.WithContext(ctx))
116 }
117 
118-func StartApiServer() {
119-	cfg := NewConfigSite()
120-	logger := cfg.Logger
121-
122-	dbpool := postgres.NewDB(cfg.DbURL, cfg.Logger)
123-	defer dbpool.Close()
124+type SubdomainProps struct {
125+	ProjectName string
126+	Username    string
127+}
128 
129-	var st storage.StorageServe
130-	var err error
131-	if cfg.MinioURL == "" {
132-		st, err = storage.NewStorageFS(cfg.StorageDir)
133+func getProjectFromSubdomain(subdomain string) (*SubdomainProps, error) {
134+	props := &SubdomainProps{}
135+	strs := strings.SplitN(subdomain, "-", 2)
136+	props.Username = strs[0]
137+	if len(strs) == 2 {
138+		props.ProjectName = strs[1]
139 	} else {
140-		st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
141-	}
142-
143-	if err != nil {
144-		logger.Error("could not connect to object storage", "err", err.Error())
145-		return
146+		props.ProjectName = props.Username
147 	}
148-
149-	ch := make(chan *db.AnalyticsVisits)
150-	go shared.AnalyticsCollect(ch, dbpool, logger)
151-
152-	routes := NewWebRouter(cfg, logger, dbpool, st, ch)
153-
154-	portStr := fmt.Sprintf(":%s", cfg.Port)
155-	logger.Info(
156-		"starting server on port",
157-		"port", cfg.Port,
158-		"domain", cfg.Domain,
159-	)
160-	err = http.ListenAndServe(portStr, routes)
161-	logger.Error(
162-		"listen and serve",
163-		"err", err.Error(),
164-	)
165+	return props, nil
166 }