-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
52 lines (33 loc) · 1.12 KB
/
init.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
package router
import (
"regexp"
"strings"
"net/http"
//
"github.com/golangdaddy/tarantula/router/common"
"github.com/golangdaddy/tarantula/router/common/openapi"
)
type WildcardRouter struct {
handler http.Handler
}
func (router *WildcardRouter) Handler(pattern *regexp.Regexp, handler http.Handler) {}
func (router *WildcardRouter) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {}
func (router *WildcardRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { router.handler.ServeHTTP(w, r) }
// create a new router for an app
func NewRouter(spec *openapi.APISpec) (*common.Node, *WildcardRouter) {
root := common.Root()
root.Config.Spec = spec
f := func (res http.ResponseWriter, r *http.Request) {
node := common.Root()
req := NewRequestObject(node, res, r)
// check for subdomain routing
subdomain := strings.Split(r.URL.Host, ".")[0]
subNode := node.Config.SubdomainTrees[subdomain]
if subNode != nil {
subNode.MainHandler(req, r.URL.Path)
return
}
node.MainHandler(req, r.URL.Path)
}
return root, &WildcardRouter{http.HandlerFunc(f)}
}