-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.go
62 lines (57 loc) · 1.36 KB
/
HTTP.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
package goose
import (
"fmt"
"net/http"
)
/**
* Sahil Gulati {[email protected]}
* This structure will be responsible for registering routes with HTTP handlers.
*/
type GooseHTTP struct {
routes map[string]*GooseRoute
regexRoutes map[string]*GooseRoute
holder interface{}
}
func (gHTTP GooseHTTP) GetInstance() *GooseHTTP {
gh := new(GooseHTTP)
gh.routes = gHTTP.routes
gh.holder = gHTTP.holder
gh.regexRoutes = gHTTP.regexRoutes
return gh
}
/**
* This function will register all GooseRoutes to net/http in which
* each request will be first forwarded to GooseHTTPHandler.Pipeline
*/
func (gHTTP *GooseHTTP) Register() *GooseHTTP {
for routeURL, gooseRoute := range gHTTP.routes {
http.HandleFunc(
routeURL,
GooseGateway{
route: gooseRoute,
holder: gHTTP.holder,
}.Pipeline,
)
}
gHTTP.DefaultRoute()
return gHTTP
}
/**
* This function will register all Regex GooseRoutes to net/http in which
* each request will be first forwarded to GooseHTTPHandler.RegexPipeline
*/
func (gHTTP *GooseHTTP) DefaultRoute() *GooseHTTP {
route := "/"
http.HandleFunc(
route,
GooseGateway{
regexRoutes: gHTTP.regexRoutes,
holder: gHTTP.holder,
}.RegexPipeline,
)
return gHTTP
}
func (gHTTP *GooseHTTP) Listen(address string) {
fmt.Println(fmt.Sprintf("Listening at %s.", address))
http.ListenAndServe(address, nil)
}