2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-07-06 08:49:52 +00:00
|
|
|
"git.xdrm.io/go/aicra/checker"
|
|
|
|
"git.xdrm.io/go/aicra/config"
|
2018-07-07 17:21:00 +00:00
|
|
|
e "git.xdrm.io/go/aicra/err"
|
2018-07-06 08:49:52 +00:00
|
|
|
"git.xdrm.io/go/aicra/implement"
|
2018-07-07 20:10:56 +00:00
|
|
|
"git.xdrm.io/go/aicra/middleware"
|
2018-07-06 08:49:52 +00:00
|
|
|
"git.xdrm.io/go/aicra/request"
|
|
|
|
"log"
|
2018-05-21 10:02:24 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-07-06 08:49:52 +00:00
|
|
|
// Init initilises a new framework instance
|
2018-07-07 20:10:56 +00:00
|
|
|
// - path is the configuration file
|
|
|
|
func New(path string) (*Server, error) {
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (1) Init instance */
|
|
|
|
inst := &Server{
|
|
|
|
config: nil,
|
|
|
|
Params: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Load configuration */
|
|
|
|
config, err := config.Load(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inst.config = config
|
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (3) Default type registry */
|
|
|
|
inst.SetTypeFolder(".build/type")
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (4) Default middleware registry */
|
|
|
|
inst.SetMiddlewareFolder(".build/middleware")
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
return inst, nil
|
2018-07-07 20:10:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the type (checker) registry from
|
|
|
|
// a given folder
|
|
|
|
func (s *Server) SetTypeFolder(path string) {
|
|
|
|
s.Checker = checker.CreateRegistry(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the middleware registry from
|
|
|
|
// a given folder
|
|
|
|
func (s *Server) SetMiddlewareFolder(path string) {
|
|
|
|
s.Middleware = middleware.CreateRegistry(path)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Listens and binds the server to the given port
|
2018-07-07 20:10:56 +00:00
|
|
|
func (s *Server) Listen(port uint16) error {
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
/* (1) Bind router */
|
2018-07-06 08:49:52 +00:00
|
|
|
http.HandleFunc("/", s.routeRequest)
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
/* (2) Bind listener */
|
|
|
|
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
|
|
|
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
// Router called for each request
|
|
|
|
func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) {
|
|
|
|
|
|
|
|
/* (1) Build request */
|
2018-07-07 17:21:00 +00:00
|
|
|
req, err := request.BuildFromHttpRequest(httpReq)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Middleware: authentication */
|
2018-07-07 20:10:56 +00:00
|
|
|
scope := s.Middleware.Run(*httpReq)
|
2018-07-07 21:15:54 +00:00
|
|
|
fmt.Printf("scope is %v\n", scope)
|
2018-07-06 08:49:52 +00:00
|
|
|
|
|
|
|
/* (3) Find a matching controller */
|
|
|
|
controller := s.findController(req)
|
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 */
|
|
|
|
var method = controller.Method(httpReq.Method)
|
|
|
|
|
|
|
|
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-07 17:21:00 +00:00
|
|
|
parameters, paramError := s.ExtractParameters(req, method.Parameters)
|
|
|
|
|
|
|
|
// Fail if argument check failed
|
|
|
|
if paramError.Code != e.Success.Code {
|
|
|
|
httpError(res, paramError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Load controller
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
callable, err := req.LoadController(httpReq.Method)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[err] %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (6) Execute and get response
|
|
|
|
---------------------------------------------------------*/
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (1) Give Authorization header into controller */
|
2018-07-07 17:21:00 +00:00
|
|
|
authHeader := httpReq.Header.Get("Authorization")
|
|
|
|
if len(authHeader) > 0 {
|
|
|
|
parameters["_AUTHORIZATION_"] = authHeader
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (2) Give Scope into controller */
|
|
|
|
parameters["_SCOPE_"] = scope
|
|
|
|
|
|
|
|
/* (3) Execute */
|
2018-07-07 17:24:02 +00:00
|
|
|
response := callable(parameters, implement.NewResponse())
|
2018-07-07 17:21:00 +00:00
|
|
|
|
2018-07-07 20:10:56 +00:00
|
|
|
/* (4) 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-07 17:24:02 +00:00
|
|
|
// ExtractParameters extracts parameters for the request and checks
|
|
|
|
// every single one according to configuration options
|
2018-07-07 17:21:00 +00:00
|
|
|
func (s *Server) ExtractParameters(req *request.Request, methodParam map[string]*config.Parameter) (map[string]interface{}, e.Error) {
|
|
|
|
|
|
|
|
// init vars
|
2018-07-07 17:24:02 +00:00
|
|
|
var err e.Error = 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 {
|
|
|
|
p = &request.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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (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 */
|
|
|
|
if s.Checker.Run(param.Type, p.Value) != nil {
|
|
|
|
|
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
|
|
|
}
|