From f6d54899d295134be7b1257a2082d408f6c80ddc Mon Sep 17 00:00:00 2001 From: ww24 Date: Sun, 13 Nov 2022 03:12:51 +0900 Subject: [PATCH] fix(presentation/http): fix handler --- presentation/http/handler.go | 27 ++++++++++++--------------- presentation/http/middleware.go | 2 +- presentation/http/middleware_test.go | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/presentation/http/handler.go b/presentation/http/handler.go index 6954f904..33b04b11 100644 --- a/presentation/http/handler.go +++ b/presentation/http/handler.go @@ -23,11 +23,10 @@ import ( // Set provides a wire set. var Set = wire.NewSet( NewHandler, - wire.Bind(new(http.Handler), new(*Handler)), NewAuthorizer, ) -type Handler struct { +type handler struct { log *logger.Logger bot service.Bot auth *Authorizer @@ -42,8 +41,8 @@ func NewHandler( auth *Authorizer, eventHandler usecase.EventHandler, imageHandler usecase.ImageHandler, -) (*Handler, error) { - return &Handler{ +) (http.Handler, error) { + h := &handler{ log: log, bot: bot, auth: auth, @@ -51,35 +50,33 @@ func NewHandler( imageHandler: imageHandler, middlewares: []func(http.Handler) http.Handler{ tracer.HTTPMiddleware(), - PanicHandler(log), + panicHandler(log), }, - }, nil -} + } -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { mux := http.NewServeMux() mux.HandleFunc("/", h.healthCheck()) mux.HandleFunc("/line_callback", h.lineCallback()) mux.HandleFunc("/scheduler", h.executeScheduler()) mux.HandleFunc("/reminder", h.executeReminder()) mux.HandleFunc("/image/", h.serveImage()) - h.registerMiddleware(mux).ServeHTTP(w, r) + return h.registerMiddleware(mux), nil } -func (h *Handler) registerMiddleware(handler http.Handler) http.Handler { +func (h *handler) registerMiddleware(handler http.Handler) http.Handler { for _, middleware := range h.middlewares { handler = middleware(handler) } return handler } -func (h *Handler) healthCheck() func(w http.ResponseWriter, r *http.Request) { +func (h *handler) healthCheck() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } } -func (h *Handler) lineCallback() func(w http.ResponseWriter, r *http.Request) { +func (h *handler) lineCallback() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() cl := h.log.WithTraceFromContext(ctx) @@ -102,7 +99,7 @@ func (h *Handler) lineCallback() func(w http.ResponseWriter, r *http.Request) { } } -func (h *Handler) executeScheduler() func(w http.ResponseWriter, r *http.Request) { +func (h *handler) executeScheduler() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() cl := h.log.WithTraceFromContext(ctx) @@ -138,7 +135,7 @@ func (h *Handler) executeScheduler() func(w http.ResponseWriter, r *http.Request } } -func (h *Handler) executeReminder() func(w http.ResponseWriter, r *http.Request) { +func (h *handler) executeReminder() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() cl := h.log.WithTraceFromContext(ctx) @@ -195,7 +192,7 @@ func (h *Handler) executeReminder() func(w http.ResponseWriter, r *http.Request) } } -func (h *Handler) serveImage() func(w http.ResponseWriter, r *http.Request) { +func (h *handler) serveImage() func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() cl := h.log.WithTraceFromContext(ctx) diff --git a/presentation/http/middleware.go b/presentation/http/middleware.go index 8a323a2d..d7be59b7 100644 --- a/presentation/http/middleware.go +++ b/presentation/http/middleware.go @@ -8,7 +8,7 @@ import ( "github.com/ww24/linebot/logger" ) -func PanicHandler(log *logger.Logger) func(http.Handler) http.Handler { +func panicHandler(log *logger.Logger) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { diff --git a/presentation/http/middleware_test.go b/presentation/http/middleware_test.go index 6937d52d..37081fdc 100644 --- a/presentation/http/middleware_test.go +++ b/presentation/http/middleware_test.go @@ -44,7 +44,7 @@ func TestPanicHandler(t *testing.T) { t.Parallel() w := httptest.NewRecorder() r := httptest.NewRequest(http.MethodGet, "/", nil) - PanicHandler(logger.NewNop())(tt.h).ServeHTTP(w, r) + panicHandler(logger.NewNop())(tt.h).ServeHTTP(w, r) assert.Equal(t, tt.wantStatus, w.Code) assert.Equal(t, tt.wantBody, w.Body.String()) })