improvements, fixes, update to go 1.16 #16

Merged
xdrm-brackets merged 7 commits from refactor/go1.16 into 0.3.0 2021-03-28 17:44:59 +00:00
2 changed files with 23 additions and 23 deletions
Showing only changes of commit 468a09be8d - Show all commits

View File

@ -115,5 +115,5 @@ func (b Builder) Build() (http.Handler, error) {
} }
} }
return Server(b), nil return Handler(b), nil
} }

View File

@ -8,22 +8,22 @@ import (
"git.xdrm.io/go/aicra/internal/reqdata" "git.xdrm.io/go/aicra/internal/reqdata"
) )
// Server hides the builder and allows handling http requests // Handler wraps the builder to handle requests
type Server Builder type Handler Builder
// ServeHTTP implements http.Handler and is called on each request // ServeHTTP implements http.Handler
func (server Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (s Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer req.Body.Close() defer r.Body.Close()
// 1. find a matching service in the config // 1. find a matching service from config
service := server.conf.Find(req) var service = s.conf.Find(r)
if service == nil { if service == nil {
handleError(api.ErrUnknownService, w, r) handleError(api.ErrUnknownService, w, r)
return return
} }
// 2. extract request data // 2. extract request data
dataset, err := extractRequestData(service, *req) var input, err = extractInput(service, *r)
if err != nil { if err != nil {
handleError(api.ErrMissingParam, w, r) handleError(api.ErrMissingParam, w, r)
return return
@ -31,42 +31,42 @@ func (server Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
// 3. find a matching handler // 3. find a matching handler
var handler *apiHandler var handler *apiHandler
for _, h := range server.handlers { for _, h := range s.handlers {
if h.Method == service.Method && h.Path == service.Pattern { if h.Method == service.Method && h.Path == service.Pattern {
handler = h handler = h
} }
} }
// 4. fail if found no handler // 4. fail on no matching handler
if handler == nil { if handler == nil {
handleError(api.ErrUncallableService, w, r) handleError(api.ErrUncallableService, w, r)
return return
} }
// 5. execute // 5. pass execution to the handler
returned, apiErr := handler.dyn.Handle(dataset.Data) var outData, outErr = handler.dyn.Handle(input.Data)
// 6. build response from returned data // 6. build res from returned data
response := api.EmptyResponse().WithError(apiErr) var res = api.EmptyResponse().WithError(outErr)
for key, value := range returned { for key, value := range outData {
// find original name from rename // find original name from 'rename' field
for name, param := range service.Output { for name, param := range service.Output {
if param.Rename == key { if param.Rename == key {
response.SetData(name, value) res.SetData(name, value)
} }
} }
} }
// 7. apply headers // 7. apply headers
res.Header().Set("Content-Type", "application/json; charset=utf-8") w.Header().Set("Content-Type", "application/json; charset=utf-8")
for key, values := range response.Headers { for key, values := range res.Headers {
for _, value := range values { for _, value := range values {
res.Header().Add(key, value) w.Header().Add(key, value)
} }
} }
response.ServeHTTP(res, req) res.ServeHTTP(w, r)
} }
func handleError(err api.Err, w http.ResponseWriter, r *http.Request) { func handleError(err api.Err, w http.ResponseWriter, r *http.Request) {
@ -74,7 +74,7 @@ func handleError(err api.Err, w http.ResponseWriter, r *http.Request) {
response.ServeHTTP(w, r) response.ServeHTTP(w, r)
} }
func extractRequestData(service *config.Service, req http.Request) (*reqdata.T, error) { func extractInput(service *config.Service, req http.Request) (*reqdata.T, error) {
var dataset = reqdata.New(service) var dataset = reqdata.New(service)
// URI data // URI data