-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
94 lines (81 loc) · 2.47 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"flag"
"log"
"os"
"time"
notify "github.com/arturoeanton/go-notify"
"github.com/gin-gonic/gin"
"github.com/luraproject/lura/config"
"github.com/luraproject/lura/logging"
"github.com/luraproject/lura/proxy"
"github.com/luraproject/lura/router"
krakendgin "github.com/luraproject/lura/router/gin"
)
var routerFactory router.Factory
func main() {
port := flag.Int("p", 9090, "Port of the service")
logLevel := flag.String("l", "ERROR", "Logging level")
debug := flag.Bool("debug", false, "Enable the debug")
directory := flag.String("d", "./", "Directory")
configFile := flag.String("c", "configuration.json", "Path to the configuration filename")
flag.Parse()
parser := config.NewParser()
serviceConfig, err := parser.Parse(*configFile)
if err != nil {
log.Fatal("ERROR:", err)
}
serviceConfig.Debug = serviceConfig.Debug || *debug
if *port != 0 {
serviceConfig.Port = *port
}
logger, err := logging.NewLogger(*logLevel, os.Stdout, "[KRAKEND]")
if err != nil {
log.Fatal("ERROR:", err.Error())
}
// routerFactory := krakendgin.DefaultFactory(proxy.DefaultFactory(logger), logger)
routerFactory = krakendgin.NewFactory(krakendgin.Config{
Engine: gin.Default(),
ProxyFactory: customProxyFactory{logger, proxy.DefaultFactory(logger)},
Logger: logger,
HandlerFactory: func(configuration *config.EndpointConfig, proxy proxy.Proxy) gin.HandlerFunc {
return krakendgin.EndpointHandler(configuration, proxy)
},
RunServer: router.RunServer,
})
r1 := routerFactory.New()
update := func(observer *notify.ObserverNotify, event *notify.Event) {
parser := config.NewParser()
time.Sleep(time.Millisecond * 500)
serviceConfig, err := parser.Parse(observer.Filename)
if err != nil {
log.Println("ERROR:", err)
return
}
r1.ResteEngine()
r1.RegisterKrakendEndpoints(serviceConfig)
log.Println("INFO:", "configuration reloaded")
}
notify.NewObserverNotify(*directory, *configFile).
FxCreate(update).
FxWrite(update).
FxChmod(update).
//FxRename(update).
//FxRemove(update).
Run()
r1.Run(serviceConfig)
}
//
// customProxyFactory adds a logging middleware wrapping the internal factory
type customProxyFactory struct {
logger logging.Logger
factory proxy.Factory
}
// New implements the Factory interface
func (cf customProxyFactory) New(cfg *config.EndpointConfig) (p proxy.Proxy, err error) {
p, err = cf.factory.New(cfg)
if err == nil {
p = proxy.NewLoggingMiddleware(cf.logger, cfg.Endpoint)(p)
}
return
}