-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
56 lines (38 loc) · 1.21 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
53
54
55
56
package router
import (
"strings"
"strconv"
//
"github.com/valyala/fasthttp"
"github.com/golangdaddy/tarantula/log"
//
"github.com/golangdaddy/tarantula/router/common"
"github.com/golangdaddy/tarantula/router/common/openapi"
)
type FastHttpRouter func (ctx *fasthttp.RequestCtx)
func (router FastHttpRouter) Serve(port int) error {
return fasthttp.ListenAndServe(":"+strconv.Itoa(port), fasthttp.RequestHandler(router))
}
func (router FastHttpRouter) ServeTLS(port int, crt, key string) error {
return fasthttp.ListenAndServeTLS(":"+strconv.Itoa(port), crt, key, fasthttp.RequestHandler(router))
}
func NewRouter(logger logging.Logger, spec *openapi.APISpec) (*common.Node, FastHttpRouter) {
root := common.Root()
root.Config.Spec = spec
root.Config.Log = logger
return root, FastHttpRouter(
func (ctx *fasthttp.RequestCtx) {
fullPath := string(ctx.Path())
node := common.Root()
req := NewRequestObject(node, ctx)
// check for subdomain routing
subdomain := strings.Split(string(ctx.Host()), ".")[0]
subNode := node.Config.SubdomainTrees[subdomain]
if subNode != nil {
subNode.MainHandler(req, fullPath)
return
}
node.MainHandler(req, fullPath)
},
)
}