2018-09-27 11:43:36 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.xdrm.io/go/aicra/err"
|
|
|
|
"git.xdrm.io/go/aicra/response"
|
2018-09-28 08:54:13 +00:00
|
|
|
"net/http"
|
2018-10-01 10:29:05 +00:00
|
|
|
"path/filepath"
|
2018-09-27 11:43:36 +00:00
|
|
|
"plugin"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-09-28 16:30:49 +00:00
|
|
|
// Name returns the driver name
|
2018-10-01 10:29:05 +00:00
|
|
|
func (d Plugin) Name() string { return "plugin" }
|
|
|
|
|
|
|
|
// Path returns the universal path from the source path
|
|
|
|
func (d Plugin) Path(_root, _folder, _src string) string {
|
|
|
|
return filepath.Dir(_src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source returns the source path from the universal path
|
|
|
|
func (d Plugin) Source(_root, _folder, _path string) string {
|
|
|
|
return filepath.Join(_root, _folder, _path, "main.go")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build returns the build path from the universal path
|
|
|
|
func (d Plugin) Build(_root, _folder, _path string) string {
|
|
|
|
return fmt.Sprintf("%s.so", filepath.Join(_root, ".build", _folder, _path))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiled returns whether the driver has to be build
|
|
|
|
func (d Plugin) Compiled() bool { return true }
|
2018-09-28 13:58:30 +00:00
|
|
|
|
2018-09-28 08:54:13 +00:00
|
|
|
// RunController implements the Driver interface
|
|
|
|
func (d *Plugin) RunController(_path []string, _method string) (func(response.Arguments) response.Response, err.Error) {
|
2018-09-27 11:43:36 +00:00
|
|
|
|
|
|
|
/* (1) Build controller path */
|
|
|
|
path := strings.Join(_path, "-")
|
|
|
|
if len(path) == 0 {
|
|
|
|
path = fmt.Sprintf(".build/controller/ROOT.so")
|
|
|
|
} else {
|
|
|
|
path = fmt.Sprintf(".build/controller/%s.so", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Format url */
|
2018-10-01 13:14:48 +00:00
|
|
|
method := strings.ToLower(_method)
|
2018-09-27 11:43:36 +00:00
|
|
|
|
|
|
|
/* (2) Try to load plugin */
|
|
|
|
p, err2 := plugin.Open(path)
|
|
|
|
if err2 != nil {
|
|
|
|
return nil, err.UncallableController
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
/* (3) Try to extract exported field */
|
|
|
|
m, err2 := p.Lookup("Export")
|
2018-09-27 11:43:36 +00:00
|
|
|
if err2 != nil {
|
2018-10-01 13:14:48 +00:00
|
|
|
return nil, err.UncallableController
|
|
|
|
}
|
|
|
|
|
|
|
|
exported, ok := m.(func() Controller)
|
|
|
|
if !ok {
|
|
|
|
return nil, err.UncallableController
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
/* (4) Controller */
|
|
|
|
ctl := exported()
|
|
|
|
|
2018-09-27 11:43:36 +00:00
|
|
|
/* (4) Check signature */
|
2018-10-01 13:14:48 +00:00
|
|
|
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
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
2018-10-01 13:14:48 +00:00
|
|
|
fmt.Printf("method: %s\n", method)
|
2018-09-27 11:43:36 +00:00
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
return nil, err.UncallableMethod
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
2018-09-28 08:54:13 +00:00
|
|
|
|
|
|
|
// LoadMiddleware returns a new middleware function; it must be a
|
2018-09-28 09:08:44 +00:00
|
|
|
// valid and existing folder/filename file with the .so extension
|
2018-09-28 08:54:13 +00:00
|
|
|
func (d *Plugin) LoadMiddleware(_path string) (func(http.Request, *[]string), error) {
|
|
|
|
|
|
|
|
// ignore non .so files
|
|
|
|
if !strings.HasSuffix(_path, ".so") {
|
|
|
|
return nil, fmt.Errorf("Invalid name")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (1) Check plugin name */
|
|
|
|
if len(_path) < 1 {
|
2018-09-28 09:08:44 +00:00
|
|
|
return nil, fmt.Errorf("Middleware name must not be empty")
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Check plugin extension */
|
|
|
|
if !strings.HasSuffix(_path, ".so") {
|
|
|
|
_path = fmt.Sprintf("%s.so", _path)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Try to load the plugin */
|
|
|
|
p, err := plugin.Open(_path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
/* (4) Extract exported fields */
|
|
|
|
mw, err := p.Lookup("Export")
|
2018-09-28 08:54:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Missing method 'Inspect()'; %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
exported, ok := mw.(func() Middleware)
|
2018-09-28 08:54:13 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Inspect() is malformed")
|
|
|
|
}
|
|
|
|
|
2018-10-01 13:14:48 +00:00
|
|
|
/* (5) Return Inspect method */
|
|
|
|
return exported().Inspect, nil
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|