rename package 'request' -> 'apirequest' ; 'config' -> 'controller' + main types.go bundled into server.go + made attributes non-exported (lowercase)

This commit is contained in:
Adrien Marquès 2018-07-11 01:36:42 +02:00
parent 61b3c6c91c
commit 1aee9e92fd
11 changed files with 40 additions and 51 deletions

View File

@ -1,4 +1,4 @@
package request package apirequest
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package request package apirequest
// Parse parameter (json-like) if not already done // Parse parameter (json-like) if not already done
func (i *Parameter) Parse() { func (i *Parameter) Parse() {

View File

@ -1,4 +1,4 @@
package request package apirequest
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package request package apirequest
type Request struct { type Request struct {
// corresponds to the list of uri components // corresponds to the list of uri components
@ -17,12 +17,12 @@ type DataSet struct {
// ordered values from the URI // ordered values from the URI
// catches all after the controller path // catches all after the controller path
// //
// points to Request.Data // points to DataSet.Data
URI []*Parameter URI []*Parameter
// uri parameters following the QUERY format // uri parameters following the QUERY format
// //
// points to Request.Data // points to DataSet.Data
Get map[string]*Parameter Get map[string]*Parameter
// form data depending on the Content-Type: // form data depending on the Content-Type:
@ -30,7 +30,7 @@ type DataSet struct {
// 'application/x-www-form-urlencoded' => standard parameters as QUERY parameters // 'application/x-www-form-urlencoded' => standard parameters as QUERY parameters
// 'multipart/form-data' => parse form-data format // 'multipart/form-data' => parse form-data format
// //
// points to Request.Data // points to DataSet.Data
Form map[string]*Parameter Form map[string]*Parameter
// contains URL+GET+FORM data with prefixes: // contains URL+GET+FORM data with prefixes:

View File

@ -1,4 +1,4 @@
package request package apirequest
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package config package controller
import ( import (
"encoding/json" "encoding/json"

View File

@ -1,4 +1,4 @@
package config package controller
import ( import (
"git.xdrm.io/go/aicra/middleware" "git.xdrm.io/go/aicra/middleware"

View File

@ -1,4 +1,4 @@
package config package controller
/* (1) Configuration /* (1) Configuration
---------------------------------------------------------*/ ---------------------------------------------------------*/

View File

@ -3,38 +3,45 @@ package aicra
import ( import (
"fmt" "fmt"
e "git.xdrm.io/go/aicra/err" 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/checker"
"git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/controller"
"git.xdrm.io/go/aicra/internal/request"
"git.xdrm.io/go/aicra/middleware" "git.xdrm.io/go/aicra/middleware"
"git.xdrm.io/go/aicra/response" "git.xdrm.io/go/aicra/response"
"log" "log"
"net/http" "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 // New creates a framework instance from a configuration file
func New(path string) (*Server, error) { func New(path string) (*Server, error) {
/* (1) Init instance */ /* (1) Init instance */
inst := &Server{ var err error
config: nil, var i *Server
Params: make(map[string]interface{}),
}
/* (2) Load configuration */ /* (2) Load configuration */
config, err := config.Load(path) i.controller, err = controller.Load(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
inst.config = config
/* (3) Default type registry */ /* (3) Default type registry */
inst.Checker = checker.CreateRegistry(".build/type") i.checker = checker.CreateRegistry(".build/type")
/* (4) Default middleware registry */ /* (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 { func (s *Server) Listen(port uint16) error {
/* (1) Bind router */ /* (1) Bind router */
http.HandleFunc("/", s.routeRequest) http.HandleFunc("/", s.manageRequest)
/* (2) Bind listener */ /* (2) Bind listener */
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil) return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
@ -50,16 +57,16 @@ func (s *Server) Listen(port uint16) error {
} }
// Router called for each request // 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 */ /* (1) Build request */
req, err := request.BuildFromHTTPRequest(httpReq) req, err := apirequest.BuildFromHTTPRequest(httpReq)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
/* (2) Middleware: authentication */ /* (2) Middleware: authentication */
scope := s.Middleware.Run(*httpReq) scope := s.middleware.Run(*httpReq)
/* (3) Find a matching controller */ /* (3) Find a matching controller */
controller := s.findController(req) 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 // extractParameters extracts parameters for the request and checks
// every single one according to configuration options // 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 // init vars
err := e.Success err := e.Success
@ -153,7 +160,7 @@ func (s *Server) extractParameters(req *request.Request, methodParam map[string]
/* (3) Optional & missing: set default value */ /* (3) Optional & missing: set default value */
if !isset { if !isset {
p = &request.Parameter{ p = &apirequest.Parameter{
Parsed: true, Parsed: true,
File: param.Type == "FILE", File: param.Type == "FILE",
Value: nil, Value: nil,
@ -188,7 +195,7 @@ func (s *Server) extractParameters(req *request.Request, methodParam map[string]
} }
/* (7) Check type */ /* (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 = e.InvalidParam
err.BindArgument(param.Rename) err.BindArgument(param.Rename)

View File

@ -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
}

View File

@ -3,17 +3,17 @@ package aicra
import ( import (
"encoding/json" "encoding/json"
"git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/err"
"git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/internal/apirequest"
"git.xdrm.io/go/aicra/internal/request" "git.xdrm.io/go/aicra/internal/controller"
"git.xdrm.io/go/aicra/response" "git.xdrm.io/go/aicra/response"
"log" "log"
"net/http" "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 */ /* (1) Try to browse by URI */
pathi, ctl := s.config.Browse(req.URI) pathi, ctl := s.controller.Browse(req.URI)
/* (2) Set controller uri */ /* (2) Set controller uri */
req.Path = make([]string, 0, pathi) req.Path = make([]string, 0, pathi)