-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
59 lines (50 loc) · 1.25 KB
/
Copy pathmain.go
File metadata and controls
59 lines (50 loc) · 1.25 KB
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
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/hexiaopi/drawio-store/internal/handler"
log "github.com/sirupsen/logrus"
)
// 日志打印初始化
func initLogrus(logLevel string) error {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
level, err := log.ParseLevel(logLevel)
if err != nil {
return err
}
log.SetLevel(level)
return nil
}
func init() {
if err := initLogrus("debug"); err != nil {
panic(err)
}
}
func main() {
router := handler.RegisterRouter()
server := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
go func() {
log.Infof("service start at port %s", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("start http server fail:%v", err)
}
}()
killerChan := make(chan os.Signal)
signal.Notify(killerChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-killerChan
log.Infof("get killer signal. %v", sig)
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(timeoutCtx); err != nil {
log.Errorf("http server shut down server fail %v", err)
}
}