-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp.go
125 lines (96 loc) · 2.58 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
package api
import (
"net"
"net/http"
"github.com/NYTimes/gziphandler"
wh "github.com/SkycoinProject/skycoin/src/util/http"
"github.com/SkycoinProject/skycoin/src/util/logging"
)
const (
// ContentTypeJSON json content type header
ContentTypeJSON = "application/json"
// ContentTypeForm form data content type header
ContentTypeForm = "application/x-www-form-urlencoded"
apiVersion1 = "v1"
)
var (
logger = logging.MustGetLogger("multicoin-api")
)
type muxConfig struct {
host string
}
// HTTPError is included in an HTTPResponse
type HTTPError struct {
Message string `json:"message"`
Code int `json:"code"`
}
// Server exposes an HTTP API
type Server struct {
server *http.Server
listener net.Listener
done chan struct{}
}
// Serve serves the web interface on the configured host
func (s *Server) Serve() error {
defer close(s.done)
if err := s.server.Serve(s.listener); err != nil {
if err != http.ErrServerClosed {
return err
}
}
return nil
}
// Shutdown closes the HTTP service. This can only be called after Serve or ServeHTTPS has been called.
func (s *Server) Shutdown() {
if s == nil {
return
}
logger.Info("Shutting down web interface")
defer logger.Info("Web interface shut down")
if err := s.listener.Close(); err != nil {
logger.WithError(err).Warning("s.listener.Close() error")
}
<-s.done
}
func create(host string, gateway *Gateway) *Server {
mc := muxConfig{
host: host,
}
srvMux := newServerMux(mc, gateway)
srv := &http.Server{
Handler: srvMux,
}
return &Server{
server: srv,
done: make(chan struct{}),
}
}
// Create create a new http server
func Create(host string, gateway *Gateway) (*Server, error) {
listener, err := net.Listen("tcp", host)
if err != nil {
return nil, err
}
// If the host did not specify a port, allowing the kernel to assign one,
// we need to get the assigned address to know the full hostname
host = listener.Addr().String()
s := create(host, gateway)
s.listener = listener
return s, nil
}
func newServerMux(c muxConfig, gateway Gatewayer) *http.ServeMux {
mux := http.NewServeMux()
webHandlerWithOptionals := func(endpoint string, handlerFunc http.Handler) {
handler := wh.ElapsedHandler(logger, handlerFunc)
handler = gziphandler.GzipHandler(handler)
mux.Handle(endpoint, handler)
}
webHandler := func(endpoint string, handler http.Handler) {
webHandlerWithOptionals(endpoint, handler)
}
webHandlerV1 := func(endpoint string, handler http.Handler) {
webHandler("/api/"+apiVersion1+endpoint, handler)
}
gateway.SetupCoinRoutes("/multicoin", webHandlerV1)
return mux
}