From 468a09be8d75460ad050aab4b703defb2de04b05 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 28 Mar 2021 19:03:16 +0200 Subject: [PATCH] update: rename 'Server' into 'Handler' --- builder.go | 2 +- server.go => handler.go | 44 ++++++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 23 deletions(-) rename server.go => handler.go (54%) diff --git a/builder.go b/builder.go index a39c447..65c5240 100644 --- a/builder.go +++ b/builder.go @@ -115,5 +115,5 @@ func (b Builder) Build() (http.Handler, error) { } } - return Server(b), nil + return Handler(b), nil } diff --git a/server.go b/handler.go similarity index 54% rename from server.go rename to handler.go index 2a81e41..9d7f30b 100644 --- a/server.go +++ b/handler.go @@ -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