diff --git a/cmd/aicra/main.go b/cmd/aicra/main.go index 1eb4c58..441a5ad 100644 --- a/cmd/aicra/main.go +++ b/cmd/aicra/main.go @@ -97,7 +97,6 @@ func main() { build := schema.Driver.Build(schema.Root, schema.Controllers.Folder, upath) compile(source, build) - fmt.Printf("\n") } } diff --git a/driver/plugin.go b/driver/plugin.go index 272497e..ca0e600 100644 --- a/driver/plugin.go +++ b/driver/plugin.go @@ -44,9 +44,7 @@ func (d *Plugin) RunController(_path []string, _method string) (func(response.Ar } /* (2) Format url */ - tmp := []byte(strings.ToLower(_method)) - tmp[0] = tmp[0] - ('a' - 'A') - method := string(tmp) + method := strings.ToLower(_method) /* (2) Try to load plugin */ p, err2 := plugin.Open(path) @@ -54,19 +52,34 @@ func (d *Plugin) RunController(_path []string, _method string) (func(response.Ar return nil, err.UncallableController } - /* (3) Try to extract method */ - m, err2 := p.Lookup(method) + /* (3) Try to extract exported field */ + m, err2 := p.Lookup("Export") 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 */ - callable, validSignature := m.(func(response.Arguments) response.Response) - if !validSignature { - return nil, err.UncallableMethod + switch method { + case "get": + 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 @@ -94,18 +107,17 @@ func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), er return nil, err } - /* (4) Export wanted properties */ - inspect, err := p.Lookup("Inspect") + /* (4) Extract exported fields */ + mw, err := p.Lookup("Export") if err != nil { return nil, fmt.Errorf("Missing method 'Inspect()'; %s", err) } - /* (5) Cast Inspect */ - mware, ok := inspect.(func(http.Request, *[]string)) + exported, ok := mw.(func() Middleware) if !ok { return nil, fmt.Errorf("Inspect() is malformed") } - /* (6) Add type to registry */ - return mware, nil + /* (5) Return Inspect method */ + return exported().Inspect, nil } diff --git a/middleware/types.go b/middleware/types.go index b63f39a..4985027 100644 --- a/middleware/types.go +++ b/middleware/types.go @@ -12,14 +12,6 @@ import ( // purposes, the type is always used as its definition ([]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 type Wrapper struct { Inspect func(http.Request, *[]string) diff --git a/server.go b/server.go index b1e4921..1c2ae22 100644 --- a/server.go +++ b/server.go @@ -1,10 +1,10 @@ package aicra import ( - "git.xdrm.io/go/aicra/driver" e "git.xdrm.io/go/aicra/err" "git.xdrm.io/go/aicra/internal/api" "git.xdrm.io/go/aicra/internal/checker" + "git.xdrm.io/go/aicra/internal/meta" apirequest "git.xdrm.io/go/aicra/internal/request" "git.xdrm.io/go/aicra/middleware" "log" @@ -19,34 +19,34 @@ type Server struct { controller *api.Controller // controllers checker *checker.Registry // type checker registry middleware *middleware.Registry // middlewares - driver driver.Driver + schema *meta.Schema } // 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) (*Server, error) { - /* (1) Default driver : Plugin */ - if _driver == nil { - _driver = &driver.Plugin{} + /* 1. Load config */ + schema, err := meta.Parse("./aicra.json") + if err != nil { + return nil, err } - /* (2) Default folders according to drivers */ + /* 2. Default driver : Plugin */ _folders := make([]string, 0, 2) _folders = append(_folders, ".build/type") - if _driver.Name() == "plugin" { // plugin + if schema.Driver.Name() == "plugin" { // plugin _folders = append(_folders, ".build/middleware") } else { // generic _folders = append(_folders, "middleware") } /* (1) Init instance */ - var err error var i = &Server{ controller: nil, - driver: _driver, + schema: schema, } /* (2) Load configuration */ @@ -59,7 +59,7 @@ func New(_path string, _driver driver.Driver) (*Server, error) { i.checker = checker.CreateRegistry(_folders[0]) /* (4) Default middleware registry */ - i.middleware = middleware.CreateRegistry(_driver, _folders[1]) + i.middleware = middleware.CreateRegistry(schema.Driver, _folders[1]) return i, nil @@ -109,7 +109,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { /* (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 { httpError(res, callErr) log.Printf("[err] %s\n", err)