This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin-server.go
113 lines (88 loc) · 2.65 KB
/
plugin-server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Package plugin contains utilities for building a VendoPunkto plugin in Go
// A plugin is basically a binary which runs an HTTP server that VendoPunkto
// consumes. This package aims to help doing so.
package plugin
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/hashicorp/go-hclog"
"github.com/leonardochaia/vendopunkto/errors"
)
// Server is the plugin server running on every plugin
type Server struct {
plugins []ServerPlugin
pluginInfos []PluginInfo
started bool
Logger hclog.Logger
}
// ServerPlugin wraps the plugin implementation.
// It configures the router and orchestrates the underlying calls
// to the actual implementation
type ServerPlugin interface {
initializeRouter(router *chi.Mux) error
GetPluginImpl() (VendoPunktoPlugin, error)
}
// NewServer creates the plugin server which must be run by every plugin
func NewServer(logger hclog.Logger) *Server {
return &Server{
plugins: []ServerPlugin{},
pluginInfos: []PluginInfo{},
started: false,
Logger: logger,
}
}
// AddPlugin adds a plugin to the server
// This must be called before the server is started
func (s *Server) AddPlugin(plugin ServerPlugin) error {
if s.started {
return fmt.Errorf("Plugins must be added before the server is started")
}
s.plugins = append(s.plugins, plugin)
return nil
}
// Start initializes the router and starts serving on the provided net address
func (s *Server) Start(addr string) error {
if s.started {
return fmt.Errorf("Server has already been started")
}
router := chi.NewRouter()
s.pluginInfos = []PluginInfo{}
s.initializeAllPlugins(router)
router.Post(ActivatePluginEndpoint, errors.WrapHandler(s.activatePluginHandler))
return http.ListenAndServe(addr, router)
}
// initializeAllPlugins loops through registered plugins and
// initializes their router and stores references to implementations
func (s *Server) initializeAllPlugins(router chi.Router) error {
for _, value := range s.plugins {
impl, err := value.GetPluginImpl()
if err != nil {
return err
}
info, err := impl.GetPluginInfo()
if err != nil {
return err
}
pRouter := chi.NewRouter()
router.Mount(info.GetAddress(), pRouter)
err = value.initializeRouter(pRouter)
if err != nil {
return err
}
s.pluginInfos = append(s.pluginInfos, info)
s.Logger.Info("Routed plugin",
"id", info.ID,
"name", info.Name,
"address", info.GetAddress(),
)
}
return nil
}
func (s *Server) activatePluginHandler(w http.ResponseWriter, r *http.Request) error {
const op errors.Op = "plugin.base.activate"
s.Logger.Info("Activating plugin")
render.JSON(w, r, s.pluginInfos)
return nil
}