@@ -11,37 +11,43 @@ import (
11
11
"gorm.io/gorm"
12
12
)
13
13
14
+ // PluginsHandler is the handler for the plugins API
14
15
type PluginsHandler struct {
15
16
logger schemas.Logger
16
17
configStore configstore.ConfigStore
17
18
}
18
19
20
+ // NewPluginsHandler creates a new PluginsHandler
19
21
func NewPluginsHandler (configStore configstore.ConfigStore , logger schemas.Logger ) * PluginsHandler {
20
22
return & PluginsHandler {
21
23
configStore : configStore ,
22
24
logger : logger ,
23
25
}
24
26
}
25
27
28
+ // CreatePluginRequest is the request body for creating a plugin
26
29
type CreatePluginRequest struct {
27
- Name string `json:"name"`
28
- Enabled bool `json:"enabled"`
29
- Config map [string ]interface {} `json:"config"`
30
+ Name string `json:"name"`
31
+ Enabled bool `json:"enabled"`
32
+ Config map [string ]any `json:"config"`
30
33
}
31
34
35
+ // UpdatePluginRequest is the request body for updating a plugin
32
36
type UpdatePluginRequest struct {
33
- Enabled bool `json:"enabled"`
34
- Config map [string ]interface {} `json:"config"`
37
+ Enabled bool `json:"enabled"`
38
+ Config map [string ]any `json:"config"`
35
39
}
36
40
37
- func (h * PluginsHandler ) RegisterRoutes (r * router.Router , middlewares ... fasthttp.RequestHandler ) {
41
+ // RegisterRoutes registers the routes for the PluginsHandler
42
+ func (h * PluginsHandler ) RegisterRoutes (r * router.Router , middlewares ... BifrostHTTPMiddleware ) {
38
43
r .GET ("/api/plugins" , ChainMiddlewares (h .getPlugins , middlewares ... ))
39
44
r .GET ("/api/plugins/{name}" , ChainMiddlewares (h .getPlugin , middlewares ... ))
40
45
r .POST ("/api/plugins" , ChainMiddlewares (h .createPlugin , middlewares ... ))
41
46
r .PUT ("/api/plugins/{name}" , ChainMiddlewares (h .updatePlugin , middlewares ... ))
42
47
r .DELETE ("/api/plugins/{name}" , ChainMiddlewares (h .deletePlugin , middlewares ... ))
43
48
}
44
49
50
+ // getPlugins gets all plugins
45
51
func (h * PluginsHandler ) getPlugins (ctx * fasthttp.RequestCtx ) {
46
52
plugins , err := h .configStore .GetPlugins ()
47
53
if err != nil {
@@ -50,12 +56,13 @@ func (h *PluginsHandler) getPlugins(ctx *fasthttp.RequestCtx) {
50
56
return
51
57
}
52
58
53
- SendJSON (ctx , map [string ]interface {} {
59
+ SendJSON (ctx , map [string ]any {
54
60
"plugins" : plugins ,
55
61
"count" : len (plugins ),
56
62
}, h .logger )
57
63
}
58
64
65
+ // getPlugin gets a plugin by name
59
66
func (h * PluginsHandler ) getPlugin (ctx * fasthttp.RequestCtx ) {
60
67
// Safely validate the "name" parameter
61
68
nameValue := ctx .UserValue ("name" )
@@ -91,6 +98,7 @@ func (h *PluginsHandler) getPlugin(ctx *fasthttp.RequestCtx) {
91
98
SendJSON (ctx , plugin , h .logger )
92
99
}
93
100
101
+ // createPlugin creates a new plugin
94
102
func (h * PluginsHandler ) createPlugin (ctx * fasthttp.RequestCtx ) {
95
103
var request CreatePluginRequest
96
104
if err := json .Unmarshal (ctx .PostBody (), & request ); err != nil {
@@ -130,12 +138,13 @@ func (h *PluginsHandler) createPlugin(ctx *fasthttp.RequestCtx) {
130
138
}
131
139
132
140
ctx .SetStatusCode (fasthttp .StatusCreated )
133
- SendJSON (ctx , map [string ]interface {} {
141
+ SendJSON (ctx , map [string ]any {
134
142
"message" : "Plugin created successfully" ,
135
143
"plugin" : plugin ,
136
144
}, h .logger )
137
145
}
138
146
147
+ // updatePlugin updates an existing plugin
139
148
func (h * PluginsHandler ) updatePlugin (ctx * fasthttp.RequestCtx ) {
140
149
// Safely validate the "name" parameter
141
150
nameValue := ctx .UserValue ("name" )
@@ -160,9 +169,16 @@ func (h *PluginsHandler) updatePlugin(ctx *fasthttp.RequestCtx) {
160
169
161
170
// Check if plugin exists
162
171
if _ , err := h .configStore .GetPlugin (name ); err != nil {
163
- h .logger .Warn ("plugin not found for update: %s" , name )
164
- SendError (ctx , fasthttp .StatusNotFound , "Plugin not found" , h .logger )
165
- return
172
+ // If doesn't exist, create it
173
+ if err := h .configStore .CreatePlugin (& configstore.TablePlugin {
174
+ Name : name ,
175
+ Enabled : false ,
176
+ Config : map [string ]any {},
177
+ }); err != nil {
178
+ h .logger .Error ("failed to create plugin: %v" , err )
179
+ SendError (ctx , 500 , "Failed to create plugin" , h .logger )
180
+ return
181
+ }
166
182
}
167
183
168
184
var request UpdatePluginRequest
@@ -199,6 +215,7 @@ func (h *PluginsHandler) updatePlugin(ctx *fasthttp.RequestCtx) {
199
215
}, h .logger )
200
216
}
201
217
218
+ // deletePlugin deletes an existing plugin
202
219
func (h * PluginsHandler ) deletePlugin (ctx * fasthttp.RequestCtx ) {
203
220
// Safely validate the "name" parameter
204
221
nameValue := ctx .UserValue ("name" )
0 commit comments