-
Notifications
You must be signed in to change notification settings - Fork 4
/
mux.go
43 lines (35 loc) · 964 Bytes
/
mux.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
// Based on sleepy https://github.com/dougblack/sleepy
package goal
import "github.com/gorilla/mux"
// An API manages a group of resources by routing requests
// to the correct method on a matching resource and marshalling
// the returned data to JSON for the HTTP response.
//
// You can instantiate multiple APIs on separate ports. Each API
// will manage its own set of resources.
type API struct {
mux *mux.Router
muxInitialized bool
}
var sharedAPI *API
// NewAPI allocates and returns a new API.
func NewAPI() *API {
if sharedAPI == nil {
sharedAPI = &API{}
}
return sharedAPI
}
// SharedAPI return API instance
func SharedAPI() *API {
return sharedAPI
}
// Mux returns Gorilla's mux.Router used by an API. If a mux
// does not yet exist, a new one will be created and returned
func (api *API) Mux() *mux.Router {
if api.muxInitialized {
return api.mux
}
api.mux = mux.NewRouter()
api.muxInitialized = true
return api.mux
}