-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
137 lines (119 loc) · 3.92 KB
/
server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"embed"
"encoding/json"
"fmt"
"net/http"
"os"
chiprometheus "github.com/766b/chi-prometheus"
"github.com/fasibio/micropuzzle/cache"
"github.com/fasibio/micropuzzle/configloader"
"github.com/fasibio/micropuzzle/filehandler"
"github.com/fasibio/micropuzzle/fragments"
"github.com/fasibio/micropuzzle/logger"
"github.com/fasibio/micropuzzle/proxy"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
)
/* Customer endpoints */
const (
SOCKET_PATH = "socket"
SOCKET_ENDPOINT = "/micro-puzzle"
LIB_ENDPOINT = "/micro-lib/*"
)
/* Management endpoints*/
const (
METRICS_ENDPOINT = "/metrics"
HEALTH_ENDPOINT = "/health"
FRONTENDS_ENDPOINT = "/frontends"
TYPE_GENERATION_ENDPOINT = "/micro-puzzle-helper.ts"
)
//go:embed micro-lib/*.js
var embeddedLib embed.FS
type runner struct{}
func NewRunner() *runner {
return &runner{}
}
func (ru *runner) Run(c *cli.Context) error {
logs, err := logger.Initialize(c.String(CliLogLevel))
if err != nil {
return err
}
r := chi.NewRouter()
frontends, err := configloader.LoadConfig(c.String(CliMicrofrontends))
if err != nil {
return err
}
r.Use(chiprometheus.NewMiddleware("micropuzzle"))
r.Use(middleware.Compress(5))
proxy.RegisterReverseProxy(r, frontends)
cache, err := cache.NewRedisHandler(&redis.Options{
Addr: c.String(CliRedisAddress),
DB: c.Int(CliRedisDb),
Username: c.String(CliRedisUser),
Password: c.String(CliRedisPassword),
})
if err != nil {
return err
}
fragmentHandler := fragments.NewFragmentHandler(cache, cache, c.Duration(CliTimeout), frontends, c.String(CliFallbackLoader))
fragmentHandler.RegisterHandler(r, SOCKET_PATH, SOCKET_ENDPOINT)
f := filehandler.NewFileHandler(&fragmentHandler, SOCKET_PATH, *frontends)
r.Handle(LIB_ENDPOINT, http.FileServer(http.FS(embeddedLib)))
f.RegisterFileHandler(r, http.Dir(c.String(CliPublicFolder)))
for _, v := range frontends.GetPagesList() {
f.HandlePage(r, v, http.Dir(c.String(CliPublicFolder)))
}
logs.Infof("Start Server on Port :%s and Management on port %s", c.String(CliPort), c.String(CliManagementPort))
go ru.StartManagementEndpoint(c.String(CliManagementPort), frontends)
return http.ListenAndServe(fmt.Sprintf(":%s", c.String(CliPort)), r)
}
type FrontedsManagementResult struct {
Frontends []string `json:"frontends"`
}
type HealthCheckObj struct {
GitHash string `json:"git_hash"`
Version string `json:"version"`
ServiceName string `json:"service_name"`
}
func (r *runner) StartManagementEndpoint(port string, frontends *configloader.Configuration) {
managementR := chi.NewRouter()
managementR.Handle(METRICS_ENDPOINT, promhttp.Handler())
managementR.Get(HEALTH_ENDPOINT, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(HealthCheckObj{
GitHash: os.Getenv("COMMIT_SHA"),
Version: os.Getenv("APPLICATION_BUILD_ID"),
ServiceName: "mircopuzzle",
})
})
managementR.Get(FRONTENDS_ENDPOINT, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(FrontedsManagementResult{
Frontends: frontends.GetKeyList(),
})
})
managementR.Get("/debug", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
res, err := getPageDeclation(frontends.GetPagesList())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(res))
})
managementR.Get(TYPE_GENERATION_ENDPOINT, func(w http.ResponseWriter, r *http.Request) {
res, err := getTypeScriptContent(frontends.GetKeyList(), frontends.GetPagesList())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte(res))
})
http.ListenAndServe(fmt.Sprintf(":%s", port), managementR)
}