This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
/
gateway.go
100 lines (88 loc) · 2.78 KB
/
gateway.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package srv
import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/opentracing/opentracing-go"
"github.com/qeelyn/gin-contrib/errorhandle"
ginTracing "github.com/qeelyn/gin-contrib/tracing"
"github.com/qeelyn/go-common/config"
"github.com/qeelyn/go-common/config/options"
"github.com/qeelyn/go-common/grpcx/registry"
"github.com/qeelyn/go-common/logger"
"github.com/qeelyn/golang-starter-kit/gateway/app"
"github.com/qeelyn/golang-starter-kit/gateway/router"
"github.com/qeelyn/golang-starter-kit/schemas/greeter"
"net/http"
"time"
)
func RunGateway(cnfOpts options.Options, register registry.Registry) error {
var (
err error
tracer opentracing.Tracer
)
cnfOpts.FileName = "gateway.yaml"
// load application configurations
if app.Config, err = config.LoadConfig(&cnfOpts); err != nil {
return err
}
appName, listen := app.Config.GetString("appname"), app.Config.GetString("listen")
app.IsDebug = app.Config.GetBool("debug")
// create the logger
app.Logger = newLogger(app.Config)
defer app.Logger.Strict().Sync()
//use grpc log for rpc client
grpc_zap.ReplaceGrpcLogger(app.Logger.Strict())
if app.Caches, err = newBatchCache(app.Config); err != nil {
panic(err)
}
//tracing
tracer = newTracing(app.Config, appName)
app.TracerFunc = ginTracing.HandleFunc(map[string]interface{}{"useOpentracing": tracer != nil})
//rpc client
cc := newDialer(true, app.Config.Sub("rpc.greeter"), tracer)
app.GreeterClient = greeter.NewGreeterClient(cc)
defer cc.Close()
router := routers.DefaultRouter()
initRouter(router)
server := &http.Server{
Addr: listen,
Handler: router,
}
return server.ListenAndServe()
}
func initRouter(g *gin.Engine) {
g.Use(app.TracerFunc)
if glevel := app.Config.GetInt("gzip"); glevel != 0 {
g.Use(gzip.Gzip(glevel))
}
if app.Config.IsSet("log.access") {
c := logger.NewFileLogger(app.Config.GetStringMap("log.access"))
accessLogger := logger.NewLogger(c)
g.Use(app.AccessLogHandleFunc(accessLogger.Strict(), time.RFC3339, false))
}
// load error messages
ef := app.Config.GetString("error-template")
if ef != "" {
g.Use(errorhandle.ErrorHandle(map[string]interface{}{
"error-template": ef,
}, app.Logger))
}
if app.Config.GetBool("jwt.enable") {
pubKeyKey := "jwt.public-key"
if app.Config.IsSet(pubKeyKey) {
if err := config.ResetFromSource(app.Config, pubKeyKey); err != nil {
panic(err)
}
}
authConfig := app.Config.GetStringMap("jwt")
//init middleware
app.AuthHanlerFunc = app.NewAuthMiddleware(authConfig).Handle()
}
// check access
if app.Config.IsSet("auth") {
app.CheckAccessMiddleware = app.NewCheckAccessMiddleware(app.Config.GetStringMap("auth"))
}
routers.SetupRouterGroup(g)
routers.SetGraphQlRouterGroup(g)
}