-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_server.go
111 lines (94 loc) · 2.58 KB
/
http_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
package server
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/superisaac/jsoff/net"
"net/http"
)
func requestLog(r *http.Request) *log.Entry {
return log.WithFields(log.Fields{
"remoteAddr": r.RemoteAddr,
})
}
func startServer(rootCtx context.Context, bind string, handler http.Handler, tlsConfigs ...*jsoffnet.TLSConfig) error {
return jsoffnet.ListenAndServe(
rootCtx, bind,
handler,
tlsConfigs...)
}
func startMetricsServer(rootCtx context.Context, serverCfg *ServerConfig) {
bind := serverCfg.Metrics.Bind
if bind == "" {
log.Panicf("metrics bind is empty")
return
}
handler := jsoffnet.NewAuthHandler(
serverCfg.Metrics.Auth,
promhttp.Handler())
err := startServer(
rootCtx, bind, handler,
serverCfg.Metrics.TLS,
serverCfg.TLS)
if err != nil {
log.Warnf("start metrics server error %s", err)
}
}
func adminHandler(rootCtx context.Context, authCfg *jsoffnet.AuthConfig, next http.Handler) http.Handler {
h1 := jsoffnet.NewAuthHandler(authCfg, next)
return h1
}
func relayHandler(rootCtx context.Context, authCfg *jsoffnet.AuthConfig, next http.Handler) http.Handler {
h0 := NewRatelimitHandler(rootCtx, next)
h1 := NewAccHandler(rootCtx, h0)
h2 := jsoffnet.NewAuthHandler(authCfg, h1)
return h2
}
func StartHTTPServer(rootCtx context.Context, serverCfg *ServerConfig) {
bind := serverCfg.Bind
if bind == "" {
bind = "127.0.0.1:9000"
}
log.Infof("start http proxy at %s", bind)
var adminAuth *jsoffnet.AuthConfig
if serverCfg.Admin != nil {
adminAuth = serverCfg.Admin.Auth
}
rootCtx = serverCfg.AddTo(rootCtx)
serverMux := http.NewServeMux()
serverMux.Handle("/metrics", adminHandler(
rootCtx,
serverCfg.Metrics.Auth,
promhttp.Handler()))
serverMux.Handle("/admin", adminHandler(
rootCtx,
adminAuth,
NewAdminHandler()))
serverMux.Handle("/jsonrpc/", relayHandler(
rootCtx,
serverCfg.Auth,
NewJSONRPCRelayer(rootCtx)))
serverMux.Handle("/jsonrpc-ws/", relayHandler(
rootCtx,
serverCfg.Auth,
NewJSONRPCWSRelayer(rootCtx)))
serverMux.Handle("/rest/", relayHandler(
rootCtx,
serverCfg.Auth,
NewRESTRelayer(rootCtx)))
serverMux.Handle("/graphql/", relayHandler(
rootCtx,
serverCfg.Auth,
NewGraphQLRelayer(rootCtx)))
for _, entryCfg := range serverCfg.Entrypoints {
go startEntrypointServer(rootCtx, entryCfg, serverCfg)
}
if serverCfg.Metrics.Bind != "" {
go startMetricsServer(rootCtx, serverCfg)
}
err := startServer(rootCtx, bind, serverMux, serverCfg.TLS)
if err != nil {
log.Println("HTTP Server Error - ", err)
//panic(err)
}
}