-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo-api-template.go
More file actions
67 lines (56 loc) · 1.3 KB
/
go-api-template.go
File metadata and controls
67 lines (56 loc) · 1.3 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
60
61
62
63
64
65
66
67
package main
import (
"context"
"flag"
"fmt"
"net/http"
"time"
"go-api-template/config"
"go-api-template/middleware"
"go-api-template/model"
"go-api-template/pkg/logger"
"go-api-template/pkg/prometheus"
"go-api-template/pkg/swagger"
"go-api-template/routes"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
//go:generate gengin go-api-template.api
//go:generate gen-swagger --local_api= go-api-template.api
func main() {
flag.Parse()
configFile := fmt.Sprintf("etc/go-api-template-%s.yaml", config.Env)
e := gin.New()
e.Use(
middleware.RequestId,
middleware.RequestLog,
gin.Recovery(),
middleware.CorsMiddleware,
middleware.ApiHitRecord,
)
pprof.Register(e)
config.Setup(configFile)
logger.Setup()
prometheus.Setup(e)
//redis.Setup()
model.Setup()
//query.SetDefault(model.DB())
routes.Setup(e)
swagger.Setup(e)
server := http.Server{
Addr: fmt.Sprintf("%s:%d", config.ServerConf.Host, config.ServerConf.Port),
Handler: e,
}
go func() {
fmt.Println("listen to ", server.Addr)
server.ListenAndServe()
}()
wait := config.RegisterCloseFn(func() {
defer logrus.Warning("closed api server")
ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFunc()
server.Shutdown(ctx)
})
wait()
}