-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
60 lines (49 loc) · 1.27 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/otoolep/go-httpd/httpd"
"github.com/otoolep/go-httpd/store"
)
// DefaultHTTPAddr is the default HTTP bind address.
const DefaultHTTPAddr = ":8080"
// Parameters
var httpAddr string
// init initializes this package.
func init() {
flag.StringVar(&httpAddr, "addr", DefaultHTTPAddr, "Set the HTTP bind address")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
flag.PrintDefaults()
}
}
// main is the entry point for the service.
func main() {
flag.Parse()
s := store.New()
if err := s.Open(); err != nil {
log.Fatalf("failed to open store: %s", err.Error())
}
h := httpd.New(httpAddr, s)
if err := h.Start(); err != nil {
log.Fatalf("failed to start HTTP service: %s", err.Error())
}
log.Println("httpd started successfully")
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
// Block until one of the signals above is received
select {
case <-signalCh:
log.Println("signal received, shutting down...")
if err := s.Close(); err != nil {
log.Println("failed to close store:", err.Error())
}
if err := h.Close(); err != nil {
log.Println("failed to close HTTP service:", err.Error())
}
}
}