@@ -7,9 +7,10 @@ import (
77
88 // Packages
99 server "github.com/mutablelogic/go-server"
10+ "github.com/mutablelogic/go-server/npm/helloworld"
1011 httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
1112 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"
1314 types "github.com/mutablelogic/go-server/pkg/types"
1415
1516 // Plugins
@@ -26,14 +27,15 @@ import (
2627// TYPES
2728
2829type 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"`
3132}
3233
3334type ServiceRun2Command struct {
3435 Plugins []string `help:"Plugin paths"`
3536}
3637
38+ /*
3739type ServiceRunCommand struct {
3840 Router struct {
3941 httprouter.Config `embed:"" prefix:"router."` // Router configuration
@@ -57,10 +59,11 @@ type ServiceRunCommand struct {
5759 logger.Config `embed:"" prefix:"log."` // Logger configuration
5860 } `embed:""`
5961}
62+ */
6063
6164///////////////////////////////////////////////////////////////////////////////
6265// PUBLIC METHODS
63-
66+ /*
6467func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
6568 // Set the server listener and router prefix
6669 cmd.Server.Listen = app.GetEndpoint()
@@ -198,6 +201,7 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
198201 // Run the provider
199202 return provider.Run(app.Context())
200203}
204+ */
201205
202206func (cmd * ServiceRun2Command ) Run (app server.Cmd ) error {
203207 // Create a provider by loading the plugins
@@ -207,42 +211,133 @@ func (cmd *ServiceRun2Command) Run(app server.Cmd) error {
207211 }
208212
209213 // 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 {
211215 logger := config .(* logger.Config )
212216 logger .Debug = app .GetDebug () >= server .Debug
217+ return nil
213218 }))
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 {
215220 httprouter := config .(* httprouter.Config )
216221 httprouter .Prefix = types .NormalisePath (app .GetEndpoint ().Path )
217222 httprouter .Origin = "*"
218223 httprouter .Middleware = []string {"log.main" }
224+ return nil
219225 }))
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 {
221227 httpserver := config .(* httpserver.Config )
222228 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
224239 }))
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
227252 }))
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 {
229254 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
231276 }))
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 {
233278 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
236296 }))
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 {
238298 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
241315 }))
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 {
243317 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
246341 }))
247342 if err != nil {
248343 return err
0 commit comments