/
routes.go
165 lines (142 loc) · 3.06 KB
/
routes.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2014 Justin Wilson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package routes
import (
"errors"
"fmt"
"net/http"
"path"
"strings"
)
type HandlerFunc func(ctx Context)
type Params func() map[string]string
type Context struct {
*http.Request
Response http.ResponseWriter
Params Params
}
type Route interface {
Path() string
IsCanonical(path string) (string, bool)
Matches(path string) bool
Execute(ctx Context)
}
type Routes struct {
col map[string][]Route
}
func (c *Context) Redirect(path string) {
http.Redirect(c.Response, c.Request, path, http.StatusFound)
}
func (c *Context) RedirectPerm(path string) {
r := c.Response
r.Header().Set("Location", path)
r.WriteHeader(http.StatusMovedPermanently)
}
func (c *Context) Error(status int) {
c.Response.WriteHeader(status)
}
func (s *Routes) Add(r Route, methods ...string) error {
for _, m := range methods {
c, ok := s.col[strings.ToUpper(m)]
if !ok {
return errors.New(fmt.Sprintf("Invalid HTTP method: %s", m))
}
s.col[m] = append(c, r)
}
return nil
}
func (s *Routes) Options(r Route) *Routes {
s.Add(r, "OPTIONS")
return s
}
func (s *Routes) Head(r Route) *Routes {
s.Add(r, "HEAD")
return s
}
func (s *Routes) Get(r Route) *Routes {
s.Add(r, "GET")
return s
}
func (s *Routes) Post(r Route) *Routes {
s.Add(r, "POST")
return s
}
func (s *Routes) Put(r Route) *Routes {
s.Add(r, "PUT")
return s
}
func (s *Routes) Delete(r Route) *Routes {
s.Add(r, "DELETE")
return s
}
func (s *Routes) Trace(r Route) *Routes {
s.Add(r, "TRACE")
return s
}
func (s *Routes) Connect(r Route) *Routes {
s.Add(r, "CONNECT")
return s
}
func (s *Routes) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r)
c, ok := s.col[r.Method]
if !ok {
ctx.Error(http.StatusNotFound)
return
}
for _, rt := range c {
if !rt.Matches(r.URL.Path) {
continue
}
if path, ok := rt.IsCanonical(r.URL.Path); !ok {
if r.URL.RawQuery != "" {
path = fmt.Sprintf("%s?%s", path, r.URL.RawQuery)
}
ctx.RedirectPerm(path)
} else {
//defer _500Handler(ctx)
rt.Execute(ctx)
}
return
}
ctx.Error(http.StatusNotFound)
}
func emptyParams() map[string]string {
return make(map[string]string)
}
func IsCanonical(p string) (string, bool) {
if len(p) == 0 {
return "/", true
} else if p[0] != '/' {
p = "/" + p
}
cp := path.Clean(p)
if cp[len(cp)-1] == '/' && cp != "/" {
cp = cp[:len(cp)-1]
return cp + "/", cp == p
}
return cp, cp == p
}
func Wrap(fn http.HandlerFunc) HandlerFunc {
return func(ctx Context) {
fn(ctx.Response, ctx.Request)
}
}
func New() *Routes {
r := new(Routes)
r.col = make(map[string][]Route)
r.col["ADD"] = []Route{}
r.col["OPTIONS"] = []Route{}
r.col["HEAD"] = []Route{}
r.col["GET"] = []Route{}
r.col["POST"] = []Route{}
r.col["PUT"] = []Route{}
r.col["DELETE"] = []Route{}
r.col["TRACE"] = []Route{}
r.col["CONNECT"] = []Route{}
return r
}
func NewContext(w http.ResponseWriter, r *http.Request) Context {
return Context{r, w, emptyParams}
}