From 05d5c230380521179890fe35020bc751f3b34197 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 11 Jul 2018 01:51:10 +0200 Subject: [PATCH] fix aicra.files.IsDir() which logic was inverted | make add() private for registries (checker + middleware) + fix server.go (invalid address for Server.controller because wasn't initialized to NIL) --- cmd/aicra/files.go | 2 +- internal/checker/public.go | 6 +++--- middleware/public.go | 6 +++--- server.go | 27 +++++++++++++-------------- util.go | 2 +- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cmd/aicra/files.go b/cmd/aicra/files.go index 79292f4..68d6f57 100644 --- a/cmd/aicra/files.go +++ b/cmd/aicra/files.go @@ -21,5 +21,5 @@ func getAbsPath(base string, path string) (string, error) { // Returns whether a directory exists for the path @path func dirExists(path string) bool { stat, err := os.Stat(path) - return err != nil || !stat.IsDir() + return err == nil && stat.IsDir() } diff --git a/internal/checker/public.go b/internal/checker/public.go index a050d96..c361b3a 100644 --- a/internal/checker/public.go +++ b/internal/checker/public.go @@ -37,7 +37,7 @@ func CreateRegistry(loadDir ...string) *Registry { continue } - err := reg.Add(file.Name()) + err := reg.add(file.Name()) if err != nil { log.Fatalf("Cannot load plugin '%s'", file.Name()) } @@ -47,10 +47,10 @@ func CreateRegistry(loadDir ...string) *Registry { return reg } -// Add adds a type to the registry; it must be a +// add adds a type to the registry; it must be a // valid and existing plugin name with or without the .so extension // it must be located in the relative directory ./types -func (reg *Registry) Add(pluginName string) error { +func (reg *Registry) add(pluginName string) error { /* (1) Check plugin name */ if len(pluginName) < 1 { diff --git a/middleware/public.go b/middleware/public.go index 8e96d67..58cc8f5 100644 --- a/middleware/public.go +++ b/middleware/public.go @@ -38,7 +38,7 @@ func CreateRegistry(loadDir ...string) *Registry { continue } - err := reg.Add(file.Name()) + err := reg.add(file.Name()) if err != nil { log.Fatalf("Cannot load plugin '%s'", file.Name()) } @@ -48,10 +48,10 @@ func CreateRegistry(loadDir ...string) *Registry { return reg } -// Add adds a middleware to the registry; it must be a +// add adds a middleware to the registry; it must be a // valid and existing plugin name with or without the .so extension // it must be located in the relative directory .build/middleware -func (reg *Registry) Add(pluginName string) error { +func (reg *Registry) add(pluginName string) error { /* (1) Check plugin name */ if len(pluginName) < 1 { diff --git a/server.go b/server.go index 18113fd..bcd6ebc 100644 --- a/server.go +++ b/server.go @@ -27,7 +27,9 @@ func New(path string) (*Server, error) { /* (1) Init instance */ var err error - var i *Server + var i = &Server{ + controller: nil, + } /* (2) Load configuration */ i.controller, err = controller.Load(path) @@ -57,25 +59,25 @@ func (s *Server) Listen(port uint16) error { } // Router called for each request -func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) { +func (s *Server) manageRequest(res http.ResponseWriter, req *http.Request) { /* (1) Build request */ - req, err := apirequest.BuildFromHTTPRequest(httpReq) + apiRequest, err := apirequest.BuildFromHTTPRequest(req) if err != nil { log.Fatal(err) } - /* (2) Middleware: authentication */ - scope := s.middleware.Run(*httpReq) + /* (2) Launch middlewares to build the scope */ + scope := s.middleware.Run(*req) /* (3) Find a matching controller */ - controller := s.findController(req) + controller := s.matchController(apiRequest) if controller == nil { return } /* (4) Check if matching method exists */ - var method = controller.Method(httpReq.Method) + var method = controller.Method(req.Method) if method == nil { httpError(res, e.UnknownMethod) @@ -90,7 +92,7 @@ func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) { /* (4) Check parameters ---------------------------------------------------------*/ - parameters, paramError := s.extractParameters(req, method.Parameters) + parameters, paramError := s.extractParameters(apiRequest, method.Parameters) // Fail if argument check failed if paramError.Code != e.Success.Code { @@ -100,7 +102,7 @@ func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) { /* (5) Load controller ---------------------------------------------------------*/ - callable, callErr := req.LoadController(httpReq.Method) + controllerImplementation, callErr := apiRequest.LoadController(req.Method) if callErr.Code != e.Success.Code { httpError(res, callErr) log.Printf("[err] %s\n", err) @@ -110,16 +112,13 @@ func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) { /* (6) Execute and get response ---------------------------------------------------------*/ /* (1) Give Authorization header into controller */ - authHeader := httpReq.Header.Get("Authorization") - if len(authHeader) > 0 { - parameters["_AUTHORIZATION_"] = authHeader - } + parameters["_AUTHORIZATION_"] = req.Header.Get("Authorization") /* (2) Give Scope into controller */ parameters["_SCOPE_"] = scope /* (3) Execute */ - response := callable(parameters, response.New()) + response := controllerImplementation(parameters, response.New()) /* (4) Extract http headers */ for k, v := range response.Dump() { diff --git a/util.go b/util.go index 345dfd0..f27fbc9 100644 --- a/util.go +++ b/util.go @@ -10,7 +10,7 @@ import ( "net/http" ) -func (s *Server) findController(req *apirequest.Request) *controller.Controller { +func (s *Server) matchController(req *apirequest.Request) *controller.Controller { /* (1) Try to browse by URI */ pathi, ctl := s.controller.Browse(req.URI)