-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 1.17 KB
/
main.go
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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/agschwender/autoreload"
)
func main() {
log.Printf("Starting application")
// In a real production environment, you will likely want the
// autoreload flag or environment variables to be defaulted to false
// so that it is only enabled in the local environment.
var shouldAutoReload bool
flag.BoolVar(&shouldAutoReload, "autoreload", true, "enable autoreload")
flag.Parse()
server := &http.Server{Addr: ":8000"}
if shouldAutoReload {
log.Printf("Auto-reload is enabled")
autoreload.New(
autoreload.WithMaxAttempts(6),
autoreload.WithOnReload(func() {
log.Printf("Received change event, shutting down")
server.Shutdown(context.Background())
}),
).Start()
}
go func() {
log.Printf("Starting HTTP server")
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("HTTP server error: %v", err)
}
}()
interruptChannel := make(chan os.Signal, 1)
signal.Notify(interruptChannel, os.Interrupt, syscall.SIGTERM)
<-interruptChannel
log.Printf("Received interrupt signal, shutting down")
server.Shutdown(context.Background())
}