-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (60 loc) · 1.75 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
72
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/hutaochu/hello-hutao/constants"
"github.com/hutaochu/hello-hutao/docs"
"github.com/hutaochu/hello-hutao/internal/config"
"github.com/hutaochu/hello-hutao/internal/entities"
"github.com/hutaochu/hello-hutao/pkg/middlewares"
"github.com/hutaochu/hello-hutao/pkg/utils/logger"
"github.com/hutaochu/hello-hutao/web"
"github.com/hutaochu/hello-hutao/web/handlers"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
config.InitEnv()
config.InitConfig()
entities.DBOpen()
router := gin.Default()
docs.SwaggerInfo.BasePath = ""
port := os.Getenv("PORT")
if port == "" {
port = constants.DefaultPort
}
v1 := router.Group("/apis/v1")
v1.Use(middlewares.Trace())
v1.Use(middlewares.RequestLogger())
v1.Use(middlewares.HandleResponse())
web.Routes(v1)
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.GET("/public_key", middlewares.HandleResponse(), handlers.GetPublicKey)
server := &http.Server{
Addr: ":8080",
Handler: router,
}
logger.Logger.Info(fmt.Sprintf("server start to listen on: %s", port))
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
logger.Logger.Info(fmt.Sprintf("Start shutdown server with signal: %s", s.String()))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("Server shutdown: \n", err)
}
logger.Logger.Info("Server shutdown")
}