add 'driver.Driver.Name() string' and use it to select the default type/middleware folder (.build/middleware or middleware) | fix driver.Generic.LoadMiddleware() | remove variadic arguments for middleware.CreateRegistry() and checker.CreateRegistry()
This commit is contained in:
parent
b957d82a64
commit
9c9079f06b
|
@ -10,6 +10,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (d *Generic) Name() string { return "generic" }
|
||||||
|
|
||||||
// RunController implements the Driver interface
|
// RunController implements the Driver interface
|
||||||
func (d *Generic) RunController(_path []string, _method string) (func(response.Arguments) response.Response, e.Error) {
|
func (d *Generic) RunController(_path []string, _method string) (func(response.Arguments) response.Response, e.Error) {
|
||||||
|
|
||||||
|
@ -114,24 +116,23 @@ func (d *Generic) LoadMiddleware(_path string) (func(http.Request, *[]string), e
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output, ok := outputI.(map[string]interface{})
|
/* (4) Get as []string */
|
||||||
|
scope, ok := outputI.([]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
|
fmt.Printf("3\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract 'scope' (empty by default or error)
|
/* (5) Try to add each value to the scope */
|
||||||
scopeOut, ok := output["scope"]
|
for _, v := range scope {
|
||||||
|
stringScope, ok := v.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
fmt.Printf("4\n")
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
*_scope = append(*_scope, stringScope)
|
||||||
scope, ok := scopeOut.([]string)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*_scope = append(*_scope, scope...)
|
|
||||||
|
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (d *Plugin) Name() string { return "plugin" }
|
||||||
|
|
||||||
// RunController implements the Driver interface
|
// RunController implements the Driver interface
|
||||||
func (d *Plugin) RunController(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) {
|
func (d *Plugin) RunController(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) {
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
// Driver defines the driver interface to load controller/middleware implementation or executables
|
// Driver defines the driver interface to load controller/middleware implementation or executables
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
|
Name() string
|
||||||
RunController(_path []string, _method string) (func(response.Arguments) response.Response, err.Error)
|
RunController(_path []string, _method string) (func(response.Arguments) response.Response, err.Error)
|
||||||
LoadMiddleware(_path string) (func(http.Request, *[]string), error)
|
LoadMiddleware(_path string) (func(http.Request, *[]string), error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package checker
|
package checker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -8,29 +9,26 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNoMatchingType = errors.New("no matching type")
|
||||||
|
var ErrDoesNotMatch = errors.New("does not match")
|
||||||
|
var ErrEmptyTypeName = errors.New("type name must not be empty")
|
||||||
|
|
||||||
// CreateRegistry creates an empty type registry
|
// CreateRegistry creates an empty type registry
|
||||||
// - if loadDir is True if will load all available types
|
func CreateRegistry(_folder string) *Registry {
|
||||||
// inside the local ./types folder
|
|
||||||
func CreateRegistry(loadDir ...string) *Registry {
|
|
||||||
|
|
||||||
/* (1) Create registry */
|
/* (1) Create registry */
|
||||||
reg := &Registry{
|
reg := &Registry{
|
||||||
Types: make([]Type, 0),
|
Types: make([]Type, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) If no default to use -> empty registry */
|
/* (2) List types */
|
||||||
if len(loadDir) < 1 {
|
files, err := ioutil.ReadDir(_folder)
|
||||||
return reg
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (3) List types */
|
|
||||||
plugins, err := ioutil.ReadDir(loadDir[0])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Else try to load each given default */
|
/* (3) Else try to load each given default */
|
||||||
for _, file := range plugins {
|
for _, file := range files {
|
||||||
|
|
||||||
// ignore non .so files
|
// ignore non .so files
|
||||||
if !strings.HasSuffix(file.Name(), ".so") {
|
if !strings.HasSuffix(file.Name(), ".so") {
|
||||||
|
@ -39,7 +37,7 @@ func CreateRegistry(loadDir ...string) *Registry {
|
||||||
|
|
||||||
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.Printf("cannot load plugin '%s'", file.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -54,7 +52,7 @@ func (reg *Registry) add(pluginName string) error {
|
||||||
|
|
||||||
/* (1) Check plugin name */
|
/* (1) Check plugin name */
|
||||||
if len(pluginName) < 1 {
|
if len(pluginName) < 1 {
|
||||||
return fmt.Errorf("Plugin name must not be empty")
|
return ErrEmptyTypeName
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Check if valid plugin name */
|
/* (2) Check if valid plugin name */
|
||||||
|
@ -123,12 +121,12 @@ func (reg Registry) Run(typeName string, value interface{}) error {
|
||||||
|
|
||||||
/* (2) Abort if no matching type */
|
/* (2) Abort if no matching type */
|
||||||
if T == nil {
|
if T == nil {
|
||||||
return fmt.Errorf("No matching type")
|
return ErrNoMatchingType
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Check */
|
/* (3) Check */
|
||||||
if !T.Check(value) {
|
if !T.Check(value) {
|
||||||
return fmt.Errorf("Does not match")
|
return ErrDoesNotMatch
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -92,9 +92,9 @@ func FetchFormData(req *http.Request) map[string]interface{} {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadController tries to load a controller from its uri
|
// RunController tries to load a controller from its uri
|
||||||
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
// checks for its given method ('Get', 'Post', 'Put', or 'Delete')
|
||||||
func (i *Request) LoadController(_method string, _driver driver.Driver) (func(response.Arguments) response.Response, err.Error) {
|
func (i *Request) RunController(_method string, _driver driver.Driver) (func(response.Arguments) response.Response, err.Error) {
|
||||||
|
|
||||||
return _driver.RunController(i.Path, _method)
|
return _driver.RunController(i.Path, _method)
|
||||||
|
|
||||||
|
|
|
@ -9,32 +9,25 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateRegistry creates an empty middleware registry
|
// CreateRegistry creates an empty middleware registry
|
||||||
// - if loadDir is set -> load all available middlewares
|
func CreateRegistry(_driver driver.Driver, _folder string) *Registry {
|
||||||
// inside the local ./middleware folder
|
|
||||||
func CreateRegistry(_driver driver.Driver, loadDir ...string) *Registry {
|
|
||||||
|
|
||||||
/* (1) Create registry */
|
/* (1) Create registry */
|
||||||
reg := &Registry{
|
reg := &Registry{
|
||||||
Middlewares: make([]*Wrapper, 0),
|
Middlewares: make([]*Wrapper, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) If no default to use -> empty registry */
|
/* (2) List middleware files */
|
||||||
if len(loadDir) < 1 {
|
files, err := ioutil.ReadDir(_folder)
|
||||||
return reg
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (3) List middleware files */
|
|
||||||
files, err := ioutil.ReadDir(loadDir[0])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Else try to load each given default */
|
/* (3) Else try to load each given default */
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
|
||||||
mwFunc, err := _driver.LoadMiddleware(path.Join(loadDir[0], file.Name()))
|
mwFunc, err := _driver.LoadMiddleware(path.Join(_folder, file.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Cannot load middleware '%s' | %s", file.Name(), err)
|
log.Printf("cannot load middleware '%s' | %s", file.Name(), err)
|
||||||
}
|
}
|
||||||
reg.Middlewares = append(reg.Middlewares, &Wrapper{Inspect: mwFunc})
|
reg.Middlewares = append(reg.Middlewares, &Wrapper{Inspect: mwFunc})
|
||||||
|
|
||||||
|
|
25
server.go
25
server.go
|
@ -1,7 +1,6 @@
|
||||||
package aicra
|
package aicra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"git.xdrm.io/go/aicra/driver"
|
"git.xdrm.io/go/aicra/driver"
|
||||||
e "git.xdrm.io/go/aicra/err"
|
e "git.xdrm.io/go/aicra/err"
|
||||||
"git.xdrm.io/go/aicra/internal/checker"
|
"git.xdrm.io/go/aicra/internal/checker"
|
||||||
|
@ -23,14 +22,24 @@ type Server struct {
|
||||||
driver driver.Driver
|
driver driver.Driver
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrNilDriver is raised when a NULL driver is given to the constructor
|
|
||||||
var ErrNilDriver = errors.New("the driver is <nil>")
|
|
||||||
|
|
||||||
// New creates a framework instance from a configuration file
|
// New creates a framework instance from a configuration file
|
||||||
|
// _path is the json configuration path
|
||||||
|
// _driver is used to load/run the controllers and middlewares (default: )
|
||||||
|
//
|
||||||
func New(_path string, _driver driver.Driver) (*Server, error) {
|
func New(_path string, _driver driver.Driver) (*Server, error) {
|
||||||
|
|
||||||
|
/* (1) Default driver : Plugin */
|
||||||
if _driver == nil {
|
if _driver == nil {
|
||||||
return nil, ErrNilDriver
|
_driver = &driver.Plugin{}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (2) Default folders according to drivers */
|
||||||
|
_folders := make([]string, 0, 2)
|
||||||
|
_folders = append(_folders, ".build/type")
|
||||||
|
if _driver.Name() == "plugin" { // plugin
|
||||||
|
_folders = append(_folders, ".build/middleware")
|
||||||
|
} else { // generic
|
||||||
|
_folders = append(_folders, "middleware")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (1) Init instance */
|
/* (1) Init instance */
|
||||||
|
@ -47,10 +56,10 @@ func New(_path string, _driver driver.Driver) (*Server, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Default type registry */
|
/* (3) Default type registry */
|
||||||
i.checker = checker.CreateRegistry(".build/type")
|
i.checker = checker.CreateRegistry(_folders[0])
|
||||||
|
|
||||||
/* (4) Default middleware registry */
|
/* (4) Default middleware registry */
|
||||||
i.middleware = middleware.CreateRegistry(_driver, ".build/middleware")
|
i.middleware = middleware.CreateRegistry(_driver, _folders[1])
|
||||||
|
|
||||||
return i, nil
|
return i, nil
|
||||||
|
|
||||||
|
@ -100,7 +109,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
/* (5) Load controller
|
/* (5) Load controller
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
controllerImplementation, callErr := apiRequest.LoadController(req.Method, s.driver)
|
controllerImplementation, callErr := apiRequest.RunController(req.Method, s.driver)
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue