/
api.go
68 lines (54 loc) · 1.36 KB
/
api.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
package api
import (
"net/http"
"github.com/ian-kent/gofigure"
"github.com/ian-kent/service.go/log"
"github.com/mailhog/mh2/server"
"github.com/mailhog/mh2/server/api/backend"
"github.com/mailhog/mh2/server/api/handlers"
mh2http "github.com/mailhog/mh2/server/http"
)
type apiServer struct {
config Config
httpServer mh2http.Server
backend backend.API
exit bool
}
// NewServer returns a new server
func NewServer() (server.Server, error) {
var apiConfig = Config{
BindAddr: "0.0.0.0:8025",
Backend: "mongodb",
}
if err := gofigure.Gofigure(&apiConfig); err != nil {
return nil, err
}
be, err := backend.New(apiConfig.Backend)
if err != nil {
return nil, err
}
api := &apiServer{
config: apiConfig,
httpServer: mh2http.Get(apiConfig.BindAddr),
backend: be,
}
api.httpServer.Router().Get("/api/healthcheck", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(200)
})
api.httpServer.Router().Get("/messages", handlers.List{API: api.backend}.ServeHTTP)
return api, nil
}
// Start starts the server
func (s *apiServer) Start() error {
log.Debug("api: starting server", log.Data{
"bind_addr": s.config.BindAddr,
})
return s.httpServer.Start()
}
// Stop stops the server
func (s *apiServer) Stop() error {
log.Debug("api: stopping server", nil)
s.exit = true
s.backend.Close()
return s.httpServer.Stop()
}