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)

This commit is contained in:
Adrien Marquès 2018-07-11 01:51:10 +02:00
parent 1aee9e92fd
commit 05d5c23038
5 changed files with 21 additions and 22 deletions

View File

@ -21,5 +21,5 @@ func getAbsPath(base string, path string) (string, error) {
// Returns whether a directory exists for the path @path // Returns whether a directory exists for the path @path
func dirExists(path string) bool { func dirExists(path string) bool {
stat, err := os.Stat(path) stat, err := os.Stat(path)
return err != nil || !stat.IsDir() return err == nil && stat.IsDir()
} }

View File

@ -37,7 +37,7 @@ func CreateRegistry(loadDir ...string) *Registry {
continue continue
} }
err := reg.Add(file.Name()) err := reg.add(file.Name())
if err != nil { if err != nil {
log.Fatalf("Cannot load plugin '%s'", file.Name()) log.Fatalf("Cannot load plugin '%s'", file.Name())
} }
@ -47,10 +47,10 @@ func CreateRegistry(loadDir ...string) *Registry {
return reg 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 // valid and existing plugin name with or without the .so extension
// it must be located in the relative directory ./types // 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 */ /* (1) Check plugin name */
if len(pluginName) < 1 { if len(pluginName) < 1 {

View File

@ -38,7 +38,7 @@ func CreateRegistry(loadDir ...string) *Registry {
continue continue
} }
err := reg.Add(file.Name()) err := reg.add(file.Name())
if err != nil { if err != nil {
log.Fatalf("Cannot load plugin '%s'", file.Name()) log.Fatalf("Cannot load plugin '%s'", file.Name())
} }
@ -48,10 +48,10 @@ func CreateRegistry(loadDir ...string) *Registry {
return reg 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 // valid and existing plugin name with or without the .so extension
// it must be located in the relative directory .build/middleware // 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 */ /* (1) Check plugin name */
if len(pluginName) < 1 { if len(pluginName) < 1 {

View File

@ -27,7 +27,9 @@ func New(path string) (*Server, error) {
/* (1) Init instance */ /* (1) Init instance */
var err error var err error
var i *Server var i = &Server{
controller: nil,
}
/* (2) Load configuration */ /* (2) Load configuration */
i.controller, err = controller.Load(path) i.controller, err = controller.Load(path)
@ -57,25 +59,25 @@ func (s *Server) Listen(port uint16) error {
} }
// Router called for each request // 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 */ /* (1) Build request */
req, err := apirequest.BuildFromHTTPRequest(httpReq) apiRequest, err := apirequest.BuildFromHTTPRequest(req)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
/* (2) Middleware: authentication */ /* (2) Launch middlewares to build the scope */
scope := s.middleware.Run(*httpReq) scope := s.middleware.Run(*req)
/* (3) Find a matching controller */ /* (3) Find a matching controller */
controller := s.findController(req) controller := s.matchController(apiRequest)
if controller == nil { if controller == nil {
return return
} }
/* (4) Check if matching method exists */ /* (4) Check if matching method exists */
var method = controller.Method(httpReq.Method) var method = controller.Method(req.Method)
if method == nil { if method == nil {
httpError(res, e.UnknownMethod) httpError(res, e.UnknownMethod)
@ -90,7 +92,7 @@ func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) {
/* (4) Check parameters /* (4) Check parameters
---------------------------------------------------------*/ ---------------------------------------------------------*/
parameters, paramError := s.extractParameters(req, method.Parameters) parameters, paramError := s.extractParameters(apiRequest, method.Parameters)
// Fail if argument check failed // Fail if argument check failed
if paramError.Code != e.Success.Code { if paramError.Code != e.Success.Code {
@ -100,7 +102,7 @@ func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) {
/* (5) Load controller /* (5) Load controller
---------------------------------------------------------*/ ---------------------------------------------------------*/
callable, callErr := req.LoadController(httpReq.Method) controllerImplementation, callErr := apiRequest.LoadController(req.Method)
if callErr.Code != e.Success.Code { if callErr.Code != e.Success.Code {
httpError(res, callErr) httpError(res, callErr)
log.Printf("[err] %s\n", err) 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 /* (6) Execute and get response
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Give Authorization header into controller */ /* (1) Give Authorization header into controller */
authHeader := httpReq.Header.Get("Authorization") parameters["_AUTHORIZATION_"] = req.Header.Get("Authorization")
if len(authHeader) > 0 {
parameters["_AUTHORIZATION_"] = authHeader
}
/* (2) Give Scope into controller */ /* (2) Give Scope into controller */
parameters["_SCOPE_"] = scope parameters["_SCOPE_"] = scope
/* (3) Execute */ /* (3) Execute */
response := callable(parameters, response.New()) response := controllerImplementation(parameters, response.New())
/* (4) Extract http headers */ /* (4) Extract http headers */
for k, v := range response.Dump() { for k, v := range response.Dump() {

View File

@ -10,7 +10,7 @@ import (
"net/http" "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 */ /* (1) Try to browse by URI */
pathi, ctl := s.controller.Browse(req.URI) pathi, ctl := s.controller.Browse(req.URI)