forked from reportportal/service-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (65 loc) · 2.08 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
package main
import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/reportportal/commons-go/commons"
"github.com/reportportal/commons-go/conf"
"github.com/reportportal/commons-go/server"
"github.com/reportportal/service-index/aggregator"
"github.com/reportportal/service-index/k8s"
"github.com/reportportal/service-index/traefik"
log "github.com/sirupsen/logrus"
"net/http"
"time"
)
const httpClientTimeout = 5 * time.Second
func main() {
cfg := conf.EmptyConfig()
rpCfg := struct {
*conf.ServerConfig
K8sMode bool `env:"K8S_MODE" envDefault:"false"`
TraefikLbURL string `env:"LB_URL" envDefault:"http://localhost:9091"`
}{
ServerConfig: cfg,
}
err := conf.LoadConfig(&rpCfg)
if nil != err {
log.Fatalf("Cannot load config %s", err.Error())
}
info := commons.GetBuildInfo()
info.Name = "Index Service"
srv := server.New(rpCfg.ServerConfig, info)
log.Infof("K8S mode enabled: %t", rpCfg.K8sMode)
var aggreg aggregator.Aggregator
if rpCfg.K8sMode {
aggreg, err = k8s.NewAggregator(httpClientTimeout)
if nil != err {
log.Fatalf("Incorrect K8S config %s", err.Error())
}
} else {
aggreg = traefik.NewAggregator(rpCfg.TraefikLbURL, httpClientTimeout)
}
srv.WithRouter(func(router *chi.Mux) {
router.Use(middleware.Logger)
router.NotFound(func(w http.ResponseWriter, rq *http.Request) {
http.Redirect(w, rq, "/ui/#notfound", http.StatusFound)
})
router.HandleFunc("/composite/info", func(w http.ResponseWriter, r *http.Request) {
if err := server.WriteJSON(http.StatusOK, aggreg.AggregateInfo(), w); nil != err {
log.Error(err)
}
})
router.HandleFunc("/composite/health", func(w http.ResponseWriter, r *http.Request) {
if err := server.WriteJSON(http.StatusOK, aggreg.AggregateHealth(), w); nil != err {
log.Error(err)
}
})
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ui/", http.StatusFound)
})
router.HandleFunc("/ui", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/ui/", http.StatusFound)
})
})
srv.StartServer()
}