2018-09-27 11:43:36 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-01 10:29:05 +00:00
|
|
|
"path/filepath"
|
2018-09-27 11:43:36 +00:00
|
|
|
"plugin"
|
|
|
|
)
|
|
|
|
|
2018-10-01 15:43:18 +00:00
|
|
|
// Plugin tells the aicra instance to use the plugin driver to load controller/middleware executables
|
|
|
|
//
|
|
|
|
// It will load go .so plugins with the following interface :
|
|
|
|
//
|
|
|
|
// type Controller interface {
|
|
|
|
// Get(d i.Arguments, r *i.Response) i.Response
|
|
|
|
// Post(d i.Arguments, r *i.Response) i.Response
|
|
|
|
// Put(d i.Arguments, r *i.Response) i.Response
|
|
|
|
// Delete(d i.Arguments, r *i.Response) i.Response
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The controllers are exported by calling the 'Export() Controller' method
|
|
|
|
type Plugin struct{}
|
|
|
|
|
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 {
|
2018-10-01 17:27:38 +00:00
|
|
|
|
2018-10-01 10:29:05 +00:00
|
|
|
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 {
|
2018-10-01 17:27:38 +00:00
|
|
|
if _path == "" {
|
|
|
|
return fmt.Sprintf("%s", filepath.Join(_root, ".build", _folder, "ROOT.so"))
|
|
|
|
}
|
2018-10-01 10:29:05 +00:00
|
|
|
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-10-01 17:27:38 +00:00
|
|
|
// LoadController returns a new Controller
|
|
|
|
func (d *Plugin) LoadController(_path string) (Controller, error) {
|
2018-09-27 11:43:36 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 1. Try to load plugin */
|
|
|
|
p, err := plugin.Open(_path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 2. Try to extract exported field */
|
|
|
|
m, err := p.Lookup("Export")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-10-01 13:14:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
exporter, ok := m.(func() Controller)
|
2018-10-01 13:14:48 +00:00
|
|
|
if !ok {
|
2018-10-01 17:27:38 +00:00
|
|
|
return nil, err
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 3. Controller */
|
|
|
|
return exporter(), nil
|
2018-09-27 11:43:36 +00:00
|
|
|
}
|
2018-09-28 08:54:13 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
// LoadMiddleware returns a new Middleware
|
|
|
|
func (d *Plugin) LoadMiddleware(_path string) (Middleware, error) {
|
2018-09-28 08:54:13 +00:00
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 1. Try to load plugin */
|
|
|
|
p, err := plugin.Open(_path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 2. Try to extract exported field */
|
|
|
|
m, err := p.Lookup("Export")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
exporter, ok := m.(func() Middleware)
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
return exporter(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadChecker returns a new Checker
|
|
|
|
func (d *Plugin) LoadChecker(_path string) (Checker, error) {
|
|
|
|
|
|
|
|
/* 1. Try to load plugin */
|
2018-09-28 08:54:13 +00:00
|
|
|
p, err := plugin.Open(_path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
/* 2. Try to extract exported field */
|
|
|
|
m, err := p.Lookup("Export")
|
2018-09-28 08:54:13 +00:00
|
|
|
if err != nil {
|
2018-10-01 17:27:38 +00:00
|
|
|
return nil, err
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
exporter, ok := m.(func() Checker)
|
2018-09-28 08:54:13 +00:00
|
|
|
if !ok {
|
2018-10-01 17:27:38 +00:00
|
|
|
return nil, err
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:27:38 +00:00
|
|
|
return exporter(), nil
|
2018-09-28 08:54:13 +00:00
|
|
|
}
|