repos / pico

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

commit
dae626f
parent
413f3a2
author
Eric Bower
date
2024-10-28 22:49:02 +0000 UTC
fix(pgs): root domain routes were broken
1 files changed,  +67, -7
M pgs/api.go
+67, -7
 1@@ -5,6 +5,7 @@ import (
 2 	"fmt"
 3 	"log/slog"
 4 	"net/http"
 5+	"os"
 6 	"regexp"
 7 	"strings"
 8 	"time"
 9@@ -60,19 +61,78 @@ func NewWebRouter(cfg *shared.ConfigSite, logger *slog.Logger, dbpool db.DB, st
10 	return router
11 }
12 
13+func (web *WebRouter) serveFile(file string, contentType string) http.HandlerFunc {
14+	return func(w http.ResponseWriter, r *http.Request) {
15+		logger := web.Logger
16+		cfg := web.Cfg
17+
18+		contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file)))
19+		if err != nil {
20+			logger.Error(
21+				"could not read file",
22+				"fname", file,
23+				"err", err.Error(),
24+			)
25+			http.Error(w, "file not found", 404)
26+		}
27+
28+		w.Header().Add("Content-Type", contentType)
29+
30+		_, err = w.Write(contents)
31+		if err != nil {
32+			logger.Error(
33+				"could not write http response",
34+				"file", file,
35+				"err", err.Error(),
36+			)
37+		}
38+	}
39+}
40+
41+func (web *WebRouter) createPageHandler(fname string) http.HandlerFunc {
42+	return func(w http.ResponseWriter, r *http.Request) {
43+		logger := web.Logger
44+		cfg := web.Cfg
45+		ts, err := shared.RenderTemplate(cfg, []string{cfg.StaticPath(fname)})
46+
47+		if err != nil {
48+			logger.Error(
49+				"could not render template",
50+				"fname", fname,
51+				"err", err.Error(),
52+			)
53+			http.Error(w, err.Error(), http.StatusInternalServerError)
54+			return
55+		}
56+
57+		data := shared.PageData{
58+			Site: *cfg.GetSiteData(),
59+		}
60+		err = ts.Execute(w, data)
61+		if err != nil {
62+			logger.Error(
63+				"could not execute template",
64+				"fname", fname,
65+				"err", err.Error(),
66+			)
67+			http.Error(w, err.Error(), http.StatusInternalServerError)
68+		}
69+	}
70+}
71+
72 func (web *WebRouter) initRouters() {
73 	// root domain
74 	rootRouter := http.NewServeMux()
75 	rootRouter.HandleFunc("GET /check", web.checkHandler)
76-	rootRouter.Handle("GET /main.css", shared.ServeFile("main.css", "text/css"))
77-	rootRouter.Handle("GET /card.png", shared.ServeFile("card.png", "image/png"))
78-	rootRouter.Handle("GET /favicon-16x16.png", shared.ServeFile("favicon-16x16.png", "image/png"))
79-	rootRouter.Handle("GET /apple-touch-icon.png", shared.ServeFile("apple-touch-icon.png", "image/png"))
80-	rootRouter.Handle("GET /favicon.ico", shared.ServeFile("favicon.ico", "image/x-icon"))
81-	rootRouter.Handle("GET /robots.txt", shared.ServeFile("robots.txt", "text/plain"))
82+	rootRouter.Handle("GET /main.css", web.serveFile("main.css", "text/css"))
83+	rootRouter.Handle("GET /card.png", web.serveFile("card.png", "image/png"))
84+	rootRouter.Handle("GET /favicon-16x16.png", web.serveFile("favicon-16x16.png", "image/png"))
85+	rootRouter.Handle("GET /apple-touch-icon.png", web.serveFile("apple-touch-icon.png", "image/png"))
86+	rootRouter.Handle("GET /favicon.ico", web.serveFile("favicon.ico", "image/x-icon"))
87+	rootRouter.Handle("GET /robots.txt", web.serveFile("robots.txt", "text/plain"))
88 	rootRouter.Handle("GET /rss/updated", web.createRssHandler("updated_at"))
89 	rootRouter.Handle("GET /rss", web.createRssHandler("created_at"))
90-	rootRouter.Handle("GET /{$}", shared.CreatePageHandler("html/marketing.page.tmpl"))
91+	rootRouter.Handle("GET /{$}", web.createPageHandler("html/marketing.page.tmpl"))
92 	web.RootRouter = rootRouter
93 
94 	// subdomain or custom domains