about summary refs log tree commit diff
path: root/router.go
diff options
context:
space:
mode:
authorFranck Cuny <franck@lumberjaph.net>2013-04-28 15:07:53 -0700
committerFranck Cuny <franck@lumberjaph.net>2013-04-28 15:07:53 -0700
commitd07a939b5ef5d0f4e72f0f23e2314a5e934b42a0 (patch)
tree857bd3ce2a1b84a8391a4965437fb9ae4ff392e4 /router.go
parent`BuildRouter` return a reference to itself. (diff)
downloadpath-router-d07a939b5ef5d0f4e72f0f23e2314a5e934b42a0.tar.gz
By calling AddOptions on the router, a route for OPTIONS is added
The AddOptions method on the Router creates a route to respond to the
OPTIONS request, and list all the supported HTTP methods for the given route.
Diffstat (limited to 'router.go')
-rw-r--r--router.go16
1 files changed, 16 insertions, 0 deletions
diff --git a/router.go b/router.go
index d4c9058..23b8214 100644
--- a/router.go
+++ b/router.go
@@ -19,6 +19,22 @@ func BuildRouter() *Router {
 	return &router
 }
 
+func (self *Router) AddOptions() *Router {
+	self.notAllowed = true
+	routes := self.GetRouteList()
+	for _, r := range routes {
+		m := self.GetMethodsForPath(r)
+		allowed := strings.Join(m, ", ")
+		defaultFn := func(req *Request) (Response, error) {
+			r := Response{Status: 204}
+			r.AddHeader("Allow", allowed)
+			return r, nil
+		}
+		self.AddRoute(&Route{Method: "OPTIONS", Path: r, Code: defaultFn})
+	}
+	return self
+}
+
 func (self *Router) routeIsKnown(route *Route) bool {
 	if self.knownPaths[route.Path] == nil {
 		self.knownPaths[route.Path] = map[string]bool{}