-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
72 lines (61 loc) · 1.39 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"time"
"github.com/zhanglindeng/goweb/config"
"github.com/zhanglindeng/goweb/model"
"github.com/zhanglindeng/goweb/route"
"github.com/zhanglindeng/goweb/schedule"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
// true: goweb.exe -migrate
migrate := flag.Bool("migrate", false, "migrate database")
flag.Parse()
r, err := route.Create()
if err != nil {
log.Fatalln(err)
}
// migrate database
if *migrate {
log.Println("database migrating...")
if err := model.Migrate(); err != nil {
log.Fatalln(err)
}
log.Println("database migrated")
}
// schedule
if err := schedule.Create(); err != nil {
log.Fatalln(err)
}
srv := &http.Server{
Addr: config.AppPort,
Handler: r,
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
log.Println("server listen port:", config.AppPort)
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 30 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}