2020-04-20 16:36:34 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-11-30 20:42:06 +00:00
|
|
|
"path/filepath"
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-04-20 16:36:34 +00:00
|
|
|
)
|
|
|
|
|
2020-09-07 11:58:03 +00:00
|
|
|
// Constructor creates a plugin handler.
|
|
|
|
type Constructor func(context.Context, http.Handler) (http.Handler, error)
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
type pluginMiddleware interface {
|
|
|
|
NewHandler(ctx context.Context, next http.Handler) (http.Handler, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type middlewareBuilder interface {
|
|
|
|
newMiddleware(config map[string]interface{}, middlewareName string) (pluginMiddleware, error)
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:36:34 +00:00
|
|
|
// Builder is a plugin builder.
|
|
|
|
type Builder struct {
|
2022-01-14 11:22:06 +00:00
|
|
|
providerBuilders map[string]providerBuilder
|
2023-11-30 20:42:06 +00:00
|
|
|
middlewareBuilders map[string]middlewareBuilder
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuilder creates a new Builder.
|
2021-06-25 13:50:09 +00:00
|
|
|
func NewBuilder(client *Client, plugins map[string]Descriptor, localPlugins map[string]LocalDescriptor) (*Builder, error) {
|
2023-11-30 20:42:06 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-04-20 16:36:34 +00:00
|
|
|
pb := &Builder{
|
2023-11-30 20:42:06 +00:00
|
|
|
middlewareBuilders: map[string]middlewareBuilder{},
|
2022-01-14 11:22:06 +00:00
|
|
|
providerBuilders: map[string]providerBuilder{},
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for pName, desc := range plugins {
|
|
|
|
manifest, err := client.ReadManifest(desc.ModuleName)
|
|
|
|
if err != nil {
|
|
|
|
_ = client.ResetAll()
|
|
|
|
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
|
|
|
|
}
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
logger := log.With().
|
|
|
|
Str("plugin", "plugin-"+pName).
|
|
|
|
Str("module", desc.ModuleName).
|
|
|
|
Str("runtime", manifest.Runtime).
|
|
|
|
Logger()
|
|
|
|
logCtx := logger.WithContext(ctx)
|
2022-11-21 17:36:05 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
switch manifest.Type {
|
|
|
|
case typeMiddleware:
|
|
|
|
middleware, err := newMiddlewareBuilder(logCtx, client.GoPath(), manifest, desc.ModuleName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 13:50:09 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
pb.middlewareBuilders[pName] = middleware
|
2021-06-25 13:50:09 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
case typeProvider:
|
|
|
|
pBuilder, err := newProviderBuilder(logCtx, manifest, client.GoPath())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", desc.ModuleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pb.providerBuilders[pName] = pBuilder
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
|
2021-06-25 13:50:09 +00:00
|
|
|
}
|
2023-11-30 20:42:06 +00:00
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
for pName, desc := range localPlugins {
|
|
|
|
manifest, err := ReadManifest(localGoPath, desc.ModuleName)
|
2020-04-20 16:36:34 +00:00
|
|
|
if err != nil {
|
2023-11-30 20:42:06 +00:00
|
|
|
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
logger := log.With().
|
|
|
|
Str("plugin", "plugin-"+pName).
|
|
|
|
Str("module", desc.ModuleName).
|
|
|
|
Str("runtime", manifest.Runtime).
|
|
|
|
Logger()
|
|
|
|
logCtx := logger.WithContext(ctx)
|
|
|
|
|
2021-05-11 14:14:10 +00:00
|
|
|
switch manifest.Type {
|
2023-11-30 20:42:06 +00:00
|
|
|
case typeMiddleware:
|
|
|
|
middleware, err := newMiddlewareBuilder(logCtx, localGoPath, manifest, desc.ModuleName)
|
2022-01-14 11:22:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
2022-01-14 11:22:06 +00:00
|
|
|
|
|
|
|
pb.middlewareBuilders[pName] = middleware
|
2023-11-30 20:42:06 +00:00
|
|
|
|
|
|
|
case typeProvider:
|
|
|
|
builder, err := newProviderBuilder(logCtx, manifest, localGoPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %w", desc.ModuleName, err)
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
2023-11-30 20:42:06 +00:00
|
|
|
|
|
|
|
pb.providerBuilders[pName] = builder
|
|
|
|
|
2021-05-11 14:14:10 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-30 20:42:06 +00:00
|
|
|
return pb, nil
|
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
// Build builds a middleware plugin.
|
|
|
|
func (b Builder) Build(pName string, config map[string]interface{}, middlewareName string) (Constructor, error) {
|
|
|
|
if b.middlewareBuilders == nil {
|
|
|
|
return nil, fmt.Errorf("no plugin definitions in the static configuration: %s", pName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// plugin (pName) can be located in yaegi or wasm middleware builders.
|
|
|
|
if descriptor, ok := b.middlewareBuilders[pName]; ok {
|
|
|
|
m, err := descriptor.newMiddleware(config, middlewareName)
|
2020-04-20 16:36:34 +00:00
|
|
|
if err != nil {
|
2023-11-30 20:42:06 +00:00
|
|
|
return nil, err
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
return m.NewHandler, nil
|
|
|
|
}
|
2022-11-21 17:36:05 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
return nil, fmt.Errorf("unknown plugin type: %s", pName)
|
|
|
|
}
|
2021-06-25 13:50:09 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
func newMiddlewareBuilder(ctx context.Context, goPath string, manifest *Manifest, moduleName string) (middlewareBuilder, error) {
|
|
|
|
switch manifest.Runtime {
|
|
|
|
case runtimeWasm:
|
|
|
|
wasmPath, err := getWasmPath(manifest)
|
2021-06-25 13:50:09 +00:00
|
|
|
if err != nil {
|
2023-11-30 20:42:06 +00:00
|
|
|
return nil, fmt.Errorf("wasm path: %w", err)
|
2021-06-25 13:50:09 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
return newWasmMiddlewareBuilder(goPath, moduleName, wasmPath), nil
|
|
|
|
|
|
|
|
case runtimeYaegi, "":
|
|
|
|
i, err := newInterpreter(ctx, goPath, manifest.Import)
|
2021-06-25 13:50:09 +00:00
|
|
|
if err != nil {
|
2023-11-30 20:42:06 +00:00
|
|
|
return nil, fmt.Errorf("failed to craete Yaegi intepreter: %w", err)
|
2021-06-25 13:50:09 +00:00
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
return newYaegiMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown plugin runtime: %s", manifest.Runtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProviderBuilder(ctx context.Context, manifest *Manifest, goPath string) (providerBuilder, error) {
|
|
|
|
switch manifest.Runtime {
|
|
|
|
case runtimeYaegi, "":
|
|
|
|
i, err := newInterpreter(ctx, goPath, manifest.Import)
|
2020-04-20 16:36:34 +00:00
|
|
|
if err != nil {
|
2023-11-30 20:42:06 +00:00
|
|
|
return providerBuilder{}, err
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
return providerBuilder{
|
|
|
|
interpreter: i,
|
|
|
|
Import: manifest.Import,
|
|
|
|
BasePkg: manifest.BasePkg,
|
|
|
|
}, nil
|
2022-01-14 11:22:06 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
default:
|
|
|
|
return providerBuilder{}, fmt.Errorf("unknown plugin runtime: %s", manifest.Runtime)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
2023-11-30 20:42:06 +00:00
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2023-11-30 20:42:06 +00:00
|
|
|
func getWasmPath(manifest *Manifest) (string, error) {
|
|
|
|
wasmPath := manifest.WasmPath
|
|
|
|
if wasmPath == "" {
|
|
|
|
wasmPath = "plugin.wasm"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsLocal(wasmPath) {
|
|
|
|
return "", fmt.Errorf("wasmPath must be a local path")
|
|
|
|
}
|
|
|
|
|
|
|
|
return wasmPath, nil
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|