Skip to content

Commit d8d68e5

Browse files
committed
Updated
1 parent 72f1256 commit d8d68e5

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

cmd/server/service.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,30 +201,49 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
201201

202202
func (cmd *ServiceRun2Command) Run(app server.Cmd) error {
203203
// Create a provider by loading the plugins
204-
provider, err := provider.NewWithPlugins(nil, cmd.Plugins...)
204+
provider, err := provider.NewWithPlugins(cmd.Plugins...)
205205
if err != nil {
206206
return err
207207
}
208208

209-
// Create configurations
210-
err = errors.Join(err, provider.Load("log", "main", func(config server.Plugin) {
209+
// Set the configuration
210+
err = errors.Join(err, provider.Load("log", "main", func(ctx context.Context, config server.Plugin) {
211211
logger := config.(*logger.Config)
212212
logger.Debug = app.GetDebug() >= server.Debug
213213
}))
214-
err = errors.Join(err, provider.Load("httprouter", "main", func(config server.Plugin) {
214+
err = errors.Join(err, provider.Load("httprouter", "main", func(ctx context.Context, config server.Plugin) {
215215
httprouter := config.(*httprouter.Config)
216-
httprouter.Origin = "*"
217216
httprouter.Prefix = types.NormalisePath(app.GetEndpoint().Path)
217+
httprouter.Origin = "*"
218+
httprouter.Middleware = []string{"log.main"}
218219
}))
219-
err = errors.Join(err, provider.Load("httpserver", "main", func(config server.Plugin) {
220+
err = errors.Join(err, provider.Load("httpserver", "main", func(ctx context.Context, config server.Plugin) {
220221
httpserver := config.(*httpserver.Config)
221222
httpserver.Listen = app.GetEndpoint()
223+
httpserver.Router = provider.Task(ctx, "httprouter.main").(http.Handler)
224+
}))
225+
err = errors.Join(err, provider.Load("helloworld", "main", func(ctx context.Context, config server.Plugin) {
226+
// NO-OP
227+
}))
228+
err = errors.Join(err, provider.Load("pgpool", "main", func(ctx context.Context, config server.Plugin) {
229+
pgpool := config.(*pg.Config)
230+
pgpool.Router = provider.Task(ctx, "httprouter.main").(server.HTTPRouter)
231+
}))
232+
err = errors.Join(err, provider.Load("auth", "main", func(ctx context.Context, config server.Plugin) {
233+
auth := config.(*auth.Config)
234+
auth.Pool = provider.Task(ctx, "pgpool.main").(server.PG)
235+
auth.Router = provider.Task(ctx, "httprouter.main").(server.HTTPRouter)
236+
}))
237+
err = errors.Join(err, provider.Load("pgqueue", "main", func(ctx context.Context, config server.Plugin) {
238+
pgqueue := config.(*pgqueue.Config)
239+
pgqueue.Pool = provider.Task(ctx, "pgpool.main").(server.PG)
240+
pgqueue.Router = provider.Task(ctx, "httprouter.main").(server.HTTPRouter)
241+
}))
242+
err = errors.Join(err, provider.Load("certmanager", "main", func(ctx context.Context, config server.Plugin) {
243+
certmanager := config.(*cert.Config)
244+
certmanager.Router = provider.Task(ctx, "httprouter.main").(server.HTTPRouter)
245+
certmanager.Pool = provider.Task(ctx, "pgpool.main").(server.PG)
222246
}))
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))
228247
if err != nil {
229248
return err
230249
}

pkg/provider/plugin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider
22

33
import (
4+
"context"
45
"errors"
56
"os"
67
"path/filepath"
@@ -25,7 +26,7 @@ const (
2526
///////////////////////////////////////////////////////////////////////////////
2627
// PUBLIC METHODS
2728

28-
func NewWithPlugins(resolver ResolverFunc, paths ...string) (server.Provider, error) {
29+
func NewWithPlugins(paths ...string) (server.Provider, error) {
2930
self := new(provider)
3031

3132
// Load plugins
@@ -38,7 +39,6 @@ func NewWithPlugins(resolver ResolverFunc, paths ...string) (server.Provider, er
3839
self.protos = make(map[string]*meta.Meta, len(plugins))
3940
self.plugin = make(map[string]server.Plugin, len(plugins))
4041
self.task = make(map[string]*state, len(plugins))
41-
self.resolver = resolver
4242
self.Logger = logger.New(os.Stderr, logger.Term, false)
4343
for _, plugin := range plugins {
4444
name := plugin.Name()
@@ -59,7 +59,7 @@ func NewWithPlugins(resolver ResolverFunc, paths ...string) (server.Provider, er
5959

6060
// Create a "concrete" plugin from a prototype and a label, using the function to "hook"
6161
// any values into the plugin
62-
func (provider *provider) Load(name, label string, fn func(server.Plugin)) error {
62+
func (provider *provider) Load(name, label string, fn func(context.Context, server.Plugin)) error {
6363
proto, exists := provider.protos[name]
6464
if !exists {
6565
return httpresponse.ErrBadRequest.Withf("Plugin not found: %q", name)
@@ -83,7 +83,7 @@ func (provider *provider) Load(name, label string, fn func(server.Plugin)) error
8383
// Create a plugin from the prototype
8484
plugin := proto.New()
8585
if fn != nil {
86-
fn(plugin)
86+
fn(context.TODO(), plugin)
8787
}
8888
provider.plugin[label] = plugin
8989
provider.porder = append(provider.porder, label)

plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Provider interface {
3838
Task
3939

4040
// Load a plugin by name and label
41-
Load(string, string, func(config Plugin)) error
41+
Load(string, string, func(context.Context, Plugin)) error
4242

4343
// Return a task given a plugin label
4444
Task(context.Context, string) Task

0 commit comments

Comments
 (0)