-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample
More file actions
69 lines (55 loc) · 1.45 KB
/
Copy pathexample
File metadata and controls
69 lines (55 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
app := NewEngine()
app.Use(Logger())
app.GET("/health", func(c *Context) error {
return c.JSON(http.StatusOK, map[string]string{"status": "UP"})
})
v1 := app.Group("/api/v1")
{
v1.GET("/users", func(c *Context) error {
return c.JSON(http.StatusOK, map[string]string{"users": "test"})
})
}
srv := &http.Server{
Addr: ":8080",
Handler: app,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
serverErrors := make(chan error, 1)
go func() {
log.Println("[START] Framework Server is running on :8080...")
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
serverErrors <- err
}
}()
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
select {
case err := <-serverErrors:
log.Fatalf("[FATAL] Error starting server: %v", err)
case sig := <-shutdown:
log.Printf("[SHUTDOWN] Signal %v received. Initiating graceful shutdown...", sig)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[ERROR] Graceful shutdown failed: %v", err)
if err := srv.Close(); err != nil {
log.Fatalf("[FATAL] Force close failed: %v", err)
}
}
}
log.Println("[STOP] Server cleanly stopped")
}