diff --git a/internal/request/dataset.go b/internal/apirequest/dataset.go similarity index 99% rename from internal/request/dataset.go rename to internal/apirequest/dataset.go index ad2ac67..7dec98a 100644 --- a/internal/request/dataset.go +++ b/internal/apirequest/dataset.go @@ -1,4 +1,4 @@ -package request +package apirequest import ( "encoding/json" diff --git a/internal/request/parameter.go b/internal/apirequest/parameter.go similarity index 92% rename from internal/request/parameter.go rename to internal/apirequest/parameter.go index 0d21d83..e5e4398 100644 --- a/internal/request/parameter.go +++ b/internal/apirequest/parameter.go @@ -1,4 +1,4 @@ -package request +package apirequest // Parse parameter (json-like) if not already done func (i *Parameter) Parse() { diff --git a/internal/request/request.go b/internal/apirequest/request.go similarity index 99% rename from internal/request/request.go rename to internal/apirequest/request.go index ca008f0..69beb27 100644 --- a/internal/request/request.go +++ b/internal/apirequest/request.go @@ -1,4 +1,4 @@ -package request +package apirequest import ( "encoding/json" diff --git a/internal/request/types.go b/internal/apirequest/types.go similarity index 93% rename from internal/request/types.go rename to internal/apirequest/types.go index 511ce71..88d5922 100644 --- a/internal/request/types.go +++ b/internal/apirequest/types.go @@ -1,4 +1,4 @@ -package request +package apirequest type Request struct { // corresponds to the list of uri components @@ -17,12 +17,12 @@ type DataSet struct { // ordered values from the URI // catches all after the controller path // - // points to Request.Data + // points to DataSet.Data URI []*Parameter // uri parameters following the QUERY format // - // points to Request.Data + // points to DataSet.Data Get map[string]*Parameter // form data depending on the Content-Type: @@ -30,7 +30,7 @@ type DataSet struct { // 'application/x-www-form-urlencoded' => standard parameters as QUERY parameters // 'multipart/form-data' => parse form-data format // - // points to Request.Data + // points to DataSet.Data Form map[string]*Parameter // contains URL+GET+FORM data with prefixes: diff --git a/internal/request/utils.go b/internal/apirequest/utils.go similarity index 99% rename from internal/request/utils.go rename to internal/apirequest/utils.go index 6af5abb..56be8e5 100644 --- a/internal/request/utils.go +++ b/internal/apirequest/utils.go @@ -1,4 +1,4 @@ -package request +package apirequest import ( "encoding/json" diff --git a/internal/config/controller.go b/internal/controller/controller.go similarity index 99% rename from internal/config/controller.go rename to internal/controller/controller.go index 3f4f512..849d3e3 100644 --- a/internal/config/controller.go +++ b/internal/controller/controller.go @@ -1,4 +1,4 @@ -package config +package controller import ( "encoding/json" diff --git a/internal/config/method.go b/internal/controller/method.go similarity index 97% rename from internal/config/method.go rename to internal/controller/method.go index 03fb08e..9d0d7b8 100644 --- a/internal/config/method.go +++ b/internal/controller/method.go @@ -1,4 +1,4 @@ -package config +package controller import ( "git.xdrm.io/go/aicra/middleware" diff --git a/internal/config/types.go b/internal/controller/types.go similarity index 97% rename from internal/config/types.go rename to internal/controller/types.go index 87f7bb6..340d743 100644 --- a/internal/config/types.go +++ b/internal/controller/types.go @@ -1,4 +1,4 @@ -package config +package controller /* (1) Configuration ---------------------------------------------------------*/ diff --git a/server.go b/server.go index f35255f..18113fd 100644 --- a/server.go +++ b/server.go @@ -3,38 +3,45 @@ package aicra import ( "fmt" e "git.xdrm.io/go/aicra/err" + "git.xdrm.io/go/aicra/internal/apirequest" "git.xdrm.io/go/aicra/internal/checker" - "git.xdrm.io/go/aicra/internal/config" - "git.xdrm.io/go/aicra/internal/request" + "git.xdrm.io/go/aicra/internal/controller" "git.xdrm.io/go/aicra/middleware" "git.xdrm.io/go/aicra/response" "log" "net/http" ) +// Server represents an AICRA instance featuring: +// * its type checkers +// * its middlewares +// * its controllers (config) +type Server struct { + controller *controller.Controller // controllers + checker *checker.Registry // type checker registry + middleware *middleware.Registry // middlewares +} + // New creates a framework instance from a configuration file func New(path string) (*Server, error) { /* (1) Init instance */ - inst := &Server{ - config: nil, - Params: make(map[string]interface{}), - } + var err error + var i *Server /* (2) Load configuration */ - config, err := config.Load(path) + i.controller, err = controller.Load(path) if err != nil { return nil, err } - inst.config = config /* (3) Default type registry */ - inst.Checker = checker.CreateRegistry(".build/type") + i.checker = checker.CreateRegistry(".build/type") /* (4) Default middleware registry */ - inst.Middleware = middleware.CreateRegistry(".build/middleware") + i.middleware = middleware.CreateRegistry(".build/middleware") - return inst, nil + return i, nil } @@ -42,7 +49,7 @@ func New(path string) (*Server, error) { func (s *Server) Listen(port uint16) error { /* (1) Bind router */ - http.HandleFunc("/", s.routeRequest) + http.HandleFunc("/", s.manageRequest) /* (2) Bind listener */ return http.ListenAndServe(fmt.Sprintf(":%d", port), nil) @@ -50,16 +57,16 @@ func (s *Server) Listen(port uint16) error { } // Router called for each request -func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) { +func (s *Server) manageRequest(res http.ResponseWriter, httpReq *http.Request) { /* (1) Build request */ - req, err := request.BuildFromHTTPRequest(httpReq) + req, err := apirequest.BuildFromHTTPRequest(httpReq) if err != nil { log.Fatal(err) } /* (2) Middleware: authentication */ - scope := s.Middleware.Run(*httpReq) + scope := s.middleware.Run(*httpReq) /* (3) Find a matching controller */ controller := s.findController(req) @@ -132,7 +139,7 @@ func (s *Server) routeRequest(res http.ResponseWriter, httpReq *http.Request) { // 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 *apirequest.Request, methodParam map[string]*controller.Parameter) (map[string]interface{}, e.Error) { // init vars err := e.Success @@ -153,7 +160,7 @@ func (s *Server) extractParameters(req *request.Request, methodParam map[string] /* (3) Optional & missing: set default value */ if !isset { - p = &request.Parameter{ + p = &apirequest.Parameter{ Parsed: true, File: param.Type == "FILE", Value: nil, @@ -188,7 +195,7 @@ func (s *Server) extractParameters(req *request.Request, methodParam map[string] } /* (7) Check type */ - if s.Checker.Run(param.Type, p.Value) != nil { + if s.checker.Run(param.Type, p.Value) != nil { err = e.InvalidParam err.BindArgument(param.Rename) diff --git a/types.go b/types.go deleted file mode 100644 index 6cd71d3..0000000 --- a/types.go +++ /dev/null @@ -1,18 +0,0 @@ -package aicra - -import ( - "git.xdrm.io/go/aicra/internal/checker" - "git.xdrm.io/go/aicra/internal/config" - "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{} - Checker *checker.Registry // type check - Middleware *middleware.Registry // middlewares -} diff --git a/util.go b/util.go index 81ed309..345dfd0 100644 --- a/util.go +++ b/util.go @@ -3,17 +3,17 @@ package aicra import ( "encoding/json" "git.xdrm.io/go/aicra/err" - "git.xdrm.io/go/aicra/internal/config" - "git.xdrm.io/go/aicra/internal/request" + "git.xdrm.io/go/aicra/internal/apirequest" + "git.xdrm.io/go/aicra/internal/controller" "git.xdrm.io/go/aicra/response" "log" "net/http" ) -func (s *Server) findController(req *request.Request) *config.Controller { +func (s *Server) findController(req *apirequest.Request) *controller.Controller { /* (1) Try to browse by URI */ - pathi, ctl := s.config.Browse(req.URI) + pathi, ctl := s.controller.Browse(req.URI) /* (2) Set controller uri */ req.Path = make([]string, 0, pathi)