[fix] Refactored handler initialization
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Piotr Biernat 2022-04-23 21:13:14 +02:00
parent 41292b25e3
commit bf3f5a0fc0
3 changed files with 14 additions and 10 deletions

View File

@ -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) {

View File

@ -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
}

View File

@ -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())
}
}