]> localhost Git - tq-wave11-pf.git/commitdiff
feat: handle signal
authorJansen <[email protected]>
Thu, 7 Aug 2025 21:21:22 +0000 (17:21 -0400)
committerJansen <[email protected]>
Thu, 7 Aug 2025 21:21:45 +0000 (17:21 -0400)
cmd/api/main.go
pkg/http/dish.go
pkg/http/http.go
pkg/http/menu.go

index 0cffd3e371ef4ab9631a833df78841c3912f1538..e259d5dde693fd8b9b521335250f881c7f4cfe87 100644 (file)
@@ -1,8 +1,14 @@
 package main
 
 import (
+       "log"
+       "os"
+       "os/signal"
+       "syscall"
+
        "jsdaj.tq/pf/persistence/inmemory"
        "jsdaj.tq/pf/pkg/http"
+       "jsdaj.tq/pf/pkg/util"
 )
 
 const PORT int = 8080
@@ -12,4 +18,18 @@ func main() {
 
        serv := http.InitServer(PORT, &persistence)
        serv.Start()
+
+       handleSignal(serv)
+}
+
+func handleSignal(serv http.Server) {
+       sigChan := make(chan os.Signal, 1)
+       signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt)
+
+       sig := <-sigChan
+       switch sig {
+       default:
+               log.Printf("%s?? Signal detected, shutting down server...%s\n", util.Yellow, util.Reset)
+               serv.Shutdown()
+       }
 }
index b7806e795968b6f402987fe97eaa0e618d0bd428..3186eff8e4e53b01174a5153ad8778a1c8719bf4 100644 (file)
@@ -4,24 +4,13 @@ import (
        shttp "net/http"
 )
 
-func dishHandleCreate(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) dishHandleCreate(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func dishHandleUpdate(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) dishHandleUpdate(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func dishHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) dishHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func dishHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) dishHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func dishHandleList(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) dishHandleList(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func dishHandler() shttp.Handler {
-       mux := shttp.NewServeMux()
-
-       mux.HandleFunc("POST /dish", dishHandleCreate)
-       mux.HandleFunc("PUT /dish/{id}", dishHandleUpdate)
-       mux.HandleFunc("DELETE /dish/{id}", dishHandleDelete)
-       mux.HandleFunc("GET /dish/{id}", dishHandleGet)
-       mux.HandleFunc("GET /dish", dishHandleList)
-
-       return mux
-}
index f4ad4d0976d66ad8df0c0437c7216852fcf8b6c6..9571cfcc5b9706ccaf3e421f1e08f6b69e6700ba 100644 (file)
@@ -1,9 +1,11 @@
 package http
 
 import (
+       "context"
        "fmt"
        "log"
        shttp "net/http"
+       "time"
 
        "jsdaj.tq/pf/persistence"
        "jsdaj.tq/pf/pkg/http/util"
@@ -24,8 +26,17 @@ func (s *Server) Start() {
 
        s.serv.Handler = util.Middlewares(s.mux)
 
-       log.Printf(">> Starting server at :%d\n", s.port)
-       s.serv.ListenAndServe()
+       go func() {
+               log.Printf(">> Starting server at :%d\n", s.port)
+               s.serv.ListenAndServe()
+       }()
+}
+
+func (s *Server) Shutdown() {
+       ctx, cancel := context.WithTimeout(context.Background(), time.Second * 15)
+       defer cancel()
+
+       _ = s.serv.Shutdown(ctx)
 }
 
 func InitServer(port int, persist persistence.Persistence) Server {
@@ -52,6 +63,15 @@ func (s *Server) initRoutes() {
        s.mux.HandleFunc("GET /ingredient/{id}", s.ingredientHandleGet)
        s.mux.HandleFunc("GET /ingredient", s.ingredientHandleList)
 
-       s.mux.Handle("/dish", dishHandler())
-       s.mux.Handle("/menu", menuHandler())
+       s.mux.HandleFunc("POST /dish", s.dishHandleCreate)
+       s.mux.HandleFunc("PUT /dish/{id}", s.dishHandleUpdate)
+       s.mux.HandleFunc("DELETE /dish/{id}", s.dishHandleDelete)
+       s.mux.HandleFunc("GET /dish/{id}", s.dishHandleGet)
+       s.mux.HandleFunc("GET /dish", s.dishHandleList)
+
+       s.mux.HandleFunc("POST /menu", s.menuHandleCreate)
+       s.mux.HandleFunc("PUT /menu/{id}", s.menuHandleUpdate)
+       s.mux.HandleFunc("DELETE /menu/{id}", s.menuHandleDelete)
+       s.mux.HandleFunc("GET /menu/{id}", s.menuHandleGet)
+       s.mux.HandleFunc("GET /menu", s.menuHandleList)
 }
index 01f02cf4a3589cc4bfd45fed848a5100bcf00bb9..0411e5029265f557ed4d0d42d1423c5364951493 100644 (file)
@@ -4,24 +4,12 @@ import (
        shttp "net/http"
 )
 
-func menuHandleCreate(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) menuHandleCreate(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func menuHandleUpdate(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) menuHandleUpdate(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func menuHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) menuHandleDelete(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func menuHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
+func (s *Server) menuHandleGet(w shttp.ResponseWriter, r *shttp.Request) {}
 
-func menuHandleList(w shttp.ResponseWriter, r *shttp.Request) {}
-
-func menuHandler() shttp.Handler {
-       mux := shttp.NewServeMux()
-
-       mux.HandleFunc("POST /menu", menuHandleCreate)
-       mux.HandleFunc("PUT /menu/{id}", menuHandleUpdate)
-       mux.HandleFunc("DELETE /menu/{id}", menuHandleDelete)
-       mux.HandleFunc("GET /menu/{id}", menuHandleGet)
-       mux.HandleFunc("GET /menu", menuHandleList)
-
-       return mux
-}
+func (s *Server) menuHandleList(w shttp.ResponseWriter, r *shttp.Request) {}