-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 1.58 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/itsubaki/mackerel-server-go/infrastructure"
"github.com/itsubaki/mackerel-server-go/infrastructure/config"
"github.com/itsubaki/mackerel-server-go/infrastructure/handler"
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
c := config.New()
log.Printf("%#v\n", c)
h, err := handler.New(c.Driver, c.Host, c.Database, handler.Opt{
SQLMode: c.SQLMode,
Timeout: &c.Timeout,
Sleep: &c.Sleep,
MaxIdleConns: &c.MaxIdleConns,
MaxOpenConns: &c.MaxOpenConns,
ConnMaxLifetime: &c.ConnMaxLifetime,
})
if err != nil {
log.Printf("handler new: %v", err)
}
log.Printf("db connected")
if c.RunFixture {
if err := infrastructure.RunFixture(h); err != nil {
log.Fatalf("run fixture: %v", err)
}
}
r := infrastructure.Default()
infrastructure.APIv0(r, h)
s := &http.Server{
Addr: fmt.Sprintf(":%s", c.Port),
Handler: r,
}
go func() {
log.Println("http server listen and serve")
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatalf("http server shutdown: %v\n", err)
}
if err := h.Close(); err != nil {
log.Fatalf("handler closed: %v\n", err)
}
log.Println("shutdown finished")
}