forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (48 loc) · 1.55 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
package admin
import (
"crypto/tls"
"fmt"
"net/http"
"github.com/fabiolb/fabio/admin/api"
"github.com/fabiolb/fabio/admin/ui"
"github.com/fabiolb/fabio/config"
"github.com/fabiolb/fabio/proxy"
)
// Server provides the HTTP server for the admin UI and API.
type Server struct {
Access string
Color string
Title string
Version string
Commands string
Cfg *config.Config
}
// ListenAndServe starts the admin server.
func (s *Server) ListenAndServe(l config.Listen, tlscfg *tls.Config) error {
return proxy.ListenAndServeHTTP(l, s.handler(), tlscfg)
}
func (s *Server) handler() http.Handler {
mux := http.NewServeMux()
switch s.Access {
case "ro":
mux.HandleFunc("/api/manual", forbidden)
mux.HandleFunc("/manual", forbidden)
case "rw":
mux.Handle("/api/manual", &api.ManualHandler{})
mux.Handle("/manual", &ui.ManualHandler{Color: s.Color, Title: s.Title, Version: s.Version, Commands: s.Commands})
}
mux.Handle("/api/config", &api.ConfigHandler{Config: s.Cfg})
mux.Handle("/api/routes", &api.RoutesHandler{})
mux.Handle("/api/version", &api.VersionHandler{Version: s.Version})
mux.Handle("/routes", &ui.RoutesHandler{Color: s.Color, Title: s.Title, Version: s.Version})
mux.HandleFunc("/logo.svg", ui.HandleLogo)
mux.HandleFunc("/health", handleHealth)
mux.Handle("/", http.RedirectHandler("/routes", http.StatusSeeOther))
return mux
}
func handleHealth(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
func forbidden(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Forbidden", http.StatusForbidden)
}