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 (
"encoding/json"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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 (
"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)