Openstatus
www.openstatus.dev
1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7 "net/http"
8 "os/signal"
9 "syscall"
10 "time"
11
12 "github.com/openstatushq/openstatus/apps/private-location/internal/server"
13)
14
15func gracefulShutdown(apiServer *http.Server, cleanup func(context.Context), done chan bool) {
16 // Create context that listens for the interrupt signal from the OS.
17 ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
18 defer stop()
19
20 // Listen for the interrupt signal.
21 <-ctx.Done()
22
23 fmt.Println("shutting down gracefully, press Ctrl+C again to force")
24 stop() // Allow Ctrl+C to force shutdown
25
26 // The context is used to inform the server it has 5 seconds to finish
27 // the request it is currently handling
28 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
29 defer cancel()
30 if err := apiServer.Shutdown(ctx); err != nil {
31 log.Printf("Server forced to shutdown with error: %v", err)
32 }
33
34 // Cleanup log provider
35 cleanup(ctx)
36
37 log.Println("Server exiting")
38
39 // Notify the main goroutine that the shutdown is complete
40 done <- true
41}
42
43func main() {
44
45 server, cleanup := server.NewServer()
46
47 // Create a done channel to signal when the shutdown is complete
48 done := make(chan bool, 1)
49
50 // Run graceful shutdown in a separate goroutine
51 go gracefulShutdown(server, cleanup, done)
52
53 err := server.ListenAndServe()
54 if err != nil && err != http.ErrServerClosed {
55 panic(fmt.Sprintf("http server error: %s", err))
56 }
57
58 // Wait for the graceful shutdown to complete
59 <-done
60 log.Println("Graceful shutdown complete.")
61}