add 'driver/generic' (to test, alpha version)
This commit is contained in:
parent
3feae783dc
commit
fced676c42
|
@ -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 <stdin> -> 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
|
||||||
|
}
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load implements the Driver interface
|
// 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 */
|
/* (1) Build controller path */
|
||||||
path := strings.Join(_path, "-")
|
path := strings.Join(_path, "-")
|
||||||
|
@ -37,7 +37,7 @@ func (d *Plugin) Load(_path []string, _method string) (func(response.Arguments,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (4) Check signature */
|
/* (4) Check signature */
|
||||||
callable, validSignature := m.(func(response.Arguments, *response.Response) response.Response)
|
callable, validSignature := m.(func(response.Arguments) response.Response)
|
||||||
if !validSignature {
|
if !validSignature {
|
||||||
return nil, err.UncallableMethod
|
return nil, err.UncallableMethod
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Driver interface {
|
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
|
// 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 HTTP method is send as the key _HTTP_METHOD_ (in upper case)
|
||||||
// The standard output must be a json corresponding to the data
|
// The standard output must be a json corresponding to the data
|
||||||
//
|
//
|
||||||
|
|
|
@ -94,7 +94,7 @@ func FetchFormData(req *http.Request) map[string]interface{} {
|
||||||
|
|
||||||
// LoadController tries to load a controller from its uri
|
// LoadController 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) 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)
|
return _driver.Load(i.Path, _method)
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"git.xdrm.io/go/aicra/internal/checker"
|
"git.xdrm.io/go/aicra/internal/checker"
|
||||||
"git.xdrm.io/go/aicra/internal/config"
|
"git.xdrm.io/go/aicra/internal/config"
|
||||||
"git.xdrm.io/go/aicra/middleware"
|
"git.xdrm.io/go/aicra/middleware"
|
||||||
"git.xdrm.io/go/aicra/response"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -116,7 +115,7 @@ func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
parameters["_SCOPE_"] = scope
|
parameters["_SCOPE_"] = scope
|
||||||
|
|
||||||
/* (3) Execute */
|
/* (3) Execute */
|
||||||
response := controllerImplementation(parameters, response.New())
|
response := controllerImplementation(parameters)
|
||||||
|
|
||||||
/* (4) Extract http headers */
|
/* (4) Extract http headers */
|
||||||
for k, v := range response.Dump() {
|
for k, v := range response.Dump() {
|
||||||
|
|
Loading…
Reference in New Issue