2018-05-20 08:46:39 +00:00
|
|
|
package config
|
2018-05-19 13:17:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2018-05-24 14:18:28 +00:00
|
|
|
"strings"
|
2018-05-19 13:17:21 +00:00
|
|
|
)
|
|
|
|
|
2018-05-20 08:46:39 +00:00
|
|
|
// Load builds a structured representation of the
|
2018-05-19 13:17:21 +00:00
|
|
|
// configuration file located at @path
|
2018-05-20 08:40:04 +00:00
|
|
|
// The struct definition checks for most format errors
|
2018-05-20 08:46:39 +00:00
|
|
|
func Load(path string) (*Controller, error) {
|
2018-05-19 13:17:21 +00:00
|
|
|
|
|
|
|
/* (1) Extract data
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Open file */
|
|
|
|
var configFile, err = os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
/* (2) Init receiving dataset */
|
2018-05-20 08:46:39 +00:00
|
|
|
receiver := &Controller{}
|
2018-05-19 13:17:21 +00:00
|
|
|
|
|
|
|
/* (3) Decode JSON */
|
|
|
|
decoder := json.NewDecoder(configFile)
|
|
|
|
err = decoder.Decode(receiver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-20 10:21:27 +00:00
|
|
|
/* (4) Format result */
|
|
|
|
err = receiver.format("/")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-19 13:17:21 +00:00
|
|
|
return receiver, nil
|
2018-05-20 08:46:39 +00:00
|
|
|
|
2018-05-19 13:17:21 +00:00
|
|
|
}
|
2018-05-24 14:18:28 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-05-24 14:22:29 +00:00
|
|
|
// Method returns whether the controller has a given
|
|
|
|
// method by name (case insensitive)
|
|
|
|
// NIL is returned if no method is found
|
|
|
|
func (c Controller) Method(method string) *Method {
|
2018-05-24 14:18:28 +00:00
|
|
|
method = strings.ToUpper(method)
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
|
|
|
|
case "GET":
|
2018-05-24 14:22:29 +00:00
|
|
|
return c.GET
|
2018-05-24 14:18:28 +00:00
|
|
|
case "POST":
|
2018-05-24 14:22:29 +00:00
|
|
|
return c.POST
|
2018-05-24 14:18:28 +00:00
|
|
|
case "PUT":
|
2018-05-24 14:22:29 +00:00
|
|
|
return c.PUT
|
2018-05-24 14:18:28 +00:00
|
|
|
case "DELETE":
|
2018-05-24 14:22:29 +00:00
|
|
|
return c.DELETE
|
2018-05-24 14:18:28 +00:00
|
|
|
default:
|
2018-05-24 14:22:29 +00:00
|
|
|
return nil
|
2018-05-24 14:18:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|