From d07a939b5ef5d0f4e72f0f23e2314a5e934b42a0 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sun, 28 Apr 2013 15:07:53 -0700 Subject: 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. --- router.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'router.go') 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{} -- cgit 1.4.1