forked from topfreegames/fluxcloud
/
api.go
41 lines (36 loc) · 879 Bytes
/
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
package apis
import (
"github.com/justinbarrick/fluxcloud/pkg/config"
"github.com/justinbarrick/fluxcloud/pkg/exporters"
"github.com/justinbarrick/fluxcloud/pkg/formatters"
"net/http"
"time"
)
// All of the configuration necessary to run a fluxcloud API
type APIConfig struct {
Server *http.ServeMux
Client *http.Client
Exporter exporters.Exporter
Formatter formatters.Formatter
Config config.Config
}
// Initialize API configuration
func NewAPIConfig(f formatters.Formatter, e exporters.Exporter, c config.Config) APIConfig {
return APIConfig{
Server: http.NewServeMux(),
Client: &http.Client{
Timeout: 120 * time.Second,
},
Formatter: f,
Exporter: e,
Config: c,
}
}
// Listen on addr
func (a *APIConfig) Listen(addr string) error {
server := http.Server{
Addr: addr,
Handler: a.Server,
}
return server.ListenAndServe()
}