1
1
package api
2
2
3
+ //go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config cfg.yaml ../openapi.yaml
4
+
3
5
import (
4
6
"bytes"
5
7
"context"
6
8
"encoding/json"
7
9
"errors"
8
10
"fmt"
11
+ "log/slog"
9
12
"net/http"
10
13
11
14
"github.com/chadweimer/gomp/db"
15
+ "github.com/chadweimer/gomp/middleware"
12
16
"github.com/chadweimer/gomp/upload"
13
- "github.com/go-chi/chi/v5"
14
- "github.com/go-chi/chi/v5/middleware"
15
- "github.com/rs/zerolog"
16
- "github.com/rs/zerolog/hlog"
17
- "github.com/rs/zerolog/log"
18
17
)
19
18
20
19
// ---- Begin Standard Errors ----
@@ -31,10 +30,7 @@ func (k contextKey) String() string {
31
30
return "gomp context key: " + string (k )
32
31
}
33
32
34
- const (
35
- currentUserIdCtxKey = contextKey ("current-user-id" )
36
- currentUserTokenCtxKey = contextKey ("current-user-token" )
37
- )
33
+ const currentUserIdCtxKey = contextKey ("current-user-id" )
38
34
39
35
// ---- End Context Keys ----
40
36
@@ -45,16 +41,14 @@ type apiHandler struct {
45
41
}
46
42
47
43
// NewHandler returns a new instance of http.Handler
48
- func NewHandler (secureKeys []string , upl * upload.ImageUploader , db db.Driver ) http.Handler {
44
+ func NewHandler (secureKeys []string , upl * upload.ImageUploader , drDriver db.Driver ) http.Handler {
49
45
h := apiHandler {
50
46
secureKeys : secureKeys ,
51
47
upl : upl ,
52
- db : db ,
48
+ db : drDriver ,
53
49
}
54
50
55
- r := chi .NewRouter ()
56
- r .Use (middleware .SetHeader ("Content-Type" , "application/json" ))
57
- r .Mount ("/v1" , HandlerWithOptions (NewStrictHandlerWithOptions (
51
+ return HandlerWithOptions (NewStrictHandlerWithOptions (
58
52
h ,
59
53
[]StrictMiddlewareFunc {},
60
54
StrictHTTPServerOptions {
@@ -65,25 +59,26 @@ func NewHandler(secureKeys []string, upl *upload.ImageUploader, db db.Driver) ht
65
59
writeErrorResponse (w , r , http .StatusInternalServerError , err )
66
60
},
67
61
}),
68
- ChiServerOptions {
62
+ StdHTTPServerOptions {
63
+ BaseURL : "/v1" ,
69
64
Middlewares : []MiddlewareFunc {h .checkScopes },
70
65
ErrorHandlerFunc : func (w http.ResponseWriter , r * http.Request , err error ) {
71
66
writeErrorResponse (w , r , http .StatusBadRequest , err )
72
67
},
73
- }))
74
- r .NotFound (func (w http.ResponseWriter , r * http.Request ) {
75
- writeErrorResponse (w , r , http .StatusNotFound , fmt .Errorf ("%s is not a valid API endpoint" , r .URL .Path ))
76
- })
68
+ })
69
+ }
77
70
78
- return r
71
+ func logger (ctx context.Context ) * slog.Logger {
72
+ return middleware .GetLoggerFromContext (ctx )
79
73
}
80
74
81
- func writeJSONResponse (w http.ResponseWriter , r * http.Request , status int , v interface {} ) {
75
+ func writeJSONResponse (w http.ResponseWriter , r * http.Request , status int , v any ) {
82
76
buf := new (bytes.Buffer )
83
77
if err := json .NewEncoder (buf ).Encode (v ); err != nil {
84
- hlog .FromRequest (r ).UpdateContext (func (c zerolog.Context ) zerolog.Context {
85
- return c .AnErr ("encode-error" , err ).Int ("original-status" , status )
86
- })
78
+ logger (r .Context ()).
79
+ Error ("Failed to encode response" ,
80
+ "error" , err ,
81
+ "original-status" , status )
87
82
88
83
w .WriteHeader (http .StatusInternalServerError )
89
84
return
@@ -96,14 +91,8 @@ func writeJSONResponse(w http.ResponseWriter, r *http.Request, status int, v int
96
91
}
97
92
}
98
93
99
- func logErrorToContext (ctx context.Context , err error ) {
100
- log .Ctx (ctx ).UpdateContext (func (c zerolog.Context ) zerolog.Context {
101
- return c .Err (err )
102
- })
103
- }
104
-
105
94
func writeErrorResponse (w http.ResponseWriter , r * http.Request , status int , err error ) {
106
- logErrorToContext (r .Context (), err )
95
+ logger (r .Context ()). Error ( "failure on request" , "error" , err )
107
96
status = getStatusFromError (err , status )
108
97
writeJSONResponse (w , r , status , http .StatusText (status ))
109
98
}
0 commit comments