@@ -2,7 +2,7 @@ package main
22
33import (
44 "context"
5- "fmt "
5+ "errors "
66 "net/http"
77
88 // Packages
@@ -20,23 +20,24 @@ import (
2020 logger "github.com/mutablelogic/go-server/pkg/logger/config"
2121 pg "github.com/mutablelogic/go-server/pkg/pgmanager/config"
2222 pgqueue "github.com/mutablelogic/go-server/pkg/pgqueue/config"
23-
24- // Static content
25- helloworld "github.com/mutablelogic/go-server/npm/helloworld"
2623)
2724
2825///////////////////////////////////////////////////////////////////////////////
2926// TYPES
3027
3128type ServiceCommands struct {
32- Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
29+ Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
30+ Run2 ServiceRun2Command `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
3331}
3432
35- type ServiceRunCommand struct {
33+ type ServiceRun2Command struct {
3634 Plugins []string `help:"Plugin paths"`
37- Router struct {
35+ }
36+
37+ type ServiceRunCommand struct {
38+ Router struct {
3839 httprouter.Config `embed:"" prefix:"router."` // Router configuration
39- } `embed:""`
40+ } `embed:"" prefix:"" `
4041 Server struct {
4142 httpserver.Config `embed:"" prefix:"server."` // Server configuration
4243 } `embed:""`
@@ -55,24 +56,12 @@ type ServiceRunCommand struct {
5556 Log struct {
5657 logger.Config `embed:"" prefix:"log."` // Logger configuration
5758 } `embed:""`
58- HelloWorld struct {
59- helloworld.Config `embed:"" prefix:"helloworld."` // HelloWorld configuration
60- } `embed:""`
6159}
6260
6361///////////////////////////////////////////////////////////////////////////////
6462// PUBLIC METHODS
6563
6664func (cmd * ServiceRunCommand ) Run (app server.Cmd ) error {
67- // Load plugins
68- plugins , err := provider .LoadPluginsForPattern (cmd .Plugins ... )
69- if err != nil {
70- return err
71- }
72- for _ , plugin := range plugins {
73- fmt .Println ("TODO: Loaded plugins:" , plugin .Name ())
74- }
75-
7665 // Set the server listener and router prefix
7766 cmd .Server .Listen = app .GetEndpoint ()
7867 cmd .Router .Prefix = types .NormalisePath (cmd .Server .Listen .Path )
@@ -134,19 +123,6 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
134123 // Return the new configuration with the router
135124 return config , nil
136125
137- case "helloworld" :
138- config := plugin .(helloworld.Config )
139-
140- // Set the router
141- if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
142- return nil , httpresponse .ErrInternalError .Withf ("Invalid router %q" , "httprouter" )
143- } else {
144- config .Router = router
145- }
146-
147- // Return the new configuration with the router
148- return config , nil
149-
150126 case "auth" :
151127 config := plugin .(auth.Config )
152128
@@ -214,7 +190,41 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
214190
215191 // No-op
216192 return plugin , nil
217- }, cmd .Log .Config , cmd .Router .Config , cmd .Server .Config , cmd .HelloWorld .Config , cmd .Auth .Config , cmd .PGPool .Config , cmd .PGQueue .Config , cmd .CertManager .Config )
193+ }, cmd .Log .Config , cmd .Router .Config , cmd .Server .Config , cmd .Auth .Config , cmd .PGPool .Config , cmd .PGQueue .Config , cmd .CertManager .Config )
194+ if err != nil {
195+ return err
196+ }
197+
198+ // Run the provider
199+ return provider .Run (app .Context ())
200+ }
201+
202+ func (cmd * ServiceRun2Command ) Run (app server.Cmd ) error {
203+ // Create a provider by loading the plugins
204+ provider , err := provider .NewWithPlugins (nil , cmd .Plugins ... )
205+ if err != nil {
206+ return err
207+ }
208+
209+ // Create configurations
210+ err = errors .Join (err , provider .Load ("log" , "main" , func (config server.Plugin ) {
211+ logger := config .(* logger.Config )
212+ logger .Debug = app .GetDebug () >= server .Debug
213+ }))
214+ err = errors .Join (err , provider .Load ("httprouter" , "main" , func (config server.Plugin ) {
215+ httprouter := config .(* httprouter.Config )
216+ httprouter .Origin = "*"
217+ httprouter .Prefix = types .NormalisePath (app .GetEndpoint ().Path )
218+ }))
219+ err = errors .Join (err , provider .Load ("httpserver" , "main" , func (config server.Plugin ) {
220+ httpserver := config .(* httpserver.Config )
221+ httpserver .Listen = app .GetEndpoint ()
222+ }))
223+ err = errors .Join (err , provider .Load ("helloworld" , "main" , nil ))
224+ err = errors .Join (err , provider .Load ("auth" , "main" , nil ))
225+ err = errors .Join (err , provider .Load ("pgpool" , "main" , nil ))
226+ err = errors .Join (err , provider .Load ("pgqueue" , "main" , nil ))
227+ err = errors .Join (err , provider .Load ("certmanager" , "main" , nil ))
218228 if err != nil {
219229 return err
220230 }
0 commit comments