@@ -7,9 +7,10 @@ import (
7
7
8
8
// Packages
9
9
server "github.com/mutablelogic/go-server"
10
+ "github.com/mutablelogic/go-server/npm/helloworld"
10
11
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
11
12
provider "github.com/mutablelogic/go-server/pkg/provider"
12
- ref "github.com/mutablelogic/go-server/pkg/ref"
13
+ "github.com/mutablelogic/go-server/pkg/ref"
13
14
types "github.com/mutablelogic/go-server/pkg/types"
14
15
15
16
// Plugins
@@ -26,14 +27,15 @@ import (
26
27
// TYPES
27
28
28
29
type ServiceCommands struct {
29
- Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
30
- Run2 ServiceRun2Command `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
30
+ // Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
31
+ Run ServiceRun2Command `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
31
32
}
32
33
33
34
type ServiceRun2Command struct {
34
35
Plugins []string `help:"Plugin paths"`
35
36
}
36
37
38
+ /*
37
39
type ServiceRunCommand struct {
38
40
Router struct {
39
41
httprouter.Config `embed:"" prefix:"router."` // Router configuration
@@ -57,10 +59,11 @@ type ServiceRunCommand struct {
57
59
logger.Config `embed:"" prefix:"log."` // Logger configuration
58
60
} `embed:""`
59
61
}
62
+ */
60
63
61
64
///////////////////////////////////////////////////////////////////////////////
62
65
// PUBLIC METHODS
63
-
66
+ /*
64
67
func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
65
68
// Set the server listener and router prefix
66
69
cmd.Server.Listen = app.GetEndpoint()
@@ -198,6 +201,7 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
198
201
// Run the provider
199
202
return provider.Run(app.Context())
200
203
}
204
+ */
201
205
202
206
func (cmd * ServiceRun2Command ) Run (app server.Cmd ) error {
203
207
// Create a provider by loading the plugins
@@ -207,42 +211,133 @@ func (cmd *ServiceRun2Command) Run(app server.Cmd) error {
207
211
}
208
212
209
213
// Set the configuration
210
- err = errors .Join (err , provider .Load ("log" , "main" , func (ctx context.Context , config server.Plugin ) {
214
+ err = errors .Join (err , provider .Load ("log" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
211
215
logger := config .(* logger.Config )
212
216
logger .Debug = app .GetDebug () >= server .Debug
217
+ return nil
213
218
}))
214
- err = errors .Join (err , provider .Load ("httprouter" , "main" , func (ctx context.Context , config server.Plugin ) {
219
+ err = errors .Join (err , provider .Load ("httprouter" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
215
220
httprouter := config .(* httprouter.Config )
216
221
httprouter .Prefix = types .NormalisePath (app .GetEndpoint ().Path )
217
222
httprouter .Origin = "*"
218
223
httprouter .Middleware = []string {"log.main" }
224
+ return nil
219
225
}))
220
- err = errors .Join (err , provider .Load ("httpserver" , "main" , func (ctx context.Context , config server.Plugin ) {
226
+ err = errors .Join (err , provider .Load ("httpserver" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
221
227
httpserver := config .(* httpserver.Config )
222
228
httpserver .Listen = app .GetEndpoint ()
223
- httpserver .Router = provider .Task (ctx , "httprouter.main" ).(http.Handler )
229
+
230
+ // Set router
231
+ if router , ok := provider .Task (ctx , "httprouter.main" ).(http.Handler ); ! ok || router == nil {
232
+ return httpresponse .ErrInternalError .With ("Invalid router" )
233
+ } else {
234
+ httpserver .Router = router
235
+ }
236
+
237
+ // Return success
238
+ return nil
224
239
}))
225
- err = errors .Join (err , provider .Load ("helloworld" , "main" , func (ctx context.Context , config server.Plugin ) {
226
- // NO-OP
240
+ err = errors .Join (err , provider .Load ("helloworld" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
241
+ helloworld := config .(* helloworld.Config )
242
+
243
+ // Set router
244
+ if router , ok := provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter ); ! ok || router == nil {
245
+ return httpresponse .ErrInternalError .With ("Invalid router" )
246
+ } else {
247
+ helloworld .Router = router
248
+ }
249
+
250
+ // Return success
251
+ return nil
227
252
}))
228
- err = errors .Join (err , provider .Load ("pgpool" , "main" , func (ctx context.Context , config server.Plugin ) {
253
+ err = errors .Join (err , provider .Load ("pgpool" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
229
254
pgpool := config .(* pg.Config )
230
- pgpool .Router = provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter )
255
+
256
+ // Set router
257
+ if router , ok := provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter ); ! ok || router == nil {
258
+ return httpresponse .ErrInternalError .With ("Invalid router" )
259
+ } else {
260
+ pgpool .Router = router
261
+ }
262
+
263
+ // Set trace
264
+ if app .GetDebug () == server .Trace {
265
+ pgpool .Trace = func (ctx context.Context , query string , args any , err error ) {
266
+ if err != nil {
267
+ ref .Log (ctx ).With ("args" , args ).Print (ctx , err , " ON " , query )
268
+ } else {
269
+ ref .Log (ctx ).With ("args" , args ).Debug (ctx , query )
270
+ }
271
+ }
272
+ }
273
+
274
+ // Return success
275
+ return nil
231
276
}))
232
- err = errors .Join (err , provider .Load ("auth" , "main" , func (ctx context.Context , config server.Plugin ) {
277
+ err = errors .Join (err , provider .Load ("auth" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
233
278
auth := config .(* auth.Config )
234
- auth .Pool = provider .Task (ctx , "pgpool.main" ).(server.PG )
235
- auth .Router = provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter )
279
+
280
+ // Set the router
281
+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
282
+ return httpresponse .ErrInternalError .With ("Invalid router" )
283
+ } else {
284
+ auth .Router = router
285
+ }
286
+
287
+ // Set the connection pool
288
+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
289
+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
290
+ } else {
291
+ auth .Pool = pool
292
+ }
293
+
294
+ // Return success
295
+ return nil
236
296
}))
237
- err = errors .Join (err , provider .Load ("pgqueue" , "main" , func (ctx context.Context , config server.Plugin ) {
297
+ err = errors .Join (err , provider .Load ("pgqueue" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
238
298
pgqueue := config .(* pgqueue.Config )
239
- pgqueue .Pool = provider .Task (ctx , "pgpool.main" ).(server.PG )
240
- pgqueue .Router = provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter )
299
+
300
+ // Set the router
301
+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
302
+ return httpresponse .ErrInternalError .With ("Invalid router" )
303
+ } else {
304
+ pgqueue .Router = router
305
+ }
306
+
307
+ // Set the connection pool
308
+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
309
+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
310
+ } else {
311
+ pgqueue .Pool = pool
312
+ }
313
+
314
+ return nil
241
315
}))
242
- err = errors .Join (err , provider .Load ("certmanager" , "main" , func (ctx context.Context , config server.Plugin ) {
316
+ err = errors .Join (err , provider .Load ("certmanager" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
243
317
certmanager := config .(* cert.Config )
244
- certmanager .Router = provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter )
245
- certmanager .Pool = provider .Task (ctx , "pgpool.main" ).(server.PG )
318
+
319
+ // Set the router
320
+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
321
+ return httpresponse .ErrInternalError .With ("Invalid router" )
322
+ } else {
323
+ certmanager .Router = router
324
+ }
325
+
326
+ // Set the connection pool
327
+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
328
+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
329
+ } else {
330
+ certmanager .Pool = pool
331
+ }
332
+
333
+ // Set the queue
334
+ if queue , ok := ref .Provider (ctx ).Task (ctx , "pgqueue" ).(server.PGQueue ); ! ok || queue == nil {
335
+ return httpresponse .ErrInternalError .With ("Invalid task queue" )
336
+ } else {
337
+ certmanager .Queue = queue
338
+ }
339
+
340
+ return nil
246
341
}))
247
342
if err != nil {
248
343
return err
0 commit comments