2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-07 17:21:00 +00:00
|
|
|
e "git.xdrm.io/go/aicra/err"
|
2018-10-01 12:15:00 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/api"
|
2018-07-08 22:08:53 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/checker"
|
2018-10-01 15:43:18 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
2018-09-28 06:10:41 +00:00
|
|
|
apirequest "git.xdrm.io/go/aicra/internal/request"
|
2018-07-07 20:10:56 +00:00
|
|
|
"git.xdrm.io/go/aicra/middleware"
|
2018-10-01 17:27:38 +00:00
|
|
|
"git.xdrm.io/go/aicra/response"
|
2018-07-06 08:49:52 +00:00
|
|
|
"log"
|
2018-05-21 10:02:24 +00:00
|
|
|
"net/http"
|
2018-10-01 17:27:38 +00:00
|
|
|
"strings"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2018-07-10 23:36:42 +00:00
|
|
|
// Server represents an AICRA instance featuring:
|
|
|
|
// * its type checkers
|
|
|
|
// * its middlewares
|
2018-10-01 12:15:00 +00:00
|
|
|
// * its controllers (api config)
|
2018-07-10 23:36:42 +00:00
|
|
|
type Server struct {
|
2018-10-01 17:27:38 +00:00
|
|
|
controller *api.Controller // controllers
|
|
|
|
checker checker.Registry // type checker registry
|
|
|
|
middleware middleware.Registry // middlewares
|
2018-10-01 15:43:18 +00:00
|
|
|
schema *config.Schema
|
2018-07-10 23:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 22:32:19 +00:00
|
|
|
// New creates a framework instance from a configuration file
|
2018-09-28 13:58:30 +00:00
|
|
|
// _path is the json configuration path
|
|
|
|
// _driver is used to load/run the controllers and middlewares (default: )
|
|
|
|
//
|
2018-10-01 13:14:48 +00:00
|
|
|
func New(_path string) (*Server, error) {
|
2018-09-27 11:43:36 +00:00
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
/* 1. Load config */
|
2018-10-01 15:43:18 +00:00
|
|
|
schema, err := config.Parse("./aicra.json")
|
2018-10-01 13:14:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-28 13:58:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 2. Init instance */
|
2018-07-10 23:51:10 +00:00
|
|
|
var i = &Server{
|
|
|
|
controller: nil,
|
2018-10-01 13:14:48 +00:00
|
|
|
schema: schema,
|
2018-07-10 23:51:10 +00:00
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 3. Load configuration */
|
2018-10-01 12:15:00 +00:00
|
|
|
i.controller, err = api.Parse(_path)
|
2018-07-06 08:49:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 4. Load type registry */
|
|
|
|
i.checker = checker.CreateRegistry()
|
|
|
|
|
|
|
|
// add default types if set
|
|
|
|
|
|
|
|
// add custom types
|
|
|
|
for name, path := range schema.Types.Map {
|
|
|
|
|
|
|
|
fullpath := schema.Driver.Build(schema.Root, schema.Types.Folder, path)
|
|
|
|
mwFunc, err := schema.Driver.LoadChecker(fullpath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cannot load type checker '%s' | %s", name, err)
|
|
|
|
}
|
|
|
|
i.checker.Add(path, mwFunc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. Load middleware registry */
|
|
|
|
i.middleware = middleware.CreateRegistry()
|
|
|
|
for name, path := range schema.Middlewares.Map {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
fullpath := schema.Driver.Build(schema.Root, schema.Middlewares.Folder, path)
|
|
|
|
mwFunc, err := schema.Driver.LoadMiddleware(fullpath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("cannot load middleware '%s' | %s", name, err)
|
|
|
|
}
|
|
|
|
i.middleware.Add(path, mwFunc)
|
|
|
|
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2018-07-10 23:36:42 +00:00
|
|
|
return i, nil
|
2018-07-07 20:10:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:02:33 +00:00
|
|
|
// ServeHTTP implements http.Handler and has to be called on each request
|
|
|
|
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (1) Build request */
|
2018-07-11 17:02:33 +00:00
|
|
|
apiRequest, err := apirequest.FromHTTP(req)
|
2018-07-07 17:21:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 23:51:10 +00:00
|
|
|
/* (2) Launch middlewares to build the scope */
|
|
|
|
scope := s.middleware.Run(*req)
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (3) Find a matching controller */
|
2018-07-10 23:51:10 +00:00
|
|
|
controller := s.matchController(apiRequest)
|
2018-07-07 20:10:56 +00:00
|
|
|
if controller == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (4) Check if matching method exists */
|
2018-07-10 23:51:10 +00:00
|
|
|
var method = controller.Method(req.Method)
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
if method == nil {
|
2018-07-07 17:21:00 +00:00
|
|
|
httpError(res, e.UnknownMethod)
|
2018-07-06 08:49:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (5) Check scope permissions */
|
|
|
|
if !method.CheckScope(scope) {
|
|
|
|
httpError(res, e.Permission)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-06 08:49:52 +00:00
|
|
|
/* (4) Check parameters
|
|
|
|
---------------------------------------------------------*/
|
2018-07-10 23:51:10 +00:00
|
|
|
parameters, paramError := s.extractParameters(apiRequest, method.Parameters)
|
2018-07-07 17:21:00 +00:00
|
|
|
|
|
|
|
// Fail if argument check failed
|
|
|
|
if paramError.Code != e.Success.Code {
|
|
|
|
httpError(res, paramError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Load controller
|
|
|
|
---------------------------------------------------------*/
|
2018-10-01 17:27:38 +00:00
|
|
|
// get paths
|
|
|
|
ctlBuildPath := strings.Join(apiRequest.Path, "/")
|
|
|
|
ctlBuildPath = s.schema.Driver.Build(s.schema.Root, s.schema.Controllers.Folder, ctlBuildPath)
|
|
|
|
|
|
|
|
// get controller
|
|
|
|
ctlObject, err := s.schema.Driver.LoadController(ctlBuildPath)
|
|
|
|
httpMethod := strings.ToUpper(req.Method)
|
|
|
|
if err != nil {
|
|
|
|
httpErr := e.UncallableController
|
|
|
|
httpErr.BindArgument(err)
|
|
|
|
httpError(res, httpErr)
|
|
|
|
log.Printf("err( %s )\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ctlMethod func(response.Arguments) response.Response
|
|
|
|
// select method
|
|
|
|
switch httpMethod {
|
|
|
|
case "GET":
|
|
|
|
ctlMethod = ctlObject.Get
|
|
|
|
case "POST":
|
|
|
|
ctlMethod = ctlObject.Post
|
|
|
|
case "PUT":
|
|
|
|
ctlMethod = ctlObject.Put
|
|
|
|
case "DELETE":
|
|
|
|
ctlMethod = ctlObject.Delete
|
|
|
|
default:
|
|
|
|
httpError(res, e.UnknownMethod)
|
2018-07-07 17:21:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Execute and get response
|
|
|
|
---------------------------------------------------------*/
|
2018-10-01 17:27:38 +00:00
|
|
|
/* (1) Give HTTP METHOD */
|
|
|
|
parameters["_HTTP_METHOD_"] = httpMethod
|
|
|
|
|
|
|
|
/* (2) Give Authorization header into controller */
|
2018-07-10 23:51:10 +00:00
|
|
|
parameters["_AUTHORIZATION_"] = req.Header.Get("Authorization")
|
2018-07-07 17:21:00 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* (3) Give Scope into controller */
|
2018-07-07 20:10:56 +00:00
|
|
|
parameters["_SCOPE_"] = scope
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* (4) Execute */
|
|
|
|
response := ctlMethod(parameters)
|
2018-07-07 17:21:00 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* (5) Extract http headers */
|
2018-07-07 17:21:00 +00:00
|
|
|
for k, v := range response.Dump() {
|
|
|
|
if k == "_REDIRECT_" {
|
|
|
|
if newLocation, ok := v.(string); ok {
|
|
|
|
httpRedirect(res, newLocation)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (5) Build JSON response */
|
2018-07-07 17:21:00 +00:00
|
|
|
httpPrint(res, response)
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:00:45 +00:00
|
|
|
// extractParameters extracts parameters for the request and checks
|
2018-07-07 17:24:02 +00:00
|
|
|
// every single one according to configuration options
|
2018-10-01 12:15:00 +00:00
|
|
|
func (s *Server) extractParameters(req *apirequest.Request, methodParam map[string]*api.Parameter) (map[string]interface{}, e.Error) {
|
2018-07-07 17:21:00 +00:00
|
|
|
|
|
|
|
// init vars
|
2018-07-08 23:00:45 +00:00
|
|
|
err := e.Success
|
2018-07-06 08:49:52 +00:00
|
|
|
parameters := make(map[string]interface{})
|
2018-07-07 17:21:00 +00:00
|
|
|
|
2018-07-07 17:24:02 +00:00
|
|
|
// for each param of the config
|
2018-07-07 17:21:00 +00:00
|
|
|
for name, param := range methodParam {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (1) Extract value */
|
|
|
|
p, isset := req.Data.Set[name]
|
|
|
|
|
|
|
|
/* (2) Required & missing */
|
|
|
|
if !isset && !param.Optional {
|
2018-07-07 17:24:02 +00:00
|
|
|
err = e.MissingParam
|
|
|
|
err.BindArgument(name)
|
|
|
|
return nil, err
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Optional & missing: set default value */
|
|
|
|
if !isset {
|
2018-07-10 23:36:42 +00:00
|
|
|
p = &apirequest.Parameter{
|
2018-07-06 08:49:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Parse parameter if not file */
|
|
|
|
if !p.File {
|
|
|
|
p.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Fail on unexpected multipart file */
|
|
|
|
waitFile, gotFile := param.Type == "FILE", p.File
|
|
|
|
if gotFile && !waitFile || !gotFile && waitFile {
|
2018-07-07 17:24:02 +00:00
|
|
|
err = e.InvalidParam
|
|
|
|
err.BindArgument(param.Rename)
|
|
|
|
err.BindArgument("FILE")
|
|
|
|
return nil, err
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Do not check if file */
|
|
|
|
if gotFile {
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (7) Check type */
|
2018-07-10 23:36:42 +00:00
|
|
|
if s.checker.Run(param.Type, p.Value) != nil {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2018-07-07 17:24:02 +00:00
|
|
|
err = e.InvalidParam
|
|
|
|
err.BindArgument(param.Rename)
|
|
|
|
err.BindArgument(param.Type)
|
|
|
|
err.BindArgument(p.Value)
|
2018-07-06 08:49:52 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
parameters[param.Rename] = p.Value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-07 17:24:02 +00:00
|
|
|
return parameters, err
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|