@@ -2,11 +2,13 @@ package main
22
33import (
44 "context"
5- "fmt "
5+ "errors "
66 "net/http"
7+ "os"
78
89 // Packages
910 server "github.com/mutablelogic/go-server"
11+ helloworld "github.com/mutablelogic/go-server/npm/helloworld"
1012 httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
1113 provider "github.com/mutablelogic/go-server/pkg/provider"
1214 ref "github.com/mutablelogic/go-server/pkg/ref"
@@ -20,23 +22,186 @@ import (
2022 logger "github.com/mutablelogic/go-server/pkg/logger/config"
2123 pg "github.com/mutablelogic/go-server/pkg/pgmanager/config"
2224 pgqueue "github.com/mutablelogic/go-server/pkg/pgqueue/config"
23-
24- // Static content
25- helloworld "github.com/mutablelogic/go-server/npm/helloworld"
2625)
2726
2827///////////////////////////////////////////////////////////////////////////////
2928// TYPES
3029
3130type ServiceCommands struct {
32- Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
31+ // Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
32+ Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
33+ Config ServiceConfigCommand `cmd:"" group:"SERVICE" help:"Output the plugin configuration"`
3334}
3435
3536type ServiceRunCommand struct {
36- Plugins []string `help:"Plugin paths"`
37- Router struct {
37+ Plugins []string `help:"Plugin paths" env:"PLUGIN_PATH"`
38+ }
39+
40+ type ServiceConfigCommand struct {
41+ ServiceRunCommand
42+ }
43+
44+ ///////////////////////////////////////////////////////////////////////////////
45+ // PUBLIC METHODS
46+
47+ func (cmd * ServiceConfigCommand ) Run (app server.Cmd ) error {
48+ // Create a provider by loading the plugins
49+ provider , err := provider .NewWithPlugins (cmd .Plugins ... )
50+ if err != nil {
51+ return err
52+ }
53+ return provider .WriteConfig (os .Stdout )
54+ }
55+
56+ func (cmd * ServiceRunCommand ) Run (app server.Cmd ) error {
57+ // Create a provider by loading the plugins
58+ provider , err := provider .NewWithPlugins (cmd .Plugins ... )
59+ if err != nil {
60+ return err
61+ }
62+
63+ // Set the configuration
64+ err = errors .Join (err , provider .Load ("log" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
65+ logger := config .(* logger.Config )
66+ logger .Debug = app .GetDebug () >= server .Debug
67+ return nil
68+ }))
69+ err = errors .Join (err , provider .Load ("httprouter" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
70+ httprouter := config .(* httprouter.Config )
71+ httprouter .Prefix = types .NormalisePath (app .GetEndpoint ().Path )
72+ httprouter .Origin = "*"
73+ httprouter .Middleware = []string {"log.main" }
74+ return nil
75+ }))
76+ err = errors .Join (err , provider .Load ("httpserver" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
77+ httpserver := config .(* httpserver.Config )
78+ httpserver .Listen = app .GetEndpoint ()
79+
80+ // Set router
81+ if router , ok := provider .Task (ctx , "httprouter.main" ).(http.Handler ); ! ok || router == nil {
82+ return httpresponse .ErrInternalError .With ("Invalid router" )
83+ } else {
84+ httpserver .Router = router
85+ }
86+
87+ // Return success
88+ return nil
89+ }))
90+ err = errors .Join (err , provider .Load ("helloworld" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
91+ helloworld := config .(* helloworld.Config )
92+
93+ // Set router
94+ if router , ok := provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter ); ! ok || router == nil {
95+ return httpresponse .ErrInternalError .With ("Invalid router" )
96+ } else {
97+ helloworld .Router = router
98+ }
99+
100+ // Return success
101+ return nil
102+ }))
103+ err = errors .Join (err , provider .Load ("pgpool" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
104+ pgpool := config .(* pg.Config )
105+
106+ // Set router
107+ if router , ok := provider .Task (ctx , "httprouter.main" ).(server.HTTPRouter ); ! ok || router == nil {
108+ return httpresponse .ErrInternalError .With ("Invalid router" )
109+ } else {
110+ pgpool .Router = router
111+ }
112+
113+ // Set trace
114+ if app .GetDebug () == server .Trace {
115+ pgpool .Trace = func (ctx context.Context , query string , args any , err error ) {
116+ if err != nil {
117+ ref .Log (ctx ).With ("args" , args ).Print (ctx , err , " ON " , query )
118+ } else {
119+ ref .Log (ctx ).With ("args" , args ).Debug (ctx , query )
120+ }
121+ }
122+ }
123+
124+ // Return success
125+ return nil
126+ }))
127+ err = errors .Join (err , provider .Load ("auth" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
128+ auth := config .(* auth.Config )
129+
130+ // Set the router
131+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
132+ return httpresponse .ErrInternalError .With ("Invalid router" )
133+ } else {
134+ auth .Router = router
135+ }
136+
137+ // Set the connection pool
138+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
139+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
140+ } else {
141+ auth .Pool = pool
142+ }
143+
144+ // Return success
145+ return nil
146+ }))
147+ err = errors .Join (err , provider .Load ("pgqueue" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
148+ pgqueue := config .(* pgqueue.Config )
149+
150+ // Set the router
151+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
152+ return httpresponse .ErrInternalError .With ("Invalid router" )
153+ } else {
154+ pgqueue .Router = router
155+ }
156+
157+ // Set the connection pool
158+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
159+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
160+ } else {
161+ pgqueue .Pool = pool
162+ }
163+
164+ return nil
165+ }))
166+ err = errors .Join (err , provider .Load ("certmanager" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
167+ certmanager := config .(* cert.Config )
168+
169+ // Set the router
170+ if router , ok := ref .Provider (ctx ).Task (ctx , "httprouter" ).(server.HTTPRouter ); ! ok || router == nil {
171+ return httpresponse .ErrInternalError .With ("Invalid router" )
172+ } else {
173+ certmanager .Router = router
174+ }
175+
176+ // Set the connection pool
177+ if pool , ok := ref .Provider (ctx ).Task (ctx , "pgpool" ).(server.PG ); ! ok || pool == nil {
178+ return httpresponse .ErrInternalError .With ("Invalid connection pool" )
179+ } else {
180+ certmanager .Pool = pool
181+ }
182+
183+ // Set the queue
184+ if queue , ok := ref .Provider (ctx ).Task (ctx , "pgqueue" ).(server.PGQueue ); ! ok || queue == nil {
185+ return httpresponse .ErrInternalError .With ("Invalid task queue" )
186+ } else {
187+ certmanager .Queue = queue
188+ }
189+
190+ return nil
191+ }))
192+ if err != nil {
193+ return err
194+ }
195+
196+ // Run the provider
197+ return provider .Run (app .Context ())
198+ }
199+
200+ /*
201+ type ServiceRunCommand struct {
202+ Router struct {
38203 httprouter.Config `embed:"" prefix:"router."` // Router configuration
39- } `embed:""`
204+ } `embed:"" prefix:"" `
40205 Server struct {
41206 httpserver.Config `embed:"" prefix:"server."` // Server configuration
42207 } `embed:""`
@@ -55,24 +220,11 @@ type ServiceRunCommand struct {
55220 Log struct {
56221 logger.Config `embed:"" prefix:"log."` // Logger configuration
57222 } `embed:""`
58- HelloWorld struct {
59- helloworld.Config `embed:"" prefix:"helloworld."` // HelloWorld configuration
60- } `embed:""`
61223}
224+ */
62225
63- ///////////////////////////////////////////////////////////////////////////////
64- // PUBLIC METHODS
65-
226+ /*
66227func (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-
76228 // Set the server listener and router prefix
77229 cmd.Server.Listen = app.GetEndpoint()
78230 cmd.Router.Prefix = types.NormalisePath(cmd.Server.Listen.Path)
@@ -134,19 +286,6 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
134286 // Return the new configuration with the router
135287 return config, nil
136288
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-
150289 case "auth":
151290 config := plugin.(auth.Config)
152291
@@ -214,11 +353,12 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
214353
215354 // No-op
216355 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 )
356+ }, cmd.Log.Config, cmd.Router.Config, cmd.Server.Config, cmd.Auth.Config, cmd.PGPool.Config, cmd.PGQueue.Config, cmd.CertManager.Config)
218357 if err != nil {
219358 return err
220359 }
221360
222361 // Run the provider
223362 return provider.Run(app.Context())
224363}
364+ */
0 commit comments