refactor server handler, rename for readability

This commit is contained in:
Adrien Marquès 2020-03-03 18:36:52 +01:00
parent 682e20a959
commit 1225e3b1f2
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
3 changed files with 50 additions and 47 deletions

87
http.go
View File

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

View File

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

View File

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