From bf3f5a0fc074cabf82d39821cb5f57b6225eeb59 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 23 Apr 2022 21:13:14 +0200 Subject: [PATCH] [fix] Refactored handler initialization --- internal/app/handler/handler.go | 7 +++++-- internal/app/router.go | 10 +++++----- internal/app/server.go | 7 ++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/internal/app/handler/handler.go b/internal/app/handler/handler.go index ca313d5..154d6c5 100644 --- a/internal/app/handler/handler.go +++ b/internal/app/handler/handler.go @@ -34,8 +34,11 @@ type response struct { Data interface{} } -func New(e *Env, h *Handler) *Handler { - return &Handler{e, h.Handle, h.Request, h.Response, Set{}} +func Init(e *Env, h *Handler) *Handler { + // return &Handler{e, h.Handle, h.Request, h.Response, Set{}} + h.Env = e + + return h } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { diff --git a/internal/app/router.go b/internal/app/router.go index 3abfe4c..59852c7 100644 --- a/internal/app/router.go +++ b/internal/app/router.go @@ -18,18 +18,18 @@ func SetupRouter(env *handler.Env) *mux.Router { r.Use(LoggingMiddleware) hc := r.PathPrefix("/health").Subrouter() - hc.Handle("", handler.New(env, handler.HealthCheckHandler)).Methods(http.MethodGet) + hc.Handle("", handler.Init(env, handler.HealthCheckHandler)).Methods(http.MethodGet) auth := r.PathPrefix("/auth").Subrouter() - auth.Handle("/login", handler.New(env, handler.AuthLoginHandler)).Methods(http.MethodPost) + auth.Handle("/login", handler.Init(env, handler.AuthLoginHandler)).Methods(http.MethodPost) api := r.PathPrefix("/api").Subrouter() api.Use(service.AuthService.ValidateUserTokenMiddleware) // only /api/** endpoints use this middleware - api.Handle("/article", handler.New(env, handler.CreateArticleHandler)).Methods(http.MethodPost) + api.Handle("/article", handler.Init(env, handler.CreateArticleHandler)).Methods(http.MethodPost) - api.Handle("/category", handler.New(env, handler.CreateCategoryHandler)).Methods(http.MethodPost) - api.Handle("/category/{id:[0-9]+}", handler.New(env, handler.DeleteCategoryHandler)).Methods(http.MethodDelete) + api.Handle("/category", handler.Init(env, handler.CreateCategoryHandler)).Methods(http.MethodPost) + api.Handle("/category/{id:[0-9]+}", handler.Init(env, handler.DeleteCategoryHandler)).Methods(http.MethodDelete) return r } diff --git a/internal/app/server.go b/internal/app/server.go index ae017e2..2ede3f0 100644 --- a/internal/app/server.go +++ b/internal/app/server.go @@ -32,8 +32,6 @@ func NewServer(env *handler.Env) *Server { } func (s *Server) Start() { - log.Println("Server listening on " + s.Addr) - if os.Getenv("LISTEN_PID") == strconv.Itoa(os.Getpid()) { // systemd run f := os.NewFile(3, "from systemd") @@ -41,9 +39,12 @@ func (s *Server) Start() { if err != nil { log.Fatalln(err) } + + log.Println("Server listening on " + l.Addr().String()) http.Serve(l, nil) } else { - // manual run + + log.Println("Server listening on " + s.Addr) log.Fatalln(s.ListenAndServe()) } }