-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (60 loc) · 1.51 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"go-ocr/utils"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"go-ocr/boot"
"go-ocr/infrastructure/config"
"go-ocr/router"
"github.com/gookit/event"
)
func main() {
flag.StringVar(&config.Env, "env", "local", "A config name that used by server")
flag.Parse()
setup := boot.MakeHandler()
handlerRouter := router.NewHandlerRouter(setup)
app := handlerRouter.RouterWithMiddleware()
port := fmt.Sprintf(":%v", config.Conf.Port)
if port == "" {
port = fmt.Sprintf(":%v", 1234)
}
log.Printf("Server running on port %s", port)
serve := &http.Server{
Addr: port,
Handler: app,
}
// Start server
go func() {
if err := serve.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shut down the server with
// a timeout of 1 second.
quit := make(chan os.Signal)
// kill (no param) default sends syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be caught, so don't need to add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutdown Server ...")
event.MustFire(utils.ShutDownEvent, nil)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
if err := serve.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
select {
case <-ctx.Done():
log.Println("timeout of 1 seconds.")
}
log.Println("Server exiting")
}