- commit
- 6e39371
- parent
- 69a6aef
- author
- Eric Bower
- date
- 2024-10-06 00:47:02 +0000 UTC
chore(pastes): better api logs
1 files changed,
+25,
-21
+25,
-21
1@@ -78,30 +78,32 @@ type HeaderTxt struct {
2 func blogHandler(w http.ResponseWriter, r *http.Request) {
3 username := shared.GetUsernameFromRequest(r)
4 dbpool := shared.GetDB(r)
5- logger := shared.GetLogger(r)
6+ blogger := shared.GetLogger(r)
7 cfg := shared.GetCfg(r)
8
9 user, err := dbpool.FindUserForName(username)
10 if err != nil {
11- logger.Info("blog not found", "user", username)
12- http.Error(w, "blog not found", http.StatusNotFound)
13+ blogger.Info("user not found", "user", username)
14+ http.Error(w, "user not found", http.StatusNotFound)
15 return
16 }
17- pager, err := dbpool.FindPostsForUser(&db.Pager{Num: 1000, Page: 0}, user.ID, cfg.Space)
18- posts := pager.Data
19+ logger := shared.LoggerWithUser(blogger, user)
20
21+ pager, err := dbpool.FindPostsForUser(&db.Pager{Num: 1000, Page: 0}, user.ID, cfg.Space)
22 if err != nil {
23- logger.Error(err.Error())
24+ logger.Error("could not find posts for user", "err", err.Error())
25 http.Error(w, "could not fetch posts for blog", http.StatusInternalServerError)
26 return
27 }
28
29+ posts := pager.Data
30+
31 ts, err := shared.RenderTemplate(cfg, []string{
32 cfg.StaticPath("html/blog.page.tmpl"),
33 })
34
35 if err != nil {
36- logger.Error(err.Error())
37+ logger.Error("could not render template", "err", err)
38 http.Error(w, err.Error(), http.StatusInternalServerError)
39 return
40 }
41@@ -138,7 +140,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
42
43 err = ts.Execute(w, data)
44 if err != nil {
45- logger.Error(err.Error())
46+ logger.Error("could not execute tempalte", "err", err)
47 http.Error(w, err.Error(), http.StatusInternalServerError)
48 }
49 }
50@@ -168,14 +170,15 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
51 }
52
53 dbpool := shared.GetDB(r)
54- logger := shared.GetLogger(r)
55+ blogger := shared.GetLogger(r)
56
57 user, err := dbpool.FindUserForName(username)
58 if err != nil {
59- logger.Info("blog not found", "user", username)
60- http.Error(w, "blog not found", http.StatusNotFound)
61+ blogger.Info("paste not found", "user", username)
62+ http.Error(w, "paste not found", http.StatusNotFound)
63 return
64 }
65+ logger := shared.LoggerWithUser(blogger, user)
66
67 blogName := GetBlogName(username)
68
69@@ -184,7 +187,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
70 if err == nil {
71 parsedText, err := ParseText(post.Filename, post.Text)
72 if err != nil {
73- logger.Error(err.Error())
74+ logger.Error("could not parse text", "err", err.Error())
75 }
76 expiresAt := "never"
77 if post.ExpiresAt != nil {
78@@ -239,7 +242,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
79
80 err = ts.Execute(w, data)
81 if err != nil {
82- logger.Error(err.Error())
83+ logger.Error("could not execute template", "err", err.Error())
84 http.Error(w, err.Error(), http.StatusInternalServerError)
85 }
86 }
87@@ -257,18 +260,19 @@ func postHandlerRaw(w http.ResponseWriter, r *http.Request) {
88 }
89
90 dbpool := shared.GetDB(r)
91- logger := shared.GetLogger(r)
92+ blogger := shared.GetLogger(r)
93
94 user, err := dbpool.FindUserForName(username)
95 if err != nil {
96- logger.Info("blog not found", "user", username)
97- http.Error(w, "blog not found", http.StatusNotFound)
98+ blogger.Info("paste not found", "user", username)
99+ http.Error(w, "paste not found", http.StatusNotFound)
100 return
101 }
102+ logger := shared.LoggerWithUser(blogger, user)
103
104 post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
105 if err != nil {
106- logger.Info("post not found", "user", username, "slug", slug)
107+ logger.Info("post not found", "slug", slug)
108 http.Error(w, "post not found", http.StatusNotFound)
109 return
110 }
111@@ -276,7 +280,7 @@ func postHandlerRaw(w http.ResponseWriter, r *http.Request) {
112 w.Header().Set("Content-Type", "text/plain")
113 _, err = w.Write([]byte(post.Text))
114 if err != nil {
115- logger.Error(err.Error())
116+ logger.Error("write error", "err", err)
117 }
118 }
119
120@@ -287,14 +291,14 @@ func serveFile(file string, contentType string) http.HandlerFunc {
121
122 contents, err := os.ReadFile(cfg.StaticPath(fmt.Sprintf("public/%s", file)))
123 if err != nil {
124- logger.Error(err.Error())
125+ logger.Error("could not read file", "err", err)
126 http.Error(w, "file not found", 404)
127 }
128 w.Header().Add("Content-Type", contentType)
129
130 _, err = w.Write(contents)
131 if err != nil {
132- logger.Error(err.Error())
133+ logger.Error("could not write contents", "err", err)
134 http.Error(w, "server error", 500)
135 }
136 }
137@@ -371,7 +375,7 @@ func StartApiServer() {
138 }
139
140 if err != nil {
141- logger.Error(err.Error())
142+ logger.Error("could not create storage adapter", "err", err.Error())
143 return
144 }
145