-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unneeded log param from handlers and middlewares
We can extract logger.Logger from context.Context.
- Loading branch information
Showing
7 changed files
with
71 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,62 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/dsh2dsh/zrepl/internal/logger" | ||
) | ||
|
||
func JsonResponder[T any](log logger.Logger, h func() (T, error)) Middleware { | ||
return func(next http.Handler) http.Handler { | ||
return &jsonResponder[T]{log: log, handler: h} | ||
func JsonResponder[T any](h func(context.Context) (T, error)) Middleware { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
res, err := h(r.Context()) | ||
if err != nil { | ||
writeError(w, r, err, "control handler error") | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(&res); err != nil { | ||
writeError(w, r, err, "control handler json marshal error") | ||
} | ||
} | ||
return func(next http.Handler) http.Handler { return http.HandlerFunc(fn) } | ||
} | ||
|
||
type jsonResponder[T any] struct { | ||
log logger.Logger | ||
handler func() (T, error) | ||
} | ||
|
||
func (self *jsonResponder[T]) ServeHTTP(w http.ResponseWriter, | ||
r *http.Request, | ||
func writeError(w http.ResponseWriter, r *http.Request, err error, msg string, | ||
) { | ||
res, err := self.handler() | ||
if err != nil { | ||
self.writeError(err, w, "control handler error") | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(&res); err != nil { | ||
self.writeError(err, w, "control handler json marshal error") | ||
} | ||
writeErrorCode(w, r, http.StatusInternalServerError, err, msg) | ||
} | ||
|
||
func (self *jsonResponder[T]) writeError(err error, w http.ResponseWriter, | ||
msg string, | ||
func writeErrorCode(w http.ResponseWriter, r *http.Request, statusCode int, | ||
err error, msg string, | ||
) { | ||
self.log.WithError(err).Error(msg) | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
getLogger(r).WithError(err).Error(msg) | ||
http.Error(w, err.Error(), statusCode) | ||
} | ||
|
||
// -------------------------------------------------- | ||
|
||
func JsonRequestResponder[T1 any, T2 any](log logger.Logger, | ||
h func(req *T1) (T2, error), | ||
func JsonRequestResponder[T1 any, T2 any](h func(ctx context.Context, req *T1, | ||
) (T2, error), | ||
) Middleware { | ||
return func(next http.Handler) http.Handler { | ||
return &jsonRequestResponder[T1, T2]{log: log, handler: h} | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
var req T1 | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
writeErrorCode(w, r, http.StatusBadRequest, err, | ||
"control handler json unmarshal error", | ||
) | ||
return | ||
} | ||
|
||
resp, err := h(r.Context(), &req) | ||
if err != nil { | ||
writeError(w, r, err, "control handler error") | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(&resp); err != nil { | ||
writeError(w, r, err, "control handler json marshal error") | ||
} | ||
} | ||
} | ||
|
||
type jsonRequestResponder[T1 any, T2 any] struct { | ||
log logger.Logger | ||
handler func(req *T1) (T2, error) | ||
} | ||
|
||
func (self *jsonRequestResponder[T1, T2]) ServeHTTP(w http.ResponseWriter, | ||
r *http.Request, | ||
) { | ||
var req T1 | ||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
self.writeError(err, w, "control handler json unmarshal error", | ||
http.StatusBadRequest) | ||
return | ||
} | ||
|
||
resp, err := self.handler(&req) | ||
if err != nil { | ||
self.writeError(err, w, "control handler error", | ||
http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
if err := json.NewEncoder(w).Encode(&resp); err != nil { | ||
self.writeError(err, w, "control handler json marshal error", | ||
http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
func (self *jsonRequestResponder[T1, T2]) writeError(err error, | ||
w http.ResponseWriter, msg string, statusCode int, | ||
) { | ||
self.log.WithError(err).Error(msg) | ||
http.Error(w, err.Error(), statusCode) | ||
return func(next http.Handler) http.Handler { return http.HandlerFunc(fn) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters