2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-30 22:02:28 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-05-01 11:44:45 +00:00
|
|
|
"os"
|
2019-04-30 22:02:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-10-07 09:40:35 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
2019-05-01 11:44:45 +00:00
|
|
|
|
2018-10-01 15:43:18 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
2019-05-01 11:44:45 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/reqdata"
|
|
|
|
checker "git.xdrm.io/go/aicra/typecheck"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// Server represents an AICRA instance featuring: type checkers, services
|
2018-07-10 23:36:42 +00:00
|
|
|
type Server struct {
|
2019-05-01 11:44:45 +00:00
|
|
|
services *config.Service
|
2019-05-01 13:14:49 +00:00
|
|
|
Checkers *checker.Set
|
2019-05-01 11:44:45 +00:00
|
|
|
handlers []*api.Handler
|
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
|
2019-05-01 11:44:45 +00:00
|
|
|
func New(configPath string) (*Server, error) {
|
2018-09-27 11:43:36 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
var err error
|
2018-09-28 13:58:30 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 1. init instance
|
2018-07-10 23:51:10 +00:00
|
|
|
var i = &Server{
|
2019-05-01 11:44:45 +00:00
|
|
|
services: nil,
|
2019-05-01 13:14:49 +00:00
|
|
|
Checkers: checker.New(),
|
2019-05-01 11:44:45 +00:00
|
|
|
handlers: make([]*api.Handler, 0),
|
2018-07-10 23:51:10 +00:00
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 2. open config file
|
|
|
|
configFile, err := os.Open(configPath)
|
2018-07-06 08:49:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-01 11:44:45 +00:00
|
|
|
defer configFile.Close()
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 3. load configuration
|
|
|
|
i.services, err = config.Parse(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-10-01 17:27:38 +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-10-02 15:14:44 +00:00
|
|
|
defer req.Body.Close()
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 1. build API request from HTTP request
|
|
|
|
apiRequest, err := api.NewRequest(req)
|
2018-07-07 17:21:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 2. find a matching service for this path in the config
|
|
|
|
serviceDef, pathIndex := s.services.Browse(apiRequest.URI)
|
|
|
|
if serviceDef == nil {
|
2018-07-07 20:10:56 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-01 11:44:45 +00:00
|
|
|
servicePath := strings.Join(apiRequest.URI[:pathIndex], "/")
|
2019-05-01 14:28:16 +00:00
|
|
|
if !strings.HasPrefix(servicePath, "/") {
|
|
|
|
servicePath = "/" + servicePath
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 13:56:18 +00:00
|
|
|
// 3. check if matching methodDef exists in config */
|
|
|
|
var methodDef = serviceDef.Method(req.Method)
|
|
|
|
if methodDef == nil {
|
2019-05-01 16:01:32 +00:00
|
|
|
apiResponse := api.NewResponse(api.ErrorUnknownMethod())
|
|
|
|
apiResponse.Write(res)
|
|
|
|
logError(apiResponse)
|
2018-07-06 08:49:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 4. parse every input data from the request
|
2019-05-01 13:14:49 +00:00
|
|
|
store := reqdata.New(apiRequest.URI[pathIndex:], req)
|
2018-07-07 20:10:56 +00:00
|
|
|
|
2018-07-06 08:49:52 +00:00
|
|
|
/* (4) Check parameters
|
|
|
|
---------------------------------------------------------*/
|
2019-05-01 13:56:18 +00:00
|
|
|
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
|
2018-07-07 17:21:00 +00:00
|
|
|
|
|
|
|
// Fail if argument check failed
|
2019-05-01 11:44:45 +00:00
|
|
|
if paramError.Code != api.ErrorSuccess().Code {
|
2019-05-01 16:01:32 +00:00
|
|
|
apiResponse := api.NewResponse(paramError)
|
|
|
|
apiResponse.Write(res)
|
|
|
|
logError(apiResponse)
|
2018-07-07 17:21:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
apiRequest.Param = parameters
|
2018-10-01 17:27:38 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
/* (5) Search a matching handler
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
var serviceHandler *api.Handler
|
|
|
|
var serviceFound bool
|
|
|
|
|
|
|
|
for _, handler := range s.handlers {
|
|
|
|
if handler.GetPath() == servicePath {
|
|
|
|
serviceFound = true
|
|
|
|
if handler.GetMethod() == req.Method {
|
|
|
|
serviceHandler = handler
|
|
|
|
}
|
|
|
|
}
|
2018-10-01 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// fail if found no handler
|
|
|
|
if serviceHandler == nil {
|
|
|
|
if serviceFound {
|
2019-05-01 16:05:58 +00:00
|
|
|
apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableMethod(), servicePath, req.Method))
|
2019-05-01 16:01:32 +00:00
|
|
|
apiResponse.Write(res)
|
|
|
|
logError(apiResponse)
|
2019-05-01 11:44:45 +00:00
|
|
|
return
|
|
|
|
}
|
2019-05-01 14:20:32 +00:00
|
|
|
|
2019-05-01 16:05:58 +00:00
|
|
|
apiResponse := api.NewResponse(api.WrapError(api.ErrorUncallableService(), servicePath))
|
2019-05-01 16:01:32 +00:00
|
|
|
apiResponse.Write(res)
|
|
|
|
logError(apiResponse)
|
2018-07-07 17:21:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
/* (6) Execute handler and return response
|
2018-07-07 17:21:00 +00:00
|
|
|
---------------------------------------------------------*/
|
2019-05-01 13:56:18 +00:00
|
|
|
// 1. feed request with configuration scope
|
|
|
|
apiRequest.Scope = methodDef.Permission
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// 1. execute
|
|
|
|
apiResponse := api.NewResponse()
|
|
|
|
serviceHandler.Handle(*apiRequest, apiResponse)
|
|
|
|
|
|
|
|
// 2. apply headers
|
|
|
|
for key, values := range apiResponse.Headers {
|
|
|
|
for _, value := range values {
|
|
|
|
res.Header().Add(key, value)
|
2018-07-07 17:21:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:01:32 +00:00
|
|
|
// 3. write to response
|
|
|
|
apiResponse.Write(res)
|
2018-07-07 17:21:00 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// HandleFunc sets a new handler for an HTTP method to a path
|
|
|
|
func (s *Server) HandleFunc(httpMethod, path string, handlerFunc api.HandlerFunc) {
|
|
|
|
handler := api.NewHandler(httpMethod, path, handlerFunc)
|
|
|
|
s.handlers = append(s.handlers, handler)
|
|
|
|
}
|
2018-07-06 08:49:52 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// Handle sets a new handler
|
|
|
|
func (s *Server) Handle(handler *api.Handler) {
|
|
|
|
s.handlers = append(s.handlers, handler)
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|