-
Notifications
You must be signed in to change notification settings - Fork 0
/
Goose.go
78 lines (76 loc) · 1.88 KB
/
Goose.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
71
72
73
74
75
76
77
78
package goose
/**
* Sahil Gulati {[email protected]}
* Global settings for an HTTP service.
*/
type Goose struct {
routes map[string]*GooseRoute
regexRoutes map[string]*GooseRoute
listenAddress string
contextRoute *GooseRoute
holder interface{}
}
func (g Goose) GetInstance() *Goose {
goose := new(Goose)
goose.routes = make(map[string]*GooseRoute)
goose.regexRoutes = make(map[string]*GooseRoute)
return goose
}
func (g *Goose) WithHolding(holder interface{}) *Goose {
g.holder = holder
return g
}
func (g *Goose) AddCors() *Goose {
g.contextRoute.hasCors = true
return g
}
func (g *Goose) Route(methods []string, routePath string) *Goose {
gooseRoute := &GooseRoute{
uRL: routePath,
methods: methods,
}
g.contextRoute = gooseRoute
g.routes[routePath] = gooseRoute
return g
}
func (g *Goose) RegexRoute(methods []string, routePath string) *Goose {
gooseRoute := &GooseRoute{
uRL: routePath,
methods: methods,
dynamics: getDynamics(routePath),
hasDynamics: true,
uRLRegex: convertDyanmicURLToRegex(routePath),
}
g.contextRoute = gooseRoute
g.regexRoutes[routePath] = gooseRoute
return g
}
func (g *Goose) Middlewares(middlewares ...GooseMiddleware) *Goose {
g.checkContextRoute()
for _, middleware := range middlewares {
g.contextRoute.middlewares = append(g.contextRoute.middlewares, middleware)
}
return g
}
func (g *Goose) Endpoint(endpoint GooseEndpoint) *Goose {
g.checkContextRoute()
g.contextRoute.endpoint = endpoint
return g
}
func (g *Goose) Register() {
g.contextRoute = nil
}
func (g *Goose) Serve(address string) *Goose {
g.listenAddress = address
GooseHTTP{
routes: g.routes,
regexRoutes: g.regexRoutes,
holder: g.holder,
}.GetInstance().Register().Listen(address)
return g
}
func (g *Goose) checkContextRoute() {
if g.contextRoute == nil {
panic("Must register context route first!")
}
}