From 6ebdd61325cf964c6d8b83582298a6f4adc97e19 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 9 Jul 2018 01:00:45 +0200 Subject: [PATCH] LINT + Server.extractParameters is now private + add management for errors : 'UncallableController' and 'UncallableMethod' --- internal/request/request.go | 11 ++++++----- server.go | 15 ++++++++------- types.go | 4 ++++ util.go | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/request/request.go b/internal/request/request.go index c1ebfc7..d521e03 100644 --- a/internal/request/request.go +++ b/internal/request/request.go @@ -3,6 +3,7 @@ package request import ( "encoding/json" "fmt" + "git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/response" "log" "net/http" @@ -93,7 +94,7 @@ func FetchFormData(req *http.Request) map[string]interface{} { // LoadController tries to load a controller from its uri // checks for its given method ('Get', 'Post', 'Put', or 'Delete') -func (i *Request) LoadController(method string) (func(response.Arguments, *response.Response) response.Response, error) { +func (i *Request) LoadController(method string) (func(response.Arguments, *response.Response) response.Response, err.Error) { /* (1) Build controller path */ path := strings.Join(i.Path, "-") @@ -111,21 +112,21 @@ func (i *Request) LoadController(method string) (func(response.Arguments, *respo /* (2) Try to load plugin */ p, err2 := plugin.Open(path) if err2 != nil { - return nil, err2 + return nil, err.UncallableController } /* (3) Try to extract method */ m, err2 := p.Lookup(method) if err2 != nil { - return nil, err2 + return nil, err.UncallableMethod } /* (4) Check signature */ callable, validSignature := m.(func(response.Arguments, *response.Response) response.Response) if !validSignature { - return nil, fmt.Errorf("Invalid signature for method %s", method) + return nil, err.UncallableMethod } - return callable, nil + return callable, err.Success } diff --git a/server.go b/server.go index 8eb176c..66c0979 100644 --- a/server.go +++ b/server.go @@ -38,7 +38,7 @@ func New(path string) (*Server, error) { } -// Listens and binds the server to the given port +// Listen binds the server to the given port func (s *Server) Listen(port uint16) error { /* (1) Bind router */ @@ -83,7 +83,7 @@ func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) { /* (4) Check parameters ---------------------------------------------------------*/ - parameters, paramError := s.ExtractParameters(req, method.Parameters) + parameters, paramError := s.extractParameters(req, method.Parameters) // Fail if argument check failed if paramError.Code != e.Success.Code { @@ -93,8 +93,9 @@ func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) { /* (5) Load controller ---------------------------------------------------------*/ - callable, err := req.LoadController(httpReq.Method) - if err != nil { + callable, callErr := req.LoadController(httpReq.Method) + if callErr.Code != e.Success.Code { + httpError(res, callErr) log.Printf("[err] %s\n", err) return } @@ -129,12 +130,12 @@ func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) { } -// ExtractParameters extracts parameters for the request and checks +// extractParameters extracts parameters for the request and checks // every single one according to configuration options -func (s *Server) ExtractParameters(req *request.Request, methodParam map[string]*config.Parameter) (map[string]interface{}, e.Error) { +func (s *Server) extractParameters(req *request.Request, methodParam map[string]*config.Parameter) (map[string]interface{}, e.Error) { // init vars - var err e.Error = e.Success + err := e.Success parameters := make(map[string]interface{}) // for each param of the config diff --git a/types.go b/types.go index 5d05943..931459f 100644 --- a/types.go +++ b/types.go @@ -6,6 +6,10 @@ import ( "git.xdrm.io/go/aicra/middleware" ) +// Server represents an AICRA instance featuring: +// * its type checkers +// * its middlewares +// * its configuration (controllers) type Server struct { config *config.Controller Params map[string]interface{} diff --git a/util.go b/util.go index e3aceb1..812c51b 100644 --- a/util.go +++ b/util.go @@ -55,8 +55,8 @@ func httpPrint(r http.ResponseWriter, res response.Response) { // Prints an error as HTTP response func httpError(r http.ResponseWriter, e err.Error) { - Json, _ := e.MarshalJSON() + JSON, _ := e.MarshalJSON() r.Header().Add("Content-Type", "application/json") - r.Write(Json) + r.Write(JSON) log.Printf("[http.fail] %s\n", e.Reason) }