forked from krakend/krakend-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_engine.go
46 lines (35 loc) · 1.62 KB
/
router_engine.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
package krakend
import (
"github.com/gin-gonic/gin"
botdetector "github.com/krakendio/krakend-botdetector/v2/gin"
httpsecure "github.com/krakendio/krakend-httpsecure/v2/gin"
lua "github.com/krakendio/krakend-lua/v2/router/gin"
opencensus "github.com/krakendio/krakend-opencensus/v2/router/gin"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/core"
luragin "github.com/luraproject/lura/v2/router/gin"
"github.com/luraproject/lura/v2/transport/http/server"
)
// NewEngine creates a new gin engine with some default values and a secure middleware
func NewEngine(cfg config.ServiceConfig, opt luragin.EngineOptions) *gin.Engine {
engine := luragin.NewEngine(cfg, opt)
engine.NoRoute(opencensus.HandlerFunc(&config.EndpointConfig{Endpoint: "NoRoute"}, defaultHandler, nil))
engine.NoMethod(opencensus.HandlerFunc(&config.EndpointConfig{Endpoint: "NoMethod"}, defaultHandler, nil))
logPrefix := "[SERVICE: Gin]"
if err := httpsecure.Register(cfg.ExtraConfig, engine); err != nil && err != httpsecure.ErrNoConfig {
opt.Logger.Warning(logPrefix+"[HTTPsecure]", err)
} else if err == nil {
opt.Logger.Debug(logPrefix + "[HTTPsecure] Successfuly loaded module")
}
lua.Register(opt.Logger, cfg.ExtraConfig, engine)
botdetector.Register(cfg, opt.Logger, engine)
return engine
}
func defaultHandler(c *gin.Context) {
c.Header(core.KrakendHeaderName, core.KrakendHeaderValue)
c.Header(server.CompleteResponseHeaderName, server.HeaderIncompleteResponseValue)
}
type engineFactory struct{}
func (engineFactory) NewEngine(cfg config.ServiceConfig, opt luragin.EngineOptions) *gin.Engine {
return NewEngine(cfg, opt)
}