-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
70 lines (57 loc) · 1.74 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
69
70
package api
import (
"net/http"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/torlenor/alolstats/config"
"github.com/torlenor/alolstats/logging"
)
// API represents a Rest API instance of a ALoLStats instance
type API struct {
config config.API
router *mux.Router
log *logrus.Entry
prefix string
}
// NewAPI creates a new Rest API instance
func NewAPI(cfg config.API) (*API, error) {
a := &API{
config: cfg,
router: mux.NewRouter(),
log: logging.Get("RestAPI"),
prefix: "/v1",
}
return a, nil
}
// AttachModuleGet registers a new GET handler for the API
func (a *API) AttachModuleGet(path string, f func(http.ResponseWriter, *http.Request)) {
a.log.Infoln("Registering GET handler:", a.prefix+path)
a.router.HandleFunc(a.prefix+path, f).Methods("GET")
}
// AttachModulePost registers a new POST handler for the API
func (a *API) AttachModulePost(path string, f func(http.ResponseWriter, *http.Request)) {
a.log.Infoln("Registering POST handler:", a.prefix+path)
a.router.HandleFunc(a.prefix+path, f).Methods("POST")
}
func (a *API) run(listenAddress string) {
a.log.Fatal(http.ListenAndServe(listenAddress, handlers.CORS()(a.router)))
}
// Start the REST API
func (a *API) Start() {
var listenAddress string
if len(a.config.IP) > 0 && len(a.config.Port) > 0 {
listenAddress = a.config.IP + ":" + a.config.Port
} else if len(a.config.Port) > 0 {
listenAddress = ":" + a.config.Port
} else {
a.log.Fatal("REST API activated but no valid configuration found. At least port has to specified in config file!")
}
a.log.Infof("Starting REST API on %s", listenAddress)
go a.run(listenAddress)
}
// Stop the REST API
func (a *API) Stop() {
a.log.Println("Stopping REST API")
// TODO
}