minfix in 'AICRA' command | plugins are now using the Export() method to return Controller/Middleware
This commit is contained in:
parent
7bcbefdf35
commit
253a2b0b59
|
@ -97,7 +97,6 @@ func main() {
|
||||||
build := schema.Driver.Build(schema.Root, schema.Controllers.Folder, upath)
|
build := schema.Driver.Build(schema.Root, schema.Controllers.Folder, upath)
|
||||||
|
|
||||||
compile(source, build)
|
compile(source, build)
|
||||||
fmt.Printf("\n")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,9 +44,7 @@ func (d *Plugin) RunController(_path []string, _method string) (func(response.Ar
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Format url */
|
/* (2) Format url */
|
||||||
tmp := []byte(strings.ToLower(_method))
|
method := strings.ToLower(_method)
|
||||||
tmp[0] = tmp[0] - ('a' - 'A')
|
|
||||||
method := string(tmp)
|
|
||||||
|
|
||||||
/* (2) Try to load plugin */
|
/* (2) Try to load plugin */
|
||||||
p, err2 := plugin.Open(path)
|
p, err2 := plugin.Open(path)
|
||||||
|
@ -54,19 +52,34 @@ func (d *Plugin) RunController(_path []string, _method string) (func(response.Ar
|
||||||
return nil, err.UncallableController
|
return nil, err.UncallableController
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (3) Try to extract method */
|
/* (3) Try to extract exported field */
|
||||||
m, err2 := p.Lookup(method)
|
m, err2 := p.Lookup("Export")
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return nil, err.UncallableMethod
|
return nil, err.UncallableController
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exported, ok := m.(func() Controller)
|
||||||
|
if !ok {
|
||||||
|
return nil, err.UncallableController
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (4) Controller */
|
||||||
|
ctl := exported()
|
||||||
|
|
||||||
/* (4) Check signature */
|
/* (4) Check signature */
|
||||||
callable, validSignature := m.(func(response.Arguments) response.Response)
|
switch method {
|
||||||
if !validSignature {
|
case "get":
|
||||||
return nil, err.UncallableMethod
|
return ctl.Get, err.Success
|
||||||
|
case "post":
|
||||||
|
return ctl.Post, err.Success
|
||||||
|
case "put":
|
||||||
|
return ctl.Put, err.Success
|
||||||
|
case "delete":
|
||||||
|
return ctl.Delete, err.Success
|
||||||
}
|
}
|
||||||
|
fmt.Printf("method: %s\n", method)
|
||||||
|
|
||||||
return callable, err.Success
|
return nil, err.UncallableMethod
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadMiddleware returns a new middleware function; it must be a
|
// LoadMiddleware returns a new middleware function; it must be a
|
||||||
|
@ -94,18 +107,17 @@ func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), er
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Export wanted properties */
|
/* (4) Extract exported fields */
|
||||||
inspect, err := p.Lookup("Inspect")
|
mw, err := p.Lookup("Export")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Missing method 'Inspect()'; %s", err)
|
return nil, fmt.Errorf("Missing method 'Inspect()'; %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (5) Cast Inspect */
|
exported, ok := mw.(func() Middleware)
|
||||||
mware, ok := inspect.(func(http.Request, *[]string))
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("Inspect() is malformed")
|
return nil, fmt.Errorf("Inspect() is malformed")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (6) Add type to registry */
|
/* (5) Return Inspect method */
|
||||||
return mware, nil
|
return exported().Inspect, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,6 @@ import (
|
||||||
// purposes, the type is always used as its definition ([]string)
|
// purposes, the type is always used as its definition ([]string)
|
||||||
type Scope []string
|
type Scope []string
|
||||||
|
|
||||||
type MiddlewareFunc func(http.Request, *[]string)
|
|
||||||
|
|
||||||
// Middleware updates the @Scope passed to it according to
|
|
||||||
// the @http.Request
|
|
||||||
type Middleware interface {
|
|
||||||
Inspect(http.Request, *[]string)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wrapper is a struct that stores middleware Inspect() method
|
// Wrapper is a struct that stores middleware Inspect() method
|
||||||
type Wrapper struct {
|
type Wrapper struct {
|
||||||
Inspect func(http.Request, *[]string)
|
Inspect func(http.Request, *[]string)
|
||||||
|
|
24
server.go
24
server.go
|
@ -1,10 +1,10 @@
|
||||||
package aicra
|
package aicra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"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/api"
|
"git.xdrm.io/go/aicra/internal/api"
|
||||||
"git.xdrm.io/go/aicra/internal/checker"
|
"git.xdrm.io/go/aicra/internal/checker"
|
||||||
|
"git.xdrm.io/go/aicra/internal/meta"
|
||||||
apirequest "git.xdrm.io/go/aicra/internal/request"
|
apirequest "git.xdrm.io/go/aicra/internal/request"
|
||||||
"git.xdrm.io/go/aicra/middleware"
|
"git.xdrm.io/go/aicra/middleware"
|
||||||
"log"
|
"log"
|
||||||
|
@ -19,34 +19,34 @@ type Server struct {
|
||||||
controller *api.Controller // controllers
|
controller *api.Controller // controllers
|
||||||
checker *checker.Registry // type checker registry
|
checker *checker.Registry // type checker registry
|
||||||
middleware *middleware.Registry // middlewares
|
middleware *middleware.Registry // middlewares
|
||||||
driver driver.Driver
|
schema *meta.Schema
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a framework instance from a configuration file
|
// New creates a framework instance from a configuration file
|
||||||
// _path is the json configuration path
|
// _path is the json configuration path
|
||||||
// _driver is used to load/run the controllers and middlewares (default: )
|
// _driver is used to load/run the controllers and middlewares (default: )
|
||||||
//
|
//
|
||||||
func New(_path string, _driver driver.Driver) (*Server, error) {
|
func New(_path string) (*Server, error) {
|
||||||
|
|
||||||
/* (1) Default driver : Plugin */
|
/* 1. Load config */
|
||||||
if _driver == nil {
|
schema, err := meta.Parse("./aicra.json")
|
||||||
_driver = &driver.Plugin{}
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Default folders according to drivers */
|
/* 2. Default driver : Plugin */
|
||||||
_folders := make([]string, 0, 2)
|
_folders := make([]string, 0, 2)
|
||||||
_folders = append(_folders, ".build/type")
|
_folders = append(_folders, ".build/type")
|
||||||
if _driver.Name() == "plugin" { // plugin
|
if schema.Driver.Name() == "plugin" { // plugin
|
||||||
_folders = append(_folders, ".build/middleware")
|
_folders = append(_folders, ".build/middleware")
|
||||||
} else { // generic
|
} else { // generic
|
||||||
_folders = append(_folders, "middleware")
|
_folders = append(_folders, "middleware")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (1) Init instance */
|
/* (1) Init instance */
|
||||||
var err error
|
|
||||||
var i = &Server{
|
var i = &Server{
|
||||||
controller: nil,
|
controller: nil,
|
||||||
driver: _driver,
|
schema: schema,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Load configuration */
|
/* (2) Load configuration */
|
||||||
|
@ -59,7 +59,7 @@ func New(_path string, _driver driver.Driver) (*Server, error) {
|
||||||
i.checker = checker.CreateRegistry(_folders[0])
|
i.checker = checker.CreateRegistry(_folders[0])
|
||||||
|
|
||||||
/* (4) Default middleware registry */
|
/* (4) Default middleware registry */
|
||||||
i.middleware = middleware.CreateRegistry(_driver, _folders[1])
|
i.middleware = middleware.CreateRegistry(schema.Driver, _folders[1])
|
||||||
|
|
||||||
return i, nil
|
return i, nil
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
/* (5) Load controller
|
/* (5) Load controller
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
controllerImplementation, callErr := apiRequest.RunController(req.Method, s.driver)
|
controllerImplementation, callErr := apiRequest.RunController(req.Method, s.schema.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