-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (40 loc) · 1.2 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/chrischenyc/microservices-in-go/ep-03/handlers"
)
func main() {
apiLogger := log.New(os.Stdout, "[api] ", log.LstdFlags)
serveMux := http.NewServeMux()
serveMux.Handle("/", handlers.NewHelloHandler(apiLogger))
serveMux.Handle("/goodbye", handlers.NewGoodbyeHandler(apiLogger))
serveMux.Handle("/products", handlers.NewProducts(apiLogger))
server := http.Server{
Addr: ":3000",
Handler: serveMux,
IdleTimeout: 10 * time.Second,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
}
// listening to port in a go routine, so it won't block the rest
go func() {
err := server.ListenAndServe()
if err != nil {
apiLogger.Fatal(err)
}
}()
osSignalChan := make(chan os.Signal, 1)
// use syscall.SIGTERM instead of os.Kill: https://github.com/braintree/manners/issues/45
signal.Notify(osSignalChan, os.Interrupt, syscall.SIGTERM)
sig := <-osSignalChan
apiLogger.Print("received OS termination signal, gracefully shut down", sig)
timeoutContext, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(timeoutContext)
}