/
http.go
54 lines (44 loc) · 1.06 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
package main
import (
"context"
"log"
"net/http"
)
type StatusCaptureResponseWriter struct {
http.ResponseWriter
status int
}
func (w *StatusCaptureResponseWriter) WriteHeader(code int) {
w.ResponseWriter.WriteHeader(code)
w.status = code
}
type AccessLogHandler struct {
http.Handler
}
func (s *AccessLogHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
capw := &StatusCaptureResponseWriter{
ResponseWriter: w,
status: http.StatusOK,
}
s.Handler.ServeHTTP(capw, req)
log.Printf("[INFO] %s %s - %d - %s", req.Method, req.URL.Path, capw.status, req.RemoteAddr)
}
type HTTPServer struct {
HTTPDir string
srv *http.Server
}
func (s *HTTPServer) Start(listen string) error {
s.srv = &http.Server{
Addr: listen,
Handler: &AccessLogHandler{http.FileServer(http.Dir(s.HTTPDir))},
}
log.Printf("[INFO] Starting HTTP server on %s ...", listen)
err := s.srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
func (s *HTTPServer) Shutdown() error {
return s.srv.Shutdown(context.TODO())
}