0.2.0 #10

Manually merged
xdrm-brackets merged 43 commits from 0.2.0 into master 2020-03-08 15:28:42 +00:00
3 changed files with 50 additions and 47 deletions
Showing only changes of commit 1225e3b1f2 - Show all commits

87
http.go
View File

@ -13,100 +13,103 @@ import (
type httpServer Server
// ServeHTTP implements http.Handler and has to be called on each request
func (s httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (server httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
// 1. build API request from HTTP request
/* (1) create api.Request from http.Request
---------------------------------------------------------*/
request, err := api.NewRequest(r)
if err != nil {
log.Fatal(err)
}
// 2. find a matching service for this path in the config
serviceDef, pathIndex := s.services.Browse(request.URI)
if serviceDef == nil {
serviceConf, pathIndex := server.config.Browse(request.URI)
if serviceConf == nil {
return
}
// 3. extract the service path from request URI
servicePath := strings.Join(request.URI[:pathIndex], "/")
if !strings.HasPrefix(servicePath, "/") {
servicePath = "/" + servicePath
}
// 3. check if matching methodDef exists in config */
var methodDef = serviceDef.Method(r.Method)
if methodDef == nil {
response := api.NewResponse(api.ErrorUnknownMethod())
response.ServeHTTP(w, r)
logError(response)
// 4. find method configuration from http method */
var methodConf = serviceConf.Method(r.Method)
if methodConf == nil {
res := api.NewResponse(api.ErrorUnknownMethod())
res.ServeHTTP(w, r)
logError(res)
return
}
// 4. parse every input data from the request
store := reqdata.New(request.URI[pathIndex:], r)
// 5. parse data from the request (uri, query, form, json)
data := reqdata.New(request.URI[pathIndex:], r)
/* (4) Check parameters
/* (2) check parameters
---------------------------------------------------------*/
parameters, paramError := s.extractParameters(store, methodDef.Parameters)
parameters, paramError := server.extractParameters(data, methodConf.Parameters)
// Fail if argument check failed
if paramError.Code != api.ErrorSuccess().Code {
response := api.NewResponse(paramError)
response.ServeHTTP(w, r)
logError(response)
res := api.NewResponse(paramError)
res.ServeHTTP(w, r)
logError(res)
return
}
request.Param = parameters
/* (5) Search a matching handler
/* (3) search for the handler
---------------------------------------------------------*/
var serviceHandler *api.Handler
var serviceFound bool
var foundHandler *api.Handler
var found bool
for _, handler := range s.handlers {
for _, handler := range server.handlers {
if handler.GetPath() == servicePath {
serviceFound = true
found = true
if handler.GetMethod() == r.Method {
serviceHandler = handler
foundHandler = handler
}
}
}
// fail if found no handler
if serviceHandler == nil {
if serviceFound {
response := api.NewResponse()
response.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
response.ServeHTTP(w, r)
logError(response)
if foundHandler == nil {
if found {
res := api.NewResponse()
res.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
res.ServeHTTP(w, r)
logError(res)
return
}
response := api.NewResponse()
response.SetError(api.ErrorUncallableService(), servicePath)
response.ServeHTTP(w, r)
logError(response)
res := api.NewResponse()
res.SetError(api.ErrorUncallableService(), servicePath)
res.ServeHTTP(w, r)
logError(res)
return
}
/* (6) Execute handler and return response
/* (4) execute handler and return response
---------------------------------------------------------*/
// 1. feed request with configuration scope
request.Scope = methodDef.Scope
request.Scope = methodConf.Scope
// 1. execute
response := api.NewResponse()
serviceHandler.Handle(*request, response)
// 2. execute
res := api.NewResponse()
foundHandler.Handle(*request, res)
// 2. apply headers
for key, values := range response.Headers {
// 3. apply headers
for key, values := range res.Headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
// 3. write to response
response.ServeHTTP(w, r)
// 4. write to response
res.ServeHTTP(w, r)
return
}

View File

@ -13,7 +13,7 @@ import (
// Server represents an AICRA instance featuring: type checkers, services
type Server struct {
services *config.Service
config *config.Service
Checkers *checker.Set
handlers []*api.Handler
}
@ -27,7 +27,7 @@ func New(configPath string) (*Server, error) {
// 1. init instance
var i = &Server{
services: nil,
config: nil,
Checkers: checker.New(),
handlers: make([]*api.Handler, 0),
}
@ -40,14 +40,14 @@ func New(configPath string) (*Server, error) {
defer configFile.Close()
// 3. load configuration
i.services, err = config.Parse(configFile)
i.config, err = config.Parse(configFile)
if err != nil {
return nil, err
}
// 4. log configuration services
log.Printf("🔧 Reading configuration '%s'\n", configPath)
logService(*i.services, "")
logService(*i.config, "")
return i, nil

View File

@ -58,7 +58,7 @@ func (s *httpServer) extractParameters(store *reqdata.Store, methodParam map[str
return nil, apiErr
}
// 6. do not check if file
// 6. ignore type check if file
if gotFile {
parameters[param.Rename] = p.Value
continue