2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-04 09:50:01 +00:00
|
|
|
"log"
|
2020-04-04 08:39:02 +00:00
|
|
|
"net/http"
|
2019-04-30 22:02:28 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
"git.xdrm.io/go/aicra/api"
|
|
|
|
"git.xdrm.io/go/aicra/internal/reqdata"
|
2018-05-21 10:02:24 +00:00
|
|
|
)
|
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// Server hides the builder and allows handling http requests
|
|
|
|
type Server Builder
|
2020-04-04 08:36:52 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// ServeHTTP implements http.Handler and is called on each request
|
|
|
|
func (server Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
|
|
|
defer req.Body.Close()
|
2018-07-10 23:36:42 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 1. find a matching service in the config
|
|
|
|
service := server.conf.Find(req)
|
|
|
|
if service == nil {
|
|
|
|
response := api.EmptyResponse().WithError(api.ErrorUnknownService)
|
|
|
|
response.ServeHTTP(res, req)
|
|
|
|
logError(response)
|
|
|
|
return
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 2. build input parameter receiver
|
|
|
|
dataset := reqdata.New(service)
|
|
|
|
|
|
|
|
// 3. extract URI data
|
|
|
|
err := dataset.ExtractURI(req)
|
|
|
|
if err != nil {
|
|
|
|
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
|
|
|
response.ServeHTTP(res, req)
|
|
|
|
logError(response)
|
|
|
|
return
|
2018-10-01 17:27:38 +00:00
|
|
|
}
|
2020-04-04 09:50:01 +00:00
|
|
|
|
|
|
|
// 4. extract query data
|
|
|
|
err = dataset.ExtractQuery(req)
|
|
|
|
if err != nil {
|
|
|
|
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
|
|
|
response.ServeHTTP(res, req)
|
|
|
|
logError(response)
|
|
|
|
return
|
2020-04-04 09:46:37 +00:00
|
|
|
}
|
2018-07-07 17:21:00 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 5. extract form/json data
|
|
|
|
err = dataset.ExtractForm(req)
|
|
|
|
if err != nil {
|
|
|
|
response := api.EmptyResponse().WithError(api.ErrorMissingParam)
|
|
|
|
response.ServeHTTP(res, req)
|
|
|
|
logError(response)
|
|
|
|
return
|
2020-04-04 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 6. find a matching handler
|
|
|
|
var handler *apiHandler
|
|
|
|
for _, h := range server.handlers {
|
|
|
|
if h.Method == service.Method && h.Path == service.Pattern {
|
|
|
|
handler = h
|
2020-03-29 13:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 08:36:52 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 7. fail if found no handler
|
|
|
|
if handler == nil {
|
|
|
|
r := api.EmptyResponse().WithError(api.ErrorUnknownService)
|
|
|
|
r.ServeHTTP(res, req)
|
|
|
|
logError(r)
|
|
|
|
return
|
2020-03-29 13:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 8. build api.Request from http.Request
|
|
|
|
apireq, err := api.NewRequest(req)
|
2020-03-28 13:57:28 +00:00
|
|
|
if err != nil {
|
2020-04-04 09:50:01 +00:00
|
|
|
log.Fatal(err)
|
2020-03-28 13:57:28 +00:00
|
|
|
}
|
2020-04-04 08:36:52 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 9. feed request with scope & parameters
|
|
|
|
apireq.Scope = service.Scope
|
|
|
|
apireq.Param = dataset.Data
|
2020-04-04 08:36:52 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 10. execute
|
|
|
|
returned, apiErr := handler.dyn.Handle(dataset.Data)
|
|
|
|
response := api.EmptyResponse().WithError(apiErr)
|
|
|
|
for key, value := range returned {
|
2020-04-04 09:46:37 +00:00
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// find original name from rename
|
|
|
|
for name, param := range service.Output {
|
|
|
|
if param.Rename == key {
|
|
|
|
response.SetData(name, value)
|
2020-03-21 15:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 09:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 11. apply headers
|
|
|
|
res.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
for key, values := range response.Headers {
|
|
|
|
for _, value := range values {
|
|
|
|
res.Header().Add(key, value)
|
2020-03-21 15:53:19 +00:00
|
|
|
}
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 09:50:01 +00:00
|
|
|
// 12. write to response
|
|
|
|
response.ServeHTTP(res, req)
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|