repos / pico

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

commit
01eb7a1
parent
6e39371
author
Eric Bower
date
2024-10-06 00:55:59 +0000 UTC
chore(pastes): better logs
1 files changed,  +20, -12
M pastes/api.go
+20, -12
  1@@ -79,15 +79,16 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
  2 	username := shared.GetUsernameFromRequest(r)
  3 	dbpool := shared.GetDB(r)
  4 	blogger := shared.GetLogger(r)
  5+	logger := blogger.With("user", username)
  6 	cfg := shared.GetCfg(r)
  7 
  8 	user, err := dbpool.FindUserForName(username)
  9 	if err != nil {
 10-		blogger.Info("user not found", "user", username)
 11+		logger.Info("user not found")
 12 		http.Error(w, "user not found", http.StatusNotFound)
 13 		return
 14 	}
 15-	logger := shared.LoggerWithUser(blogger, user)
 16+	logger = shared.LoggerWithUser(blogger, user)
 17 
 18 	pager, err := dbpool.FindPostsForUser(&db.Pager{Num: 1000, Page: 0}, user.ID, cfg.Space)
 19 	if err != nil {
 20@@ -171,23 +172,26 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 21 
 22 	dbpool := shared.GetDB(r)
 23 	blogger := shared.GetLogger(r)
 24+	logger := blogger.With("slug", slug, "user", username)
 25 
 26 	user, err := dbpool.FindUserForName(username)
 27 	if err != nil {
 28-		blogger.Info("paste not found", "user", username)
 29+		logger.Info("paste not found")
 30 		http.Error(w, "paste not found", http.StatusNotFound)
 31 		return
 32 	}
 33-	logger := shared.LoggerWithUser(blogger, user)
 34+	logger = shared.LoggerWithUser(logger, user)
 35 
 36 	blogName := GetBlogName(username)
 37 
 38 	var data PostPageData
 39 	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
 40 	if err == nil {
 41+		logger = logger.With("filename", post.Filename)
 42+		logger.Info("paste found")
 43 		parsedText, err := ParseText(post.Filename, post.Text)
 44 		if err != nil {
 45-			logger.Error("could not parse text", "err", err.Error())
 46+			logger.Error("could not parse text", "err", err)
 47 		}
 48 		expiresAt := "never"
 49 		if post.ExpiresAt != nil {
 50@@ -216,7 +220,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 51 			Unlisted:     unlisted,
 52 		}
 53 	} else {
 54-		logger.Info("post not found", "user", username, "slug", slug)
 55+		logger.Info("paste not found")
 56 		data = PostPageData{
 57 			Site:         *cfg.GetSiteData(),
 58 			PageTitle:    "Paste not found",
 59@@ -240,9 +244,10 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 60 		http.Error(w, err.Error(), http.StatusInternalServerError)
 61 	}
 62 
 63+	logger.Info("serving paste")
 64 	err = ts.Execute(w, data)
 65 	if err != nil {
 66-		logger.Error("could not execute template", "err", err.Error())
 67+		logger.Error("could not execute template", "err", err)
 68 		http.Error(w, err.Error(), http.StatusInternalServerError)
 69 	}
 70 }
 71@@ -261,21 +266,24 @@ func postHandlerRaw(w http.ResponseWriter, r *http.Request) {
 72 
 73 	dbpool := shared.GetDB(r)
 74 	blogger := shared.GetLogger(r)
 75+	logger := blogger.With("user", username, "slug", slug)
 76 
 77 	user, err := dbpool.FindUserForName(username)
 78 	if err != nil {
 79-		blogger.Info("paste not found", "user", username)
 80-		http.Error(w, "paste not found", http.StatusNotFound)
 81+		logger.Info("user not found")
 82+		http.Error(w, "user not found", http.StatusNotFound)
 83 		return
 84 	}
 85-	logger := shared.LoggerWithUser(blogger, user)
 86+	logger = shared.LoggerWithUser(blogger, user)
 87 
 88 	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
 89 	if err != nil {
 90-		logger.Info("post not found", "slug", slug)
 91-		http.Error(w, "post not found", http.StatusNotFound)
 92+		logger.Info("paste not found")
 93+		http.Error(w, "paste not found", http.StatusNotFound)
 94 		return
 95 	}
 96+	logger = logger.With("filename", post.Filename)
 97+	logger.Info("raw paste found")
 98 
 99 	w.Header().Set("Content-Type", "text/plain")
100 	_, err = w.Write([]byte(post.Text))