@@ -21,6 +21,7 @@ import (
21
21
22
22
pgxtrace "github.com/DataDog/dd-trace-go/contrib/jackc/pgx.v5/v2"
23
23
"github.com/jackc/pgx/v5"
24
+ "github.com/jackc/pgx/v5/pgxpool"
24
25
25
26
"github.com/google/uuid"
26
27
@@ -31,43 +32,24 @@ const (
31
32
connStr = "postgres://postgres:test@localhost:5432/postgres?sslmode=disable"
32
33
)
33
34
34
- func main () {
35
- ctx := context .Background ()
36
-
37
- ctx = config .ContextWithConfig (ctx , config .GetConfig (ctx ))
38
- ctx = log .ContextWithLogger (ctx , log .GetLogger (ctx ))
39
-
40
- // Tag all logs with the version string if we have one
41
- if config .FromContext (ctx ).GetString ("version" ) != "" {
42
- ctx = log .ContextWithLogger (ctx , log .GetLogger (ctx ).With (
43
- zap .String ("version" , config .FromContext (ctx ).GetString ("version" )),
44
- ))
45
- }
46
-
47
- if config .FromContext (ctx ).GetBool ("tracing.enabled" ) {
48
- tracer .Start (
49
- tracer .WithEnv (config .FromContext (ctx ).GetString ("env" )),
50
- tracer .WithService ("cluesheet" ),
51
- tracer .WithServiceVersion (config .FromContext (ctx ).GetString ("version" )),
52
- )
53
- defer tracer .Stop ()
54
- log .FromContext (ctx ).Debug ("started tracing" )
55
- }
56
-
57
- conn , err := pgxtrace .NewPool (ctx , connStr )
58
- if err != nil {
59
- log .FromContext (ctx ).Fatal ("failed setting up db pool" , zap .Error (err ))
60
- }
61
- defer conn .Close ()
62
-
63
- router := muxtrace .NewRouter ()
64
-
35
+ /**
36
+ * registerRoutes applies all the routes to the given mux router
37
+ * It's intended to break things out of main for testing
38
+ *
39
+ * rootContext is the context that the router's logger and config will be read
40
+ * from, it is expected to be descended from the context in main
41
+ *
42
+ * router is the mux router to register routes on
43
+ *
44
+ * conn is the database connection
45
+ */
46
+ func registerRoutes (rootContext context.Context , router * muxtrace.Router , conn * pgxpool.Pool ) {
65
47
// Pass the logger and config in context to all requests
66
48
router .Use (func (h http.Handler ) http.Handler {
67
49
return http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
68
50
ctx := r .Context ()
69
- ctx = config .ContextWithConfig (ctx , config .GetConfig ( ctx ))
70
- ctx = log .ContextWithLogger (ctx , log .GetLogger ( ctx ))
51
+ ctx = config .ContextWithConfig (ctx , config .FromContext ( rootContext ))
52
+ ctx = log .ContextWithLogger (ctx , log .FromContext ( rootContext ))
71
53
r = r .WithContext (ctx )
72
54
h .ServeHTTP (rw , r )
73
55
})
@@ -92,7 +74,7 @@ func main() {
92
74
}
93
75
94
76
for _ , cluesheet := range cluesheets {
95
- clues , err := GetClues (ctx , conn , cluesheet .Id )
77
+ clues , err := GetClues (r . Context () , conn , cluesheet .Id )
96
78
if err != nil {
97
79
http .Error (rw , fmt .Sprintf ("failed resolving clues: '%s'" , err ), 500 )
98
80
return
@@ -129,7 +111,7 @@ func main() {
129
111
return
130
112
}
131
113
132
- clues , err := GetClues (ctx , conn , cluesheet .Id )
114
+ clues , err := GetClues (r . Context () , conn , cluesheet .Id )
133
115
if err != nil {
134
116
http .Error (rw , fmt .Sprintf ("failed resolving clues '%s': '%s'" , vars ["id" ], err ), 500 )
135
117
return
@@ -166,14 +148,7 @@ func main() {
166
148
return
167
149
}
168
150
169
- var params struct {
170
- Name string
171
- Origin * uuid.UUID
172
- Creator string
173
- Visibility * string
174
- Owners []string
175
- Groups []string
176
- }
151
+ var params PostCluesheetParams
177
152
178
153
err = json .Unmarshal (body , & params )
179
154
if err != nil {
@@ -214,7 +189,7 @@ func main() {
214
189
Owners : params .Owners ,
215
190
Groups : params .Groups ,
216
191
}
217
- _ , err = conn .Exec (ctx , `insert into cluesheet (id, name, origin_id, created_by, created_at, edited_by, edited_at, visibility, owners, groups) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` , newSheet .Id , newSheet .Name , newSheet .Origin_id , newSheet .Created_by , newSheet .Created_at , newSheet .Edited_by , newSheet .Edited_at , newSheet .Visibility , newSheet .Owners , newSheet .Groups )
192
+ _ , err = conn .Exec (r . Context () , `insert into cluesheet (id, name, origin_id, created_by, created_at, edited_by, edited_at, visibility, owners, groups) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)` , newSheet .Id , newSheet .Name , newSheet .Origin_id , newSheet .Created_by , newSheet .Created_at , newSheet .Edited_by , newSheet .Edited_at , newSheet .Visibility , newSheet .Owners , newSheet .Groups )
218
193
if err != nil {
219
194
http .Error (rw , fmt .Sprintf ("failed to persist cluesheet: %s" , err ), 500 )
220
195
return
@@ -445,6 +420,41 @@ func main() {
445
420
rw .Write (data )
446
421
})
447
422
423
+ }
424
+
425
+ func main () {
426
+ ctx := context .Background ()
427
+
428
+ ctx = config .ContextWithConfig (ctx , config .GetConfig (ctx ))
429
+ ctx = log .ContextWithLogger (ctx , log .GetLogger (ctx ))
430
+
431
+ // Tag all logs with the version string if we have one
432
+ if config .FromContext (ctx ).GetString ("version" ) != "" {
433
+ ctx = log .ContextWithLogger (ctx , log .GetLogger (ctx ).With (
434
+ zap .String ("version" , config .FromContext (ctx ).GetString ("version" )),
435
+ ))
436
+ }
437
+
438
+ if config .FromContext (ctx ).GetBool ("tracing.enabled" ) {
439
+ tracer .Start (
440
+ tracer .WithEnv (config .FromContext (ctx ).GetString ("env" )),
441
+ tracer .WithService ("cluesheet" ),
442
+ tracer .WithServiceVersion (config .FromContext (ctx ).GetString ("version" )),
443
+ )
444
+ defer tracer .Stop ()
445
+ log .FromContext (ctx ).Debug ("started tracing" )
446
+ }
447
+
448
+ conn , err := pgxtrace .NewPool (ctx , connStr )
449
+ if err != nil {
450
+ log .FromContext (ctx ).Fatal ("failed setting up db pool" , zap .Error (err ))
451
+ }
452
+ defer conn .Close ()
453
+
454
+ router := muxtrace .NewRouter ()
455
+
456
+ registerRoutes (ctx , router , conn )
457
+
448
458
srv := & http.Server {
449
459
Addr : ":8080" ,
450
460
Handler : router ,
0 commit comments