implemented method check in 'config' package

This commit is contained in:
Adrien Marquès 2018-05-24 16:18:28 +02:00
parent 296234d6e7
commit 8106c22dbd
2 changed files with 37 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"os"
"strings"
)
// Load builds a structured representation of the
@ -38,3 +39,37 @@ func Load(path string) (*Controller, error) {
return receiver, nil
}
// IsMethodAvailable returns whether a given
// method is available (case insensitive)
func IsMethodAvailable(method string) bool {
for _, m := range AvailableMethods {
if strings.ToUpper(method) == m {
return true
}
}
return false
}
// HasMethod returns whether the controller has a given
// method (case insensitive)
func (c Controller) HasMethod(method string) bool {
method = strings.ToUpper(method)
switch method {
case "GET":
return c.GET != nil
case "POST":
return c.POST != nil
case "PUT":
return c.PUT != nil
case "DELETE":
return c.DELETE != nil
default:
return false
}
}

View File

@ -25,3 +25,5 @@ type Controller struct {
Children map[string]*Controller `json:"/"`
}
var AvailableMethods = []string{"GET", "POST", "PUT", "DELETE"}