Add support for multi http methods route registration at the .HandleMany

Former-commit-id: 6aa2c3027d64dac31a1cf84d782a3c63ad7b2fb0
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-10-02 04:01:23 +03:00
parent 4ff0571785
commit e066771207

View File

@ -188,20 +188,28 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
// //
// This method is used behind the scenes at the `Controller` function // This method is used behind the scenes at the `Controller` function
// in order to handle more than one paths for the same controller instance. // in order to handle more than one paths for the same controller instance.
func (api *APIBuilder) HandleMany(method string, relativePath string, handlers ...context.Handler) (routes []*Route) { func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti string, handlers ...context.Handler) (routes []*Route) {
trimmedPath := strings.Trim(relativePath, " ") trimmedPath := strings.Trim(relativePathorMulti, " ")
trimmedMethod := strings.Trim(methodOrMulti, " ")
// at least slash // at least slash
// a space // a space
// at least one other slash for the next path // at least one other slash for the next path
// app.Controller("/user /user{id}", new(UserController)) // app.Controller("/user /user{id}", new(UserController))
paths := strings.Split(trimmedPath, " ") paths := strings.Split(trimmedPath, " ")
methods := strings.Split(trimmedMethod, " ")
for _, p := range paths { for _, p := range paths {
if p != "" { if p != "" {
if method == "ANY" || method == "ALL" { for _, method := range methods {
routes = append(routes, api.Any(p, handlers...)...) if method == "" {
continue method = "ANY"
}
if method == "ANY" || method == "ALL" {
routes = append(routes, api.Any(p, handlers...)...)
continue
}
routes = append(routes, api.Handle(method, p, handlers...))
} }
routes = append(routes, api.Handle(method, p, handlers...))
} }
} }
return return
@ -699,18 +707,10 @@ func (api *APIBuilder) Favicon(favPath string, requestPath ...string) *Route {
return nil return nil
} }
// ignore error f.Close()
defer f.Close() defer f.Close()
fi, _ := f.Stat() fi, _ := f.Stat()
if fi.IsDir() { // if it's dir the try to get the favicon.ico if fi.IsDir() { // if it's dir the try to get the favicon.ico
fav := path.Join(favPath, "favicon.ico") return api.Favicon(path.Join(favPath, "favicon.ico"))
f, err = os.Open(fav)
if err != nil {
//we try again with .png
return api.Favicon(path.Join(favPath, "favicon.png"))
}
favPath = fav
fi, _ = f.Stat()
} }
cType := TypeByFilename(favPath) cType := TypeByFilename(favPath)
@ -747,7 +747,7 @@ func (api *APIBuilder) Favicon(favPath string, requestPath ...string) *Route {
} }
} }
reqPath := "/favicon" + path.Ext(fi.Name()) //we could use the filename, but because standards is /favicon.ico/.png. reqPath := "/favicon" + path.Ext(fi.Name()) // we could use the filename, but because standards is /favicon.ico
if len(requestPath) > 0 && requestPath[0] != "" { if len(requestPath) > 0 && requestPath[0] != "" {
reqPath = requestPath[0] reqPath = requestPath[0]
} }