-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (46 loc) · 1.04 KB
/
Copy pathmain.go
File metadata and controls
55 lines (46 loc) · 1.04 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
package main
import (
"WSServer/config"
"WSServer/handler"
"WSServer/logger"
"context"
"fmt"
"github.com/subchen/go-log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
config.Init()
logger.Init()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
httpServer := &http.Server{
Handler: handler.NewHandler(),
Addr: fmt.Sprintf(":%d", config.Get().Port),
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
}
defer shutdown(httpServer, ctx)
go serve(httpServer)
gs := make(chan os.Signal, 1)
signal.Notify(gs, syscall.SIGTERM, syscall.SIGINT)
<-gs
log.Println("exit")
os.Exit(0)
}
func serve(s *http.Server) {
log.Infof("start server: %s", s.Addr)
if err := s.ListenAndServe(); err != nil {
log.Fatalf("cant start server: %v", err)
}
}
func shutdown(s *http.Server, ctx context.Context) {
log.Infof("stop server: %s", s.Addr)
if err := s.Shutdown(ctx); err != nil {
log.Fatalf("cant stop server: %v", err)
}
}