repos / pico

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

commit
ebcdf13
parent
4e0839a
author
Eric Bower
date
2024-11-28 00:17:15 +0000 UTC
fix(auth): analytics
3 files changed,  +38, -11
M auth/api.go
+18, -7
 1@@ -6,7 +6,6 @@ import (
 2 	"crypto/hmac"
 3 	"embed"
 4 	"encoding/json"
 5-	"errors"
 6 	"fmt"
 7 	"html/template"
 8 	"io"
 9@@ -354,7 +353,11 @@ func rssHandler(apiConfig *shared.ApiConfig) http.HandlerFunc {
10 		apiToken := r.PathValue("token")
11 		user, err := apiConfig.Dbpool.FindUserForToken(apiToken)
12 		if err != nil {
13-			apiConfig.Cfg.Logger.Error(err.Error())
14+			apiConfig.Cfg.Logger.Error(
15+				"could not find user for token",
16+				"err", err.Error(),
17+				"token", apiToken,
18+			)
19 			http.Error(w, "invalid token", http.StatusNotFound)
20 			return
21 		}
22@@ -629,6 +632,7 @@ func deserializeCaddyAccessLog(dbpool db.DB, access *CaddyAccessLog) (*db.Analyt
23 	if err != nil {
24 		return nil, err
25 	}
26+
27 	// get user ID
28 	user, err := dbpool.FindUserForName(props.Username)
29 	if err != nil {
30@@ -645,8 +649,10 @@ func deserializeCaddyAccessLog(dbpool db.DB, access *CaddyAccessLog) (*db.Analyt
31 		projectID = project.ID
32 	} else if space == "prose" { // figure out post ID
33 		if path == "" || path == "/" {
34+			// ignore
35 		} else {
36-			post, err := dbpool.FindPostWithSlug(path, user.ID, space)
37+			cleanPath := strings.TrimPrefix(path, "/")
38+			post, err := dbpool.FindPostWithSlug(cleanPath, user.ID, space)
39 			if err != nil {
40 				return nil, err
41 			}
42@@ -713,7 +719,11 @@ func containerDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger) {
43 					logger.Error("could not marshal json of a visit", "err", err)
44 					continue
45 				}
46-				_, _ = send.Write(jso)
47+				jso = append(jso, []byte("\n")...)
48+				_, err = send.Write(jso)
49+				if err != nil {
50+					logger.Error("could not write to metric-drain", "err", err)
51+				}
52 			}
53 		}
54 	}
55@@ -748,14 +758,15 @@ func metricDrainSub(ctx context.Context, dbpool db.DB, logger *slog.Logger, secr
56 				logger.Info("could not unmarshal json", "err", err, "line", line)
57 				continue
58 			}
59+			logger.Info("received visit", "visit", visit)
60 			err = shared.AnalyticsVisitFromVisit(&visit, dbpool, secret)
61 			if err != nil {
62-				if !errors.Is(err, shared.ErrAnalyticsDisabled) {
63-					logger.Info("could not record analytics visit", "reason", err)
64-				}
65+				logger.Info("could not record analytics visit", "err", err)
66+				continue
67 			}
68 
69 			if visit.ContentType != "" && !strings.HasPrefix(visit.ContentType, "text/html") {
70+				logger.Info("invalid content type", "contentType", visit.ContentType)
71 				continue
72 			}
73 
M caddy/Caddyfile
+10, -2
 1@@ -11,7 +11,11 @@
 2 
 3 *.{$APP_DOMAIN}, {$APP_DOMAIN} {
 4 	reverse_proxy web:3000
 5-	log
 6+	log {
 7+		format append {
 8+			server_id {$APP_DOMAIN}
 9+		}
10+	}
11 	tls {$APP_EMAIL} {
12 		dns cloudflare {$CF_API_TOKEN}
13 		resolvers 1.1.1.1
14@@ -61,7 +65,11 @@
15 
16 :443 {
17 	reverse_proxy web:3000
18-	log
19+	log {
20+		format append {
21+			server_id {$APP_DOMAIN}
22+		}
23+	}
24 	tls {$APP_EMAIL} {
25 		on_demand
26 	}
M caddy/Caddyfile.pgs
+10, -2
 1@@ -12,7 +12,11 @@
 2 
 3 *.{$APP_DOMAIN}, {$APP_DOMAIN} {
 4 	reverse_proxy web:3000
 5-	log
 6+	log {
 7+		format append {
 8+			server_id {$APP_DOMAIN}
 9+		}
10+	}
11 	tls {$APP_EMAIL} {
12 		dns cloudflare {$CF_API_TOKEN}
13 		resolvers 1.1.1.1
14@@ -95,7 +99,11 @@ monitoring.{$MONITORING_APP_DOMAIN}, prometheus.{$MONITORING_APP_DOMAIN}, grafan
15 
16 :443 {
17 	reverse_proxy web:3000
18-	log
19+	log {
20+		format append {
21+			server_id {$APP_DOMAIN}
22+		}
23+	}
24 	tls {$APP_EMAIL} {
25 		on_demand
26 	}