2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-30 22:02:28 +00:00
|
|
|
"log"
|
2019-09-26 17:03:37 +00:00
|
|
|
"net/http"
|
2019-04-30 22:02:28 +00:00
|
|
|
|
2018-10-07 09:40:35 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2019-05-01 11:44:45 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
|
|
|
"git.xdrm.io/go/aicra/internal/reqdata"
|
2018-07-06 08:49:52 +00:00
|
|
|
)
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// extractParameters extracts parameters for the request and checks
|
|
|
|
// every single one according to configuration options
|
2019-09-26 17:03:37 +00:00
|
|
|
func (s *httpServer) extractParameters(store *reqdata.Store, methodParam map[string]*config.Parameter) (map[string]interface{}, api.Error) {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// init vars
|
|
|
|
parameters := make(map[string]interface{})
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// for each param of the config
|
|
|
|
for name, param := range methodParam {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 1. extract value
|
2019-05-01 11:44:45 +00:00
|
|
|
p, isset := store.Set[name]
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 2. fail if required & missing
|
2019-05-01 11:44:45 +00:00
|
|
|
if !isset && !param.Optional {
|
2019-05-01 16:15:44 +00:00
|
|
|
apiErr := api.ErrorMissingParam()
|
|
|
|
apiErr.SetArguments(name)
|
|
|
|
return nil, apiErr
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 3. optional & missing: set default value
|
2019-05-01 11:44:45 +00:00
|
|
|
if !isset {
|
|
|
|
p = &reqdata.Parameter{
|
|
|
|
Parsed: true,
|
|
|
|
File: param.Type == "FILE",
|
|
|
|
Value: nil,
|
|
|
|
}
|
|
|
|
if param.Default != nil {
|
|
|
|
p.Value = *param.Default
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are done
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 4. parse parameter if not file
|
2019-05-01 11:44:45 +00:00
|
|
|
if !p.File {
|
|
|
|
p.Parse()
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 5. fail on unexpected multipart file
|
2019-05-01 11:44:45 +00:00
|
|
|
waitFile, gotFile := param.Type == "FILE", p.File
|
|
|
|
if gotFile && !waitFile || !gotFile && waitFile {
|
2019-05-01 16:15:44 +00:00
|
|
|
apiErr := api.ErrorInvalidParam()
|
|
|
|
apiErr.SetArguments(param.Rename, "FILE")
|
|
|
|
return nil, apiErr
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 6. ignore type check if file
|
2019-05-01 11:44:45 +00:00
|
|
|
if gotFile {
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 7. check type
|
2019-05-01 13:14:49 +00:00
|
|
|
if s.Checkers.Run(param.Type, p.Value) != nil {
|
2019-05-01 16:15:44 +00:00
|
|
|
apiErr := api.ErrorInvalidParam()
|
|
|
|
apiErr.SetArguments(param.Rename, param.Type, p.Value)
|
|
|
|
return nil, apiErr
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parameters[param.Rename] = p.Value
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:05:58 +00:00
|
|
|
return parameters, api.ErrorSuccess()
|
2019-05-01 11:44:45 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 17:03:37 +00:00
|
|
|
var handledMethods = []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete}
|
|
|
|
|
2018-07-06 08:49:52 +00:00
|
|
|
// Prints an error as HTTP response
|
2019-05-01 16:01:32 +00:00
|
|
|
func logError(res *api.Response) {
|
2019-05-01 16:23:57 +00:00
|
|
|
log.Printf("[http.fail] %v\n", res)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
2019-09-26 17:03:37 +00:00
|
|
|
|
|
|
|
// logService logs a service details
|
|
|
|
func logService(s config.Service, path string) {
|
|
|
|
for _, method := range handledMethods {
|
|
|
|
if m := s.Method(method); m != nil {
|
|
|
|
if path == "" {
|
2019-11-19 18:19:55 +00:00
|
|
|
log.Printf(" ->\t%s\t'/'\n", method)
|
2019-09-26 17:03:37 +00:00
|
|
|
} else {
|
2019-11-19 18:19:55 +00:00
|
|
|
log.Printf(" ->\t%s\t'%s'\n", method, path)
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Children != nil {
|
|
|
|
for subPath, child := range s.Children {
|
|
|
|
logService(*child, path+"/"+subPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|