-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
312 lines (260 loc) · 9.36 KB
/
main.go
File metadata and controls
312 lines (260 loc) · 9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"database/sql"
"encoding/json"
"log/slog"
"net/http"
"os"
"strings"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/networkteam/go-sqllogger"
slogmulti "github.com/samber/slog-multi"
"github.com/networkteam/devlog"
"github.com/networkteam/devlog/collector"
sqlloggeradapter "github.com/networkteam/devlog/dbadapter/sqllogger"
)
type Todo struct {
ID int64 `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
CreatedAt time.Time `json:"created_at"`
}
func main() {
// 1. Set up slog with devlog middleware
httpServerOptions := collector.DefaultHTTPServerOptions()
httpServerOptions.Transformers = []collector.HTTPServerRequestTransformer{
func(request collector.HTTPServerRequest) collector.HTTPServerRequest {
if strings.HasPrefix(request.Path, "/todo") {
// Add a custom tag for all requests to /todo
request.Tags["api"] = "todos"
}
return request
},
}
dlog := devlog.NewWithOptions(devlog.Options{
HTTPServerOptions: &httpServerOptions,
})
defer dlog.Close()
logger := slog.New(
slogmulti.Fanout(
// Collect debug logs with devlog
dlog.CollectSlogLogs(collector.CollectSlogLogsOptions{
Level: slog.LevelDebug,
}),
// Log info to stderr
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}),
),
)
slog.SetDefault(logger)
// Initialize SQLite database
connector := newSQLiteConnector(":memory:")
loggingConnector := sqllogger.LoggingConnector(sqlloggeradapter.New(dlog.CollectDBQuery(), sqlloggeradapter.Options{
Language: "sqlite",
}), connector)
db := sql.OpenDB(loggingConnector)
defer db.Close()
var err error
// Create todos table
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
logger.Error("Failed to create todos table", slog.Any("err", err))
os.Exit(1)
}
mux := http.NewServeMux()
// 2. Create an HTTP client with devlog middleware (RoundTripper)
httpClient := &http.Client{
Transport: dlog.CollectHTTPClient(http.DefaultTransport),
Timeout: time.Second * 5,
}
type uselessfactResponse struct {
ID string `json:"id"`
Text string `json:"text"`
Source string `json:"source"`
SourceURL string `json:"source_url"`
Language string `json:"language"`
Permalink string `json:"permalink"`
}
mux.HandleFunc("/http-client", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/http-client")
logger.DebugContext(r.Context(), "Requesting uselessfacts API")
req, _ := http.NewRequestWithContext(r.Context(), http.MethodGet, "https://uselessfacts.jsph.pl/api/v2/facts/random", nil)
resp, err := httpClient.Do(req)
if err != nil {
logger.ErrorContext(r.Context(), "Failed to get uselessfacts API", slog.Any("err", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Failed to get uselessfacts API"))
return
}
defer resp.Body.Close()
var fact uselessfactResponse
if err := json.NewDecoder(resp.Body).Decode(&fact); err != nil {
logger.ErrorContext(r.Context(), "Failed to decode uselessfacts API response", slog.Any("err", err.Error()))
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("Failed to decode uselessfacts API response"))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(fact.Text))
})
// 3. Create a new HTTP server with a simple handler
mux.HandleFunc("/log", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/log")
logger.DebugContext(r.Context(), "Debug log from /log HTTP handler", slog.Group("request", slog.String("method", r.Method)))
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Log a thing"))
})
// Add todo handlers
mux.HandleFunc("GET /todos", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/todos")
ctx := r.Context()
// List todos
rows, err := db.QueryContext(ctx, "SELECT id, title, completed, created_at FROM todos ORDER BY created_at DESC")
if err != nil {
logger.ErrorContext(ctx, "Failed to query todos", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
defer rows.Close()
todos := []Todo{}
for rows.Next() {
var todo Todo
if err := rows.Scan(&todo.ID, &todo.Title, &todo.Completed, &todo.CreatedAt); err != nil {
logger.ErrorContext(ctx, "Failed to scan todo", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
todos = append(todos, todo)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(todos)
})
mux.HandleFunc("POST /todos", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/todos")
ctx := r.Context()
// Create todo
var todo Todo
if err := json.NewDecoder(r.Body).Decode(&todo); err != nil {
logger.ErrorContext(ctx, "Failed to decode todo", slog.Any("err", err))
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := db.ExecContext(ctx, "INSERT INTO todos (title, completed) VALUES (?, ?)", todo.Title, todo.Completed)
if err != nil {
logger.ErrorContext(ctx, "Failed to insert todo", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
id, err := result.LastInsertId()
if err != nil {
logger.ErrorContext(ctx, "Failed to get last insert ID", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
todo.ID = id
todo.CreatedAt = time.Now()
logger.InfoContext(ctx, "Created todo", slog.Group("todo", "id", todo.ID, "title", todo.Title))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(todo)
})
mux.HandleFunc("GET /todos/{id}", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/todos/{id}")
id := r.PathValue("id")
ctx := r.Context()
var todo Todo
err := db.QueryRowContext(ctx, "SELECT id, title, completed, created_at FROM todos WHERE id = ?", id).
Scan(&todo.ID, &todo.Title, &todo.Completed, &todo.CreatedAt)
if err == sql.ErrNoRows {
logger.WarnContext(ctx, "Todo not found", "id", id)
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
logger.ErrorContext(ctx, "Failed to query todo", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(todo)
})
mux.HandleFunc("PUT /todos/{id}", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/todos/{id}")
id := r.PathValue("id")
ctx := r.Context()
var todo Todo
if err := json.NewDecoder(r.Body).Decode(&todo); err != nil {
logger.ErrorContext(ctx, "Failed to decode todo", slog.Any("err", err))
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := db.ExecContext(ctx, "UPDATE todos SET title = ?, completed = ? WHERE id = ?", todo.Title, todo.Completed, id)
if err != nil {
logger.ErrorContext(ctx, "Failed to update todo", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
rows, err := result.RowsAffected()
if err != nil {
logger.ErrorContext(ctx, "Failed to get rows affected", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
if rows == 0 {
logger.WarnContext(ctx, "Todo not found for update", "id", id)
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("DELETE /todos/{id}", func(w http.ResponseWriter, r *http.Request) {
logger := slog.With("component", "http", "handler", "/todos/{id}")
id := r.PathValue("id")
ctx := r.Context()
result, err := db.ExecContext(ctx, "DELETE FROM todos WHERE id = ?", id)
if err != nil {
logger.ErrorContext(ctx, "Failed to delete todo", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
rows, err := result.RowsAffected()
if err != nil {
logger.ErrorContext(ctx, "Failed to get rows affected", slog.Any("err", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
if rows == 0 {
logger.WarnContext(ctx, "Todo not found for deletion", "id", id)
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
})
// 4. Wrap with devlog middleware to inspect requests and responses to the server
outerMux := http.NewServeMux()
outerMux.Handle("/", dlog.CollectHTTPServer(
mux,
))
// 5. Mount devlog dashboard
// Mount under path prefix /_devlog, so we handle the dashboard handler under this path, strip the prefix, so dashboard routes match and inform it about the path prefix to render correct URLs
outerMux.Handle("/_devlog/", http.StripPrefix("/_devlog", dlog.DashboardHandler("/_devlog")))
// Add a health check endpoint for refresh
outerMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Run the server
logger.Info("Starting server on :1095")
if err := http.ListenAndServe(":1095", outerMux); err != nil {
logger.Error("Failed to start server", slog.Group("error", slog.String("message", err.Error())))
os.Exit(1)
}
}