@@ -4,13 +4,14 @@ import (
4
4
"context"
5
5
"errors"
6
6
"net/http"
7
+ "os"
7
8
8
9
// Packages
9
10
server "github.com/mutablelogic/go-server"
10
- "github.com/mutablelogic/go-server/npm/helloworld"
11
+ helloworld "github.com/mutablelogic/go-server/npm/helloworld"
11
12
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
12
13
provider "github.com/mutablelogic/go-server/pkg/provider"
13
- "github.com/mutablelogic/go-server/pkg/ref"
14
+ ref "github.com/mutablelogic/go-server/pkg/ref"
14
15
types "github.com/mutablelogic/go-server/pkg/types"
15
16
16
17
// Plugins
@@ -28,13 +29,174 @@ import (
28
29
29
30
type ServiceCommands struct {
30
31
// Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service"`
31
- Run ServiceRun2Command `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
32
+ Run ServiceRunCommand `cmd:"" group:"SERVICE" help:"Run the service with plugins"`
33
+ Config ServiceConfigCommand `cmd:"" group:"SERVICE" help:"Output the plugin configuration"`
32
34
}
33
35
34
- type ServiceRun2Command struct {
36
+ type ServiceRunCommand struct {
35
37
Plugins []string `help:"Plugin paths"`
36
38
}
37
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
+
38
200
/*
39
201
type ServiceRunCommand struct {
40
202
Router struct {
@@ -61,8 +223,6 @@ type ServiceRunCommand struct {
61
223
}
62
224
*/
63
225
64
- ///////////////////////////////////////////////////////////////////////////////
65
- // PUBLIC METHODS
66
226
/*
67
227
func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
68
228
// Set the server listener and router prefix
@@ -202,147 +362,3 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
202
362
return provider.Run(app.Context())
203
363
}
204
364
*/
205
-
206
- func (cmd * ServiceRun2Command ) Run (app server.Cmd ) error {
207
- // Create a provider by loading the plugins
208
- provider , err := provider .NewWithPlugins (cmd .Plugins ... )
209
- if err != nil {
210
- return err
211
- }
212
-
213
- // Set the configuration
214
- err = errors .Join (err , provider .Load ("log" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
215
- logger := config .(* logger.Config )
216
- logger .Debug = app .GetDebug () >= server .Debug
217
- return nil
218
- }))
219
- err = errors .Join (err , provider .Load ("httprouter" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
220
- httprouter := config .(* httprouter.Config )
221
- httprouter .Prefix = types .NormalisePath (app .GetEndpoint ().Path )
222
- httprouter .Origin = "*"
223
- httprouter .Middleware = []string {"log.main" }
224
- return nil
225
- }))
226
- err = errors .Join (err , provider .Load ("httpserver" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
227
- httpserver := config .(* httpserver.Config )
228
- httpserver .Listen = app .GetEndpoint ()
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
239
- }))
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
252
- }))
253
- err = errors .Join (err , provider .Load ("pgpool" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
254
- pgpool := config .(* pg.Config )
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
276
- }))
277
- err = errors .Join (err , provider .Load ("auth" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
278
- auth := config .(* auth.Config )
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
296
- }))
297
- err = errors .Join (err , provider .Load ("pgqueue" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
298
- pgqueue := config .(* pgqueue.Config )
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
315
- }))
316
- err = errors .Join (err , provider .Load ("certmanager" , "main" , func (ctx context.Context , label string , config server.Plugin ) error {
317
- certmanager := config .(* cert.Config )
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
341
- }))
342
- if err != nil {
343
- return err
344
- }
345
-
346
- // Run the provider
347
- return provider .Run (app .Context ())
348
- }
0 commit comments