/
server.go
100 lines (85 loc) · 2.9 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
package handler
import (
"encoding/base64"
"fmt"
"io"
"net/http"
pprof "net/http/pprof"
rpprof "runtime/pprof"
"time"
// gorilla_handlers "github.com/gorilla/handlers"
ptomb "gopkg.in/tomb.v2"
"github.com/HailoOSS/api-proxy/controlplane"
"github.com/HailoOSS/api-proxy/statusmonitor"
service "github.com/HailoOSS/platform/server"
)
const faviconStr = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEX9vSx51WFPAAAADElEQVQImWNgIA0AAAAwAAFDlLdnAAAAAElFTkSuQmCC"
var favicon, _ = base64.StdEncoding.DecodeString(faviconStr)
type HailoServer struct {
Tomb *ptomb.Tomb // used to kill the server
CORSHandler *CORSHandler
HTTPServer *http.Server
RealIPHandler *RealIPHandler
ThrottlingHandler *ThrottlingHandler
Control *controlplane.ControlPlane
Monitor *statusmonitor.StatusMonitor
}
func (h *HailoServer) Kill(reason error) {
h.Tomb.Kill(reason)
h.Control.Kill(reason)
h.Monitor.Kill(reason)
h.Tomb.Wait()
h.Control.Wait()
h.Monitor.Wait()
}
// Port on which the HTTP server will listen
const listenPort = 8080
// Register our endpoint handlers in an http.ServeMux instance
func initServeMux(s *http.ServeMux, srv *HailoServer) {
s.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"version":"%v"}`, service.Version)
})
s.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write(favicon)
})
rpprof.StopCPUProfile()
s.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
s.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
s.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
s.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
rpcH := RpcHandler(srv)
s.HandleFunc("/rpc", rpcH) // RPC direct to H2 service
s.HandleFunc("/v2/h2/call", rpcH) // (Deprecated)
s.HandleFunc("/", Handler(srv)) // Default handler
// Define health check endpoint
s.HandleFunc("/v2/az/status", srv.Monitor.Handler)
s.HandleFunc("/status", statusmonitor.StatusHandler)
s.HandleFunc("/endpoints", EndpointsHandler(srv))
}
// Creates a new server, with the correct timeouts, throttling, etc.
func NewServer(accessLogWriter io.Writer) *HailoServer {
srv := &HailoServer{
Tomb: new(ptomb.Tomb),
Control: controlplane.New(),
}
srv.Monitor = statusmonitor.NewStatusMonitor()
h := http.NewServeMux()
initServeMux(h, srv)
ch := &CORSHandler{Handler: h}
pf := NewPCIFilter(ch, srv)
th := NewThrottlingHandler(pf, srv)
// gh := gorilla_handlers.LoggingHandler(accessLogWriter, th)
ih := &RealIPHandler{th}
srv.HTTPServer = &http.Server{
Addr: fmt.Sprintf(":%v", listenPort),
Handler: ih,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
}
srv.ThrottlingHandler = th
srv.CORSHandler = ch
srv.RealIPHandler = ih
return srv
}