2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
2020-04-04 10:05:17 +00:00
|
|
|
"git.xdrm.io/go/aicra/internal/config"
|
2020-04-04 09:50:01 +00:00
|
|
|
"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 {
|
2021-03-28 16:49:23 +00:00
|
|
|
handleError(api.ErrUnknownService, w, r)
|
2020-04-04 09:50:01 +00:00
|
|
|
return
|
2018-07-06 08:49:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:05:17 +00:00
|
|
|
// 2. extract request data
|
|
|
|
dataset, err := extractRequestData(service, *req)
|
2020-04-04 09:50:01 +00:00
|
|
|
if err != nil {
|
2021-03-28 16:49:23 +00:00
|
|
|
handleError(api.ErrMissingParam, w, r)
|
2020-04-04 09:50:01 +00:00
|
|
|
return
|
2020-04-04 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:05:17 +00:00
|
|
|
// 3. find a matching handler
|
2020-04-04 09:50:01 +00:00
|
|
|
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 10:05:17 +00:00
|
|
|
// 4. fail if found no handler
|
2020-04-04 09:50:01 +00:00
|
|
|
if handler == nil {
|
2021-03-28 16:49:23 +00:00
|
|
|
handleError(api.ErrUncallableService, w, r)
|
2020-04-04 09:50:01 +00:00
|
|
|
return
|
2020-03-29 13:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:05:17 +00:00
|
|
|
// 5. execute
|
2020-04-04 09:50:01 +00:00
|
|
|
returned, apiErr := handler.dyn.Handle(dataset.Data)
|
2020-04-04 10:05:17 +00:00
|
|
|
|
|
|
|
// 6. build response from returned data
|
2020-04-04 09:50:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-04 10:05:17 +00:00
|
|
|
// 7. apply headers
|
2020-04-04 09:50:01 +00:00
|
|
|
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
|
|
|
response.ServeHTTP(res, req)
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
2020-04-04 10:05:17 +00:00
|
|
|
|
2021-03-28 16:49:23 +00:00
|
|
|
func handleError(err api.Err, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var response = api.EmptyResponse().WithError(err)
|
|
|
|
response.ServeHTTP(w, r)
|
2020-04-04 10:05:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:56:15 +00:00
|
|
|
func extractRequestData(service *config.Service, req http.Request) (*reqdata.T, error) {
|
2021-03-28 16:49:23 +00:00
|
|
|
var dataset = reqdata.New(service)
|
2020-04-04 10:05:17 +00:00
|
|
|
|
2021-03-28 16:49:23 +00:00
|
|
|
// URI data
|
|
|
|
var err = dataset.GetURI(req)
|
2020-04-04 10:05:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-28 16:49:23 +00:00
|
|
|
// query data
|
2020-04-04 12:56:15 +00:00
|
|
|
err = dataset.GetQuery(req)
|
2020-04-04 10:05:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-28 16:49:23 +00:00
|
|
|
// form/json data
|
2020-04-04 12:56:15 +00:00
|
|
|
err = dataset.GetForm(req)
|
2020-04-04 10:05:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataset, nil
|
|
|
|
}
|