2019-09-26 17:03:37 +00:00
|
|
|
package aicra
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.xdrm.io/go/aicra/api"
|
|
|
|
"git.xdrm.io/go/aicra/internal/reqdata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// httpServer wraps the aicra server to allow handling http requests
|
|
|
|
type httpServer Server
|
|
|
|
|
|
|
|
// ServeHTTP implements http.Handler and has to be called on each request
|
2020-03-03 17:36:52 +00:00
|
|
|
func (server httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-09-26 17:03:37 +00:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
/* (1) create api.Request from http.Request
|
|
|
|
---------------------------------------------------------*/
|
2019-09-26 17:03:37 +00:00
|
|
|
request, err := api.NewRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. find a matching service for this path in the config
|
2020-03-03 17:36:52 +00:00
|
|
|
serviceConf, pathIndex := server.config.Browse(request.URI)
|
|
|
|
if serviceConf == nil {
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-03 17:36:52 +00:00
|
|
|
|
|
|
|
// 3. extract the service path from request URI
|
2019-09-26 17:03:37 +00:00
|
|
|
servicePath := strings.Join(request.URI[:pathIndex], "/")
|
|
|
|
if !strings.HasPrefix(servicePath, "/") {
|
|
|
|
servicePath = "/" + servicePath
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 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)
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 5. parse data from the request (uri, query, form, json)
|
|
|
|
data := reqdata.New(request.URI[pathIndex:], r)
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
/* (2) check parameters
|
2019-09-26 17:03:37 +00:00
|
|
|
---------------------------------------------------------*/
|
2020-03-03 17:36:52 +00:00
|
|
|
parameters, paramError := server.extractParameters(data, methodConf.Parameters)
|
2019-09-26 17:03:37 +00:00
|
|
|
|
|
|
|
// Fail if argument check failed
|
|
|
|
if paramError.Code != api.ErrorSuccess().Code {
|
2020-03-03 17:36:52 +00:00
|
|
|
res := api.NewResponse(paramError)
|
|
|
|
res.ServeHTTP(w, r)
|
|
|
|
logError(res)
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Param = parameters
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
/* (3) search for the handler
|
2019-09-26 17:03:37 +00:00
|
|
|
---------------------------------------------------------*/
|
2020-03-03 17:36:52 +00:00
|
|
|
var foundHandler *api.Handler
|
|
|
|
var found bool
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
for _, handler := range server.handlers {
|
2019-09-26 17:03:37 +00:00
|
|
|
if handler.GetPath() == servicePath {
|
2020-03-03 17:36:52 +00:00
|
|
|
found = true
|
2019-09-26 17:03:37 +00:00
|
|
|
if handler.GetMethod() == r.Method {
|
2020-03-03 17:36:52 +00:00
|
|
|
foundHandler = handler
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if found no handler
|
2020-03-03 17:36:52 +00:00
|
|
|
if foundHandler == nil {
|
|
|
|
if found {
|
|
|
|
res := api.NewResponse()
|
|
|
|
res.SetError(api.ErrorUncallableMethod(), servicePath, r.Method)
|
|
|
|
res.ServeHTTP(w, r)
|
|
|
|
logError(res)
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
res := api.NewResponse()
|
|
|
|
res.SetError(api.ErrorUncallableService(), servicePath)
|
|
|
|
res.ServeHTTP(w, r)
|
|
|
|
logError(res)
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
/* (4) execute handler and return response
|
2019-09-26 17:03:37 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
// 1. feed request with configuration scope
|
2020-03-03 17:36:52 +00:00
|
|
|
request.Scope = methodConf.Scope
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 2. execute
|
|
|
|
res := api.NewResponse()
|
|
|
|
foundHandler.Handle(*request, res)
|
2019-09-26 17:03:37 +00:00
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 3. apply headers
|
|
|
|
for key, values := range res.Headers {
|
2019-09-26 17:03:37 +00:00
|
|
|
for _, value := range values {
|
|
|
|
w.Header().Add(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:36:52 +00:00
|
|
|
// 4. write to response
|
|
|
|
res.ServeHTTP(w, r)
|
2019-09-26 17:03:37 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|