diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-07-11 00:34:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-07-11 00:34:12 +0900 |
| commit | 5048060b6f002e2ea8bdec564a708dd21b7d665f (patch) | |
| tree | 782f1c649867cc6644b0388b4cb311c5f3e80ae3 /backend | |
| parent | 8c38ceb5b864791eb6e488fe0668e737fd78c2a0 (diff) | |
| download | feedaka-5048060b6f002e2ea8bdec564a708dd21b7d665f.tar.gz feedaka-5048060b6f002e2ea8bdec564a708dd21b7d665f.tar.zst feedaka-5048060b6f002e2ea8bdec564a708dd21b7d665f.zip | |
feat(backend): implement graceful shutdown
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/main.go | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/backend/main.go b/backend/main.go index cea08fd..d01a0ed 100644 --- a/backend/main.go +++ b/backend/main.go @@ -10,8 +10,10 @@ import ( "log" "net/http" "os" + "os/signal" "strconv" "strings" + "syscall" "time" "github.com/99designs/gqlgen/graphql/handler" @@ -487,6 +489,7 @@ func main() { e.Renderer = t e.Use(middleware.Logger()) e.Use(middleware.Recover()) + e.Use(middleware.CORS()) e.GET("/", getIndex) e.GET("/settings", getSettings) @@ -529,6 +532,28 @@ func main() { } }) - err = http.ListenAndServe(":"+port, http.StripPrefix(basePath, e)) - log.Println(err) + // Setup graceful shutdown + go func() { + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) + <-sigChan + + log.Println("Shutting down server...") + cancel() + + // Give time for graceful shutdown + shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) + defer shutdownCancel() + + if err := e.Shutdown(shutdownCtx); err != nil { + log.Printf("Error during shutdown: %v\n", err) + } + }() + + log.Printf("Server starting on port %s...\n", port) + err = e.Start(":" + port) + if err != nil && err != http.ErrServerClosed { + log.Printf("Server error: %v\n", err) + } + log.Println("Server stopped") } |
