-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
55 lines (46 loc) · 1.21 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"time"
"github.com/lazygo/lazygo/examples/config"
"github.com/lazygo/lazygo/examples/framework"
"github.com/lazygo/lazygo/examples/router"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
ptrConfigPath := flag.String("c", "./config.toml", "config path")
flag.Parse()
err := config.Init(*ptrConfigPath)
if err != nil {
log.Fatalf("[msg: load config file error] [err: %v]", err)
}
app := framework.App()
app.Debug = config.ServerConfig.Debug
// Start server
go func() {
fmt.Println("Listen " + config.ServerConfig.Addr)
err = router.Init(app).Start(config.ServerConfig.Addr)
if err != nil && err != http.ErrServerClosed {
app.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := app.Shutdown(ctx); err != nil {
app.Logger.Fatal(err)
}
}