implement driver.Generic.LoadMiddleware() [TODO] test it
This commit is contained in:
parent
2cfc5a2ba0
commit
b957d82a64
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
e "git.xdrm.io/go/aicra/err"
|
e "git.xdrm.io/go/aicra/err"
|
||||||
"git.xdrm.io/go/aicra/response"
|
"git.xdrm.io/go/aicra/response"
|
||||||
|
"net/http"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -79,3 +80,58 @@ func (d *Generic) RunController(_path []string, _method string) (func(response.A
|
||||||
|
|
||||||
}, e.Success
|
}, e.Success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadMiddleware returns a new middleware function; it must be a
|
||||||
|
// valid and existing folder/filename file
|
||||||
|
func (d *Generic) LoadMiddleware(_path string) (func(http.Request, *[]string), error) {
|
||||||
|
|
||||||
|
/* (1) Check plugin name */
|
||||||
|
if len(_path) < 1 {
|
||||||
|
return nil, fmt.Errorf("Middleware name must not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (2) Create method + error */
|
||||||
|
return func(_req http.Request, _scope *[]string) {
|
||||||
|
|
||||||
|
/* (1) Prepare stdin data */
|
||||||
|
stdin, err := json.Marshal(_scope)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (2) Try to load command with <stdin> -> stdout */
|
||||||
|
cmd := exec.Command(_path, string(stdin))
|
||||||
|
|
||||||
|
stdout, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (3) Get output json */
|
||||||
|
var outputI interface{}
|
||||||
|
err = json.Unmarshal(stdout, &outputI)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
output, ok := outputI.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract 'scope' (empty by default or error)
|
||||||
|
scopeOut, ok := output["scope"]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scope, ok := scopeOut.([]string)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
*_scope = append(*_scope, scope...)
|
||||||
|
|
||||||
|
}, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -47,8 +47,7 @@ func (d *Plugin) RunController(_path []string, _method string) (func(response.Ar
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadMiddleware returns a new middleware function; it must be a
|
// LoadMiddleware returns a new middleware function; it must be a
|
||||||
// valid and existing folder/filename file with or without the .so extension
|
// valid and existing folder/filename file with the .so extension
|
||||||
// it must be located in the relative directory .build/middleware
|
|
||||||
func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), error) {
|
func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), error) {
|
||||||
|
|
||||||
// ignore non .so files
|
// ignore non .so files
|
||||||
|
@ -58,7 +57,7 @@ func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), er
|
||||||
|
|
||||||
/* (1) Check plugin name */
|
/* (1) Check plugin name */
|
||||||
if len(_path) < 1 {
|
if len(_path) < 1 {
|
||||||
return nil, fmt.Errorf("Plugin name must not be empty")
|
return nil, fmt.Errorf("Middleware name must not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (2) Check plugin extension */
|
/* (2) Check plugin extension */
|
||||||
|
|
Loading…
Reference in New Issue