2018-07-07 16:10:42 +00:00
|
|
|
package aicra
|
2018-05-21 10:02:24 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-18 14:06:49 +00:00
|
|
|
"fmt"
|
2020-04-04 08:39:02 +00:00
|
|
|
"net/http"
|
2021-05-18 14:06:49 +00:00
|
|
|
"strings"
|
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
|
|
|
)
|
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
// Handler wraps the builder to handle requests
|
|
|
|
type Handler Builder
|
2020-04-04 08:36:52 +00:00
|
|
|
|
2021-04-18 14:50:02 +00:00
|
|
|
// ServeHTTP implements http.Handler and wraps it in middlewares (adapters)
|
2021-03-28 17:03:16 +00:00
|
|
|
func (s Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-05-18 07:36:33 +00:00
|
|
|
var h = http.HandlerFunc(s.resolve)
|
2021-04-18 14:50:02 +00:00
|
|
|
|
|
|
|
for _, adapter := range s.adapters {
|
|
|
|
h = adapter(h)
|
|
|
|
}
|
|
|
|
h(w, r)
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:36:33 +00:00
|
|
|
func (s Handler) resolve(w http.ResponseWriter, r *http.Request) {
|
2021-03-28 17:03:16 +00:00
|
|
|
// 1. find a matching service from config
|
|
|
|
var service = s.conf.Find(r)
|
2020-04-04 09:50:01 +00:00
|
|
|
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
|
2021-03-28 17:03:16 +00:00
|
|
|
var input, err = extractInput(service, *r)
|
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
|
2021-03-28 17:03:16 +00:00
|
|
|
for _, h := range s.handlers {
|
2020-04-04 09:50:01 +00:00
|
|
|
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
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
// 4. fail on no matching 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
|
|
|
}
|
|
|
|
|
2021-05-18 14:06:49 +00:00
|
|
|
// replace format '[a]' in scope where 'a' is an existing input's name
|
|
|
|
scope := make([][]string, len(service.Scope))
|
|
|
|
for a, list := range service.Scope {
|
|
|
|
scope[a] = make([]string, len(list))
|
|
|
|
for b, perm := range list {
|
|
|
|
scope[a][b] = perm
|
|
|
|
for name, value := range input.Data {
|
2021-05-18 15:45:07 +00:00
|
|
|
var (
|
|
|
|
token = fmt.Sprintf("[%s]", name)
|
|
|
|
replacement = ""
|
2021-05-18 14:24:31 +00:00
|
|
|
)
|
2021-05-18 15:45:07 +00:00
|
|
|
if value != nil {
|
|
|
|
replacement = fmt.Sprintf("[%v]", value)
|
|
|
|
}
|
|
|
|
scope[a][b] = strings.ReplaceAll(scope[a][b], token, replacement)
|
2021-05-18 14:06:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 07:59:49 +00:00
|
|
|
var auth = api.Auth{
|
2021-05-18 14:06:49 +00:00
|
|
|
Required: scope,
|
2021-05-18 07:59:49 +00:00
|
|
|
Active: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. run auth-aware middlewares
|
|
|
|
var h = api.AuthHandlerFunc(func(a api.Auth, w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !a.Granted() {
|
|
|
|
handleError(api.ErrPermission, w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.handle(input, handler, service, w, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, adapter := range s.authAdapters {
|
|
|
|
h = adapter(h)
|
|
|
|
}
|
|
|
|
h(auth, w, r)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Handler) handle(input *reqdata.T, handler *apiHandler, service *config.Service, w http.ResponseWriter, r *http.Request) {
|
2021-03-28 17:03:16 +00:00
|
|
|
// 5. pass execution to the handler
|
2021-04-18 17:31:54 +00:00
|
|
|
ctx := api.Ctx{Res: w, Req: r}
|
|
|
|
var outData, outErr = handler.dyn.Handle(ctx, input.Data)
|
2020-04-04 10:05:17 +00:00
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
// 6. build res from returned data
|
|
|
|
var res = api.EmptyResponse().WithError(outErr)
|
|
|
|
for key, value := range outData {
|
2020-04-04 09:46:37 +00:00
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
// find original name from 'rename' field
|
2020-04-04 09:50:01 +00:00
|
|
|
for name, param := range service.Output {
|
|
|
|
if param.Rename == key {
|
2021-03-28 17:03:16 +00:00
|
|
|
res.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
|
2021-03-28 17:03:16 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
for key, values := range res.Headers {
|
2020-04-04 09:50:01 +00:00
|
|
|
for _, value := range values {
|
2021-03-28 17:03:16 +00:00
|
|
|
w.Header().Add(key, value)
|
2020-03-21 15:53:19 +00:00
|
|
|
}
|
2019-09-26 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
res.ServeHTTP(w, r)
|
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
|
|
|
}
|
|
|
|
|
2021-03-28 17:03:16 +00:00
|
|
|
func extractInput(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
|
|
|
|
}
|