-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (54 loc) · 1.38 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"xhttp/command"
_ "xhttp/command/combine"
_ "xhttp/command/version"
"xhttp/engine"
"xhttp/storage"
)
func main() {
// init cmd
if err := command.Build(); err != nil {
log.Fatalf("command.Build() fatal error:%s", err.Error())
}
// use file storage
fileStorage := storage.NewFileStorage()
oStore := storage.NewStorage(fileStorage)
if err := oStore.Init(); err != nil {
log.Fatalf("oStore.Init() fatal error:%s", err.Error())
}
// init engine
oEngine := engine.NewEngine(engine.WithStorage(oStore))
oEngine.InitProject()
oEngine.RegisterHook(engine.StatHook)
srv := &http.Server{
Addr: ":8888",
Handler: oEngine,
ReadTimeout: time.Second * 5,
ReadHeaderTimeout: time.Second * 5,
WriteTimeout: time.Second * 5,
}
go func() {
log.Println("listen", srv.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Println("srv.ListenAndServe() err:", err.Error())
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Println("Server Shutdown:", err)
}
log.Println("Server exiting")
}