update: rename 'Server' into 'Handler'

This commit is contained in:
Adrien Marquès 2021-03-28 19:03:16 +02:00
parent 10e59acdae
commit 468a09be8d
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
2 changed files with 23 additions and 23 deletions

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