/
hyperdrive.go
67 lines (59 loc) · 1.89 KB
/
hyperdrive.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
package hyperdrive
import (
"log"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
slugify "github.com/metal3d/go-slugify"
)
var (
hAPI API
)
// API is a logical collection of one or more endpoints, connecting requests
// to the response handlers using a gorlla mux Router.
type API struct {
Name string
Desc string
Router *mux.Router
Server *http.Server
Root *RootResource
endpoints []Endpoint
}
// NewAPI creates an instance of API, with an initialized Router, Config, Server, and RootResource.
func NewAPI(name string, desc string) API {
api := API{
Name: name,
Desc: desc,
Router: mux.NewRouter(),
}
api.Root = NewRootResource(api)
api.Router.Handle("/", api.DefaultMiddlewareChain(api.Root)).Methods("GET")
api.Server = &http.Server{
Handler: api.Router,
Addr: conf.GetPort(),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
hAPI = api
return api
}
// AddEndpoint registers endpoints, ensuring that endpoints automatically
// respond with a 405 error if the endpoint does not support a particular
// HTTP method.
func (api *API) AddEndpoint(e Endpointer) {
api.Root.AddEndpoint(e)
api.Router.Handle(e.GetPath(), api.DefaultMiddlewareChain(NewMethodHandler(e))).HeadersRegexp("Accept", GetMediaType(*api, e)+"(json|xml)")
log.Printf("Added hyperdriven Endpoint: %s http://0.0.0.0:%d%s", e.GetName(), conf.Port, e.GetPath())
log.Printf(" Methods: %s", GetMethodsList(e))
log.Printf(" Media Types: %s", GetContentTypesList(*api, e))
}
// Start starts the configured http server, listening on the configured Port
// (default: 5000). Set the PORT environment variable to change this.
func (api *API) Start() {
log.Printf("Starting hyperdriven API (%s): %s http://0.0.0.0:%d", conf.Env, api.Name, conf.Port)
log.Fatal(api.Server.ListenAndServe())
}
func slug(s string) string {
return strings.ToLower(slugify.Marshal(s))
}