-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
66 lines (50 loc) · 1.01 KB
/
router.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
package web
import (
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
)
const (
StaticDir = "/static/"
)
type RouteMatch struct {
*mux.RouteMatch
}
type Router struct {
*mux.Router
}
type RouterPath interface {
Register(root *Router)
}
type AppRouter interface {
Setup() *Router
}
type appRouter struct {
routes []RouterPath
}
func NewRouter(routes []RouterPath) AppRouter {
r := &appRouter{
routes: routes,
}
return r
}
func (r *Router) Wildcart(domain string) *mux.Route {
return r.PathPrefix("/").Subrouter().MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
fmt.Printf("%+v\n", domain)
fmt.Printf("%+v\n", r.Host)
uri := r.RequestURI
if strings.HasPrefix(uri, "/socket") {
return false
}
return true
})
}
func (r *appRouter) Setup() *Router {
router := &Router{mux.NewRouter()}
router.PathPrefix(StaticDir).Handler(http.StripPrefix(StaticDir, http.FileServer(http.Dir("./website/"+StaticDir))))
for _, a := range r.routes {
a.Register(router)
}
return router
}