From b1498e59c19401f21380a67a2363d09850b6ab1e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 4 Apr 2020 10:36:52 +0200 Subject: [PATCH] clarity rename: dynamic package to dynfunc --- {dynamic => dynfunc}/errors.go | 2 +- {dynamic => dynfunc}/handler.go | 2 +- {dynamic => dynfunc}/spec.go | 2 +- {dynamic => dynfunc}/spec_test.go | 2 +- {dynamic => dynfunc}/types.go | 2 +- handler.go | 32 ----------------------------- server.go | 34 ++++++++++++++++++++++--------- 7 files changed, 29 insertions(+), 47 deletions(-) rename {dynamic => dynfunc}/errors.go (99%) rename {dynamic => dynfunc}/handler.go (99%) rename {dynamic => dynfunc}/spec.go (99%) rename {dynamic => dynfunc}/spec_test.go (99%) rename {dynamic => dynfunc}/types.go (92%) delete mode 100644 handler.go diff --git a/dynamic/errors.go b/dynfunc/errors.go similarity index 99% rename from dynamic/errors.go rename to dynfunc/errors.go index 9467bcf..c038171 100644 --- a/dynamic/errors.go +++ b/dynfunc/errors.go @@ -1,4 +1,4 @@ -package dynamic +package dynfunc // cerr allows you to create constant "const" error with type boxing. type cerr string diff --git a/dynamic/handler.go b/dynfunc/handler.go similarity index 99% rename from dynamic/handler.go rename to dynfunc/handler.go index 29b654a..ad00224 100644 --- a/dynamic/handler.go +++ b/dynfunc/handler.go @@ -1,4 +1,4 @@ -package dynamic +package dynfunc import ( "fmt" diff --git a/dynamic/spec.go b/dynfunc/spec.go similarity index 99% rename from dynamic/spec.go rename to dynfunc/spec.go index ee6fd06..7b3c4f6 100644 --- a/dynamic/spec.go +++ b/dynfunc/spec.go @@ -1,4 +1,4 @@ -package dynamic +package dynfunc import ( "fmt" diff --git a/dynamic/spec_test.go b/dynfunc/spec_test.go similarity index 99% rename from dynamic/spec_test.go rename to dynfunc/spec_test.go index 0ce92bc..79ccfe3 100644 --- a/dynamic/spec_test.go +++ b/dynfunc/spec_test.go @@ -1,4 +1,4 @@ -package dynamic +package dynfunc import ( "errors" diff --git a/dynamic/types.go b/dynfunc/types.go similarity index 92% rename from dynamic/types.go rename to dynfunc/types.go index 041ddb9..3b0aac8 100644 --- a/dynamic/types.go +++ b/dynfunc/types.go @@ -1,4 +1,4 @@ -package dynamic +package dynfunc import "reflect" diff --git a/handler.go b/handler.go deleted file mode 100644 index 16af6ca..0000000 --- a/handler.go +++ /dev/null @@ -1,32 +0,0 @@ -package aicra - -import ( - "fmt" - "strings" - - "git.xdrm.io/go/aicra/dynamic" - "git.xdrm.io/go/aicra/internal/config" -) - -type handler struct { - Method string - Path string - dynHandler *dynamic.Handler -} - -// createHandler builds a handler from its http method and path -// also it checks whether the function signature is valid -func createHandler(method, path string, service config.Service, fn interface{}) (*handler, error) { - method = strings.ToUpper(method) - - dynHandler, err := dynamic.Build(fn, service) - if err != nil { - return nil, fmt.Errorf("%s '%s' handler: %w", method, path, err) - } - - return &handler{ - Path: path, - Method: method, - dynHandler: dynHandler, - }, nil -} diff --git a/server.go b/server.go index 7bfa323..9dcae18 100644 --- a/server.go +++ b/server.go @@ -6,13 +6,20 @@ import ( "os" "git.xdrm.io/go/aicra/datatype" + "git.xdrm.io/go/aicra/dynfunc" "git.xdrm.io/go/aicra/internal/config" ) // Server represents an AICRA instance featuring: type checkers, services type Server struct { config *config.Server - handlers []*handler + handlers []*apiHandler +} + +type apiHandler struct { + Method string + Path string + dynHandler *dynfunc.Handler } // New creates a framework instance from a configuration file @@ -48,22 +55,29 @@ func New(configPath string, dtypes ...datatype.T) (*Server, error) { // Handle sets a new handler for an HTTP method to a path func (s *Server) Handle(method, path string, fn interface{}) error { // find associated service - var found *config.Service = nil - for _, service := range s.config.Services { - if method == service.Method && path == service.Pattern { - found = service + var service *config.Service + for _, s := range s.config.Services { + if method == s.Method && path == s.Pattern { + service = s break } } - if found == nil { - return fmt.Errorf("%s '%s': %w", method, path, ErrNoServiceForHandler) + + if service == nil { + return fmt.Errorf("%s '%s': %w", method, path, ErrUnknownService) } - handler, err := createHandler(method, path, *found, fn) + dynHandler, err := dynfunc.Build(fn, *service) if err != nil { - return err + return fmt.Errorf("%s '%s' handler: %w", method, path, err) } - s.handlers = append(s.handlers, handler) + + s.handlers = append(s.handlers, &apiHandler{ + Path: path, + Method: method, + dynHandler: dynHandler, + }) + return nil }