-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.go
More file actions
60 lines (52 loc) · 1.43 KB
/
router.go
File metadata and controls
60 lines (52 loc) · 1.43 KB
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
package routex
import (
"net/http"
)
type Router struct {
routes []*RouteConfig
staticRoutes []*StaticRouteConfig
}
// Create a new Router
func New() Router {
return Router{}
}
// Connect an URL-Path to a handler function.
func (router *Router) Handle(path string, handlerfn http.HandlerFunc) *RouteConfig {
rc := RouteConfig{
route: path,
handlerfn: handlerfn,
methods: httpMethods[:],
acceptTrailingSlash: true,
allowCors: false,
}
router.routes = append(router.routes, &rc)
return &rc
}
// Accept a trailing slash for all configured routes if client added one to the request even if you configured none in your path.
func (router *Router) AcceptTrailingSlash(b bool) {
for _, rt := range router.routes {
rt.acceptTrailingSlash = b
}
}
// Set allowed methods for all configured routes.
func (router *Router) Methods(methods ...string) {
for _, rt := range router.routes {
rt.methods = methods
}
}
// Set headers to allow CORS requests for all configured routes. Default is false.
func (router *Router) AllowCORS(b bool) {
for _, rt := range router.routes {
rt.allowCors = b
}
}
// Serve static files on the configured path
func (router *Router) ServeStatic(urlpath string, dirpath string) *StaticRouteConfig {
rc := StaticRouteConfig{
route: urlpath,
dirpath: dirpath,
indexFile: "",
}
router.staticRoutes = append(router.staticRoutes, &rc)
return &rc
}