diff --git a/driver/generic.go b/driver/generic.go new file mode 100644 index 0000000..6c831f6 --- /dev/null +++ b/driver/generic.go @@ -0,0 +1,71 @@ +package driver + +import ( + "encoding/json" + "fmt" + "git.xdrm.io/go/aicra/err" + "git.xdrm.io/go/aicra/response" + "os/exec" + "strings" +) + +// Load implements the Driver interface +func (d *Generic) Load(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) { + + /* (1) Build controller path */ + path := strings.Join(_path, "-") + if len(path) == 0 { + path = fmt.Sprintf("./controller/ROOT") + } else { + path = fmt.Sprintf("./controller/%s", path) + } + + /* (2) Format method */ + method := strings.ToUpper(_method) + + return func(d response.Arguments) response.Response { + + res := response.New() + + /* (1) Prepare stdin data */ + d["_HTTP_METHOD_"] = method + stdin, err2 := json.Marshal(d) + if err2 != nil { + res.Err = err.UncallableController + return *res + } + + /* (2) Try to load command with -> stdout */ + cmd := exec.Command(path, string(stdin)) + + stdout, err2 := cmd.Output() + if err2 != nil { + res.Err = err.UncallableController + return *res + } + + /* (3) Get output json */ + output := make(response.Arguments) + err2 = json.Unmarshal(stdout, output) + + res.Err = err.Success + + // extract error (success by default or on error) + if outErr, ok := output["error"]; ok { + tmpErr, ok := outErr.(err.Error) + if ok { + res.Err = tmpErr + } + + delete(output, "error") + } + + /* (4) fill response */ + for k, v := range output { + res.Set(k, v) + } + + return *res + + }, err.Success +} diff --git a/driver/plugin.go b/driver/plugin.go index 7fff8f1..8e7c845 100644 --- a/driver/plugin.go +++ b/driver/plugin.go @@ -9,7 +9,7 @@ import ( ) // Load implements the Driver interface -func (d *Plugin) Load(_path []string, _method string) (func(response.Arguments, *response.Response) response.Response, err.Error) { +func (d *Plugin) Load(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) { /* (1) Build controller path */ path := strings.Join(_path, "-") @@ -37,7 +37,7 @@ func (d *Plugin) Load(_path []string, _method string) (func(response.Arguments, } /* (4) Check signature */ - callable, validSignature := m.(func(response.Arguments, *response.Response) response.Response) + callable, validSignature := m.(func(response.Arguments) response.Response) if !validSignature { return nil, err.UncallableMethod } diff --git a/driver/types.go b/driver/types.go index eec51ff..17516aa 100644 --- a/driver/types.go +++ b/driver/types.go @@ -6,12 +6,12 @@ import ( ) type Driver interface { - Load(_path []string, _method string) (func(response.Arguments, *response.Response) response.Response, err.Error) + Load(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) } // Generic tells the aicra instance to use the generic driver to load controller's executables // -// It will call an executable with the json input into the standard input +// It will call an executable with the json input into the standard input (argument 1) // the HTTP method is send as the key _HTTP_METHOD_ (in upper case) // The standard output must be a json corresponding to the data // diff --git a/internal/apirequest/request.go b/internal/apirequest/request.go index e5e7612..e06adf8 100644 --- a/internal/apirequest/request.go +++ b/internal/apirequest/request.go @@ -94,7 +94,7 @@ func FetchFormData(req *http.Request) map[string]interface{} { // LoadController tries to load a controller from its uri // checks for its given method ('Get', 'Post', 'Put', or 'Delete') -func (i *Request) LoadController(_method string, _driver driver.Driver) (func(response.Arguments, *response.Response) response.Response, err.Error) { +func (i *Request) LoadController(_method string, _driver driver.Driver) (func(response.Arguments) response.Response, err.Error) { return _driver.Load(i.Path, _method) diff --git a/server.go b/server.go index 6c24f05..552889b 100644 --- a/server.go +++ b/server.go @@ -8,7 +8,6 @@ import ( "git.xdrm.io/go/aicra/internal/checker" "git.xdrm.io/go/aicra/internal/config" "git.xdrm.io/go/aicra/middleware" - "git.xdrm.io/go/aicra/response" "log" "net/http" ) @@ -116,7 +115,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) { parameters["_SCOPE_"] = scope /* (3) Execute */ - response := controllerImplementation(parameters, response.New()) + response := controllerImplementation(parameters) /* (4) Extract http headers */ for k, v := range response.Dump() {