Skip to content

Commit

Permalink
fix(presentation/http): fix handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ww24 committed Nov 12, 2022
1 parent 96de0ca commit f6d5489
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
27 changes: 12 additions & 15 deletions presentation/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,44 +41,42 @@ 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,
eventHandler: eventHandler,
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion presentation/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion presentation/http/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down

0 comments on commit f6d5489

Please sign in to comment.