-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (67 loc) · 1.74 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
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-playground/log"
"github.com/go-playground/log/handlers/console"
)
func handleRoot(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Hello Universe!")
}
func mainWithStatus() int {
cLog := console.New()
log.RegisterHandler(cLog, log.AllLevels...)
defer log.Trace("Https server finished").End()
signalChannel := make(chan os.Signal, 10)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGABRT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
listenAddrFlag := flag.String("listen", "0.0.0.0:5000", "Listen address for http server")
flag.Parse()
listenAddr := *listenAddrFlag
mux := http.NewServeMux()
mux.HandleFunc("/", handleRoot)
server := &http.Server{
Addr: listenAddr,
Handler: mux,
}
listener, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Error("Failed to listen at '%s': %s", listenAddr, err)
return 1
}
// if you don't specify addr (e.g. port) we need to find to which it was bound so e.g. tests can use it
listenAddr = listener.Addr().String()
log.Infof("Http server listening on http://%s/", listenAddr)
// TODO: rewrite this to use Shutdown method once we have Go 1.8
go func() {
<-ctx.Done()
log.Infof("Stopping http server listening on http://%s/", listenAddr)
listener.Close()
}()
var errorChannel chan error
go func() {
errorChannel <- server.Serve(listener)
}()
for {
select {
case err := <-errorChannel:
log.Error(err)
return 1
case s := <-signalChannel:
log.Infof("Cancelling due to signal '%s'", s)
cancel()
return 0
}
}
}
func main() {
os.Exit(mainWithStatus())
}