-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
140 lines (124 loc) · 3.57 KB
/
http.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
138
139
140
package floridalist
import(
"log"
"context"
"net"
"net/http"
"strings"
"strconv"
"github.com/gorilla/mux"
)
type HttpController struct {
config Config
member *Member
}
func NewHttpController(config Config, member *Member) *HttpController {
c := new(HttpController)
c.config = config
c.member = member
return c
}
func (c *HttpController) HttpHandler() http.Handler {
r := mux.NewRouter()
r.StrictSlash(true)
r.HandleFunc(c.config.FloridaEndpoint, c.GetSeeds).Methods("GET")
r.HandleFunc("/_version", c.Version).Methods("GET")
r.HandleFunc("/_chk", c.CheckStatus).Methods("GET")
r.HandleFunc("/", c.CheckStatus).Methods("GET")
return r
}
func (c *HttpController) GetSeeds(res http.ResponseWriter, req *http.Request) {
if c.member.IsJoined() != true {
res.Header().Set("Content-Type", "text/plain")
res.WriteHeader(http.StatusNotAcceptable)
res.Write([]byte("cluster not joined"))
return
}
sl := c.member.SeedList()
v := strings.Join(sl, "|")
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(http.StatusOK)
res.Write([]byte(v))
}
func (c *HttpController) CheckStatus(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.WriteHeader(http.StatusOK)
v := strings.Join([]string{"OK", "\n"}, "")
res.Write([]byte(v))
}
func (c *HttpController) Version(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.WriteHeader(http.StatusOK)
v := strings.Join([]string{UA, "\n"}, "")
res.Write([]byte(v))
}
type WrapWriter struct {
Writer http.ResponseWriter
LastStatus int
}
func (w *WrapWriter) Header() http.Header {
return w.Writer.Header()
}
func (w *WrapWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w *WrapWriter) WriteHeader(status int) {
w.LastStatus = status
w.Writer.WriteHeader(status)
}
func WrapAccessLog(next http.Handler, logger HttpLogger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
wrap := new(WrapWriter)
wrap.Writer = w
next.ServeHTTP(wrap, r)
logger.Write(
r.Host,
r.Method,
r.RequestURI,
wrap.LastStatus,
r.Header.Get("User-Agent"),
)
})
}
type HttpServer struct {
config Config
Server *http.Server
Controller *HttpController
}
func NewHttpServer(ctx context.Context, m *Member) *HttpServer {
config := ctx.Value("config").(Config)
logger := ctx.Value("logger.http").(HttpLogger)
ctr := NewHttpController(config, m)
svr := new(HttpServer)
svr.config = config
svr.Controller = ctr
svr.Server = &http.Server {
Handler: WrapAccessLog(ctr.HttpHandler(), logger),
ReadTimeout: config.HttpReadTimeout,
WriteTimeout: config.HttpWriteTimeout,
}
return svr
}
func (s *HttpServer) Start(sctx context.Context) error {
config := s.config
listenAddr := net.JoinHostPort(config.FloridaBindIP, strconv.Itoa(config.FloridaBindPort))
log.Printf("info: florida starting %s", listenAddr)
listener, err := net.Listen("tcp4", listenAddr)
if err != nil {
log.Printf("error: addr '%s' listen error: %s", listenAddr, err.Error())
return err
}
if err := s.Server.Serve(listener); err != nil && err != http.ErrServerClosed {
log.Printf("error: serv error: %s", err.Error())
return err
}
return nil
}
func (s *HttpServer) Stop(sctx context.Context) error {
log.Printf("info: florida stoping")
if err := s.Server.Shutdown(sctx); err != nil {
log.Printf("error: shutdown error: %s", err.Error())
return err
}
return nil
}