repos / pico

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

commit
2dc8060
parent
dd19f3e
author
Antonio Mika
date
2022-08-04 05:22:33 +0000 UTC
Added raw handler to lists
1 files changed,  +40, -0
M lists/api.go
+40, -0
 1@@ -223,6 +223,44 @@ func GetBlogName(username string) string {
 2 	return fmt.Sprintf("%s's lists", username)
 3 }
 4 
 5+func postRawHandler(w http.ResponseWriter, r *http.Request) {
 6+	username := shared.GetUsernameFromRequest(r)
 7+	subdomain := shared.GetSubdomain(r)
 8+	cfg := shared.GetCfg(r)
 9+
10+	var slug string
11+	if !cfg.IsSubdomains() || subdomain == "" {
12+		slug, _ = url.PathUnescape(shared.GetField(r, 1))
13+	} else {
14+		slug, _ = url.PathUnescape(shared.GetField(r, 0))
15+	}
16+
17+	dbpool := shared.GetDB(r)
18+	logger := shared.GetLogger(r)
19+
20+	user, err := dbpool.FindUserForName(username)
21+	if err != nil {
22+		logger.Infof("blog not found: %s", username)
23+		http.Error(w, "blog not found", http.StatusNotFound)
24+		return
25+	}
26+
27+	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
28+	if err != nil {
29+		logger.Infof("post not found")
30+		http.Error(w, "post not found", http.StatusNotFound)
31+		return
32+	}
33+
34+	w.Header().Add("Content-Type", "text/plain")
35+
36+	_, err = w.Write([]byte(post.Text))
37+	if err != nil {
38+		logger.Error(err)
39+		http.Error(w, "server error", 500)
40+	}
41+}
42+
43 func postHandler(w http.ResponseWriter, r *http.Request) {
44 	username := shared.GetUsernameFromRequest(r)
45 	subdomain := shared.GetSubdomain(r)
46@@ -634,6 +672,7 @@ func createMainRoutes(staticRoutes []shared.Route) []shared.Route {
47 		shared.NewRoute("GET", "/([^/]+)", blogHandler),
48 		shared.NewRoute("GET", "/([^/]+)/rss", rssBlogHandler),
49 		shared.NewRoute("GET", "/([^/]+)/([^/]+)", postHandler),
50+		shared.NewRoute("GET", "/raw/([^/]+)/([^/]+)", postRawHandler),
51 	)
52 
53 	return routes
54@@ -653,6 +692,7 @@ func createSubdomainRoutes(staticRoutes []shared.Route) []shared.Route {
55 	routes = append(
56 		routes,
57 		shared.NewRoute("GET", "/([^/]+)", postHandler),
58+		shared.NewRoute("GET", "/raw/([^/]+)", postRawHandler),
59 	)
60 
61 	return routes