-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (99 loc) · 2.75 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
CommitHash = "000000"
checkErr = func(err error) {
if err != nil && err != http.ErrServerClosed {
log.Println("something error while writing response", err.Error())
}
}
)
const (
port = 8080
defGracefulTimeout = 60 * time.Second
)
type resp struct {
Code string `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
Errors interface{} `json:"errors,omitempty"`
Version string `json:"version"`
}
func main() {
log.Printf("started open API server version: %s on PORT: %d", CommitHash, port)
response := resp{
Code: "SUCCESS",
Message: "Hello world!",
Version: CommitHash,
}
marshaledResp, err := json.MarshalIndent(response, "", " ")
if err != nil {
log.Println("something error while marshaling response", err.Error())
}
pong := resp{
Code: "PONG",
Message: "pong",
Version: CommitHash,
}
marshaledPong, err := json.MarshalIndent(pong, "", " ")
if err != nil {
log.Println("something error while marshaling response", err.Error())
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write(marshaledResp)
checkErr(err)
})
mux.HandleFunc("/ping", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := w.Write(marshaledPong)
checkErr(err)
})
server := &http.Server{
Addr: fmt.Sprint(":", port),
Handler: mux,
}
mainCtx, cancel := context.WithTimeout(context.Background(), defGracefulTimeout)
defer cancel()
terminationHook := shutdownHook()
terminationCH := make(chan struct{})
terminationHook(terminationCH)
go func() {
err = server.ListenAndServe()
checkErr(err)
}()
<-terminationCH
err = server.Shutdown(mainCtx)
if err != nil {
log.Println("something error while shutting down the server", err.Error())
}
log.Println(fmt.Sprintf("stopped open API on port: %d", port))
}
func shutdownHook() func(chan struct{}) {
return func(processingCH chan struct{}) {
go func(proCH chan struct{}) {
interruptionSignal := make(chan os.Signal, 1)
signal.Notify(interruptionSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2)
// watch interruption signal
terminationSignal := <-interruptionSignal
log.Println(fmt.Sprint("caught interruption signal: ", terminationSignal))
// send interruption signal to the client through the registered channel
proCH <- struct{}{}
// stop relaying incoming signals
signal.Stop(interruptionSignal)
}(processingCH)
}
}