-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
50 lines (37 loc) · 834 Bytes
/
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
package dirty
import (
"net/http"
"github.com/go-martini/martini"
)
type Router interface {
martini.Router
}
type router struct {
martini.Router
}
func NewRouter() Router {
return &router{martini.NewRouter()}
}
type Helper interface {
URLFor(name string, params ...interface{}) string
}
type helper struct {
Routes martini.Routes
BaseUrl string
}
func (h *helper) URLFor(name string, params ...interface{}) string {
return h.BaseUrl + h.Routes.URLFor(name, params...)
}
func HelperMiddleware() func(c martini.Context, routes martini.Routes, req *http.Request) {
return func(c martini.Context, routes martini.Routes, req *http.Request) {
baseUrl := "http://" + req.Host
if req.URL.IsAbs() {
baseUrl = req.URL.Scheme + req.URL.Host
}
helper := &helper{
routes,
baseUrl,
}
c.Map(helper)
}
}