Skip to content

Commit 32c91fa

Browse files
committed
Updated resolver
1 parent d8d68e5 commit 32c91fa

File tree

11 files changed

+198
-73
lines changed

11 files changed

+198
-73
lines changed

Makefile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ $(CMD_DIR): go-dep mkdir
5757
@echo Build command $(notdir $@) GOOS=${OS} GOARCH=${ARCH}
5858
@GOOS=${OS} GOARCH=${ARCH} ${GO} build ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@) ./$@
5959

60-
6160
# Build the plugins
6261
.PHONY: plugins
63-
plugins: $(PLUGIN_DIR) $(NPM_DIR)
62+
plugins: $(NPM_DIR) $(PLUGIN_DIR)
6463

6564
# Build the plugins
6665
$(PLUGIN_DIR): go-dep mkdir
@@ -69,9 +68,8 @@ $(PLUGIN_DIR): go-dep mkdir
6968

7069
# Build the NPM packages
7170
$(NPM_DIR): npm-dep
72-
@echo Build npm $(notdir $@) GOOS=${OS} GOARCH=${ARCH}
73-
@cd $@ && npm install && npm run prod
74-
@GOOS=${OS} GOARCH=${ARCH} ${GO} build -buildmode=plugin ${BUILD_FLAGS} -o ${BUILD_DIR}/$(notdir $@).plugin ./$@
71+
@echo Build npm $(notdir $@)
72+
@cd $@ && npm install && npm run dist
7573

7674
# Build the docker image
7775
.PHONY: docker
@@ -126,9 +124,13 @@ mkdir:
126124

127125
.PHONY: clean
128126
clean:
129-
@echo Clean
127+
@echo Clean $(BUILD_DIR)
130128
@rm -fr $(BUILD_DIR)
131129
@${GO} clean
130+
@for dir in $(NPM_DIR); do \
131+
echo "Cleaning npm $$dir"; \
132+
cd $$dir && ${NPM} run clean; \
133+
done
132134

133135
###############################################################################
134136
# DEPENDENCIES

cmd/server/service.go

Lines changed: 116 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2829
type 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

3334
type ServiceRun2Command struct {
3435
Plugins []string `help:"Plugin paths"`
3536
}
3637

38+
/*
3739
type 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+
/*
6467
func (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

202206
func (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

npm/helloworld/config.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package helloworld
22

33
import (
44
"context"
@@ -25,7 +25,7 @@ var _ server.Task = (*task)(nil)
2525
////////////////////////////////////////////////////////////////////////////////
2626
// GLOBALS
2727

28-
//go:embed dist
28+
//go:embed dist/*
2929
var fs embed.FS
3030

3131
////////////////////////////////////////////////////////////////////////////////
@@ -52,10 +52,6 @@ func (c Config) Description() string {
5252
return "Hello World example static content"
5353
}
5454

55-
func Plugin() server.Plugin {
56-
return Config{}
57-
}
58-
5955
////////////////////////////////////////////////////////////////////////////////
6056
// TASK
6157

npm/helloworld/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"dev": "esbuild src/index.ts src/index.html --bundle --loader:.html=copy --watch --outdir=dist --allow-overwrite --servedir=dist --banner:js=\"document.addEventListener('DOMContentLoaded', () => { new EventSource('/esbuild').addEventListener('change', () => location.reload()) });\"",
8-
"prod": "rm -fr dist && install -d dist && esbuild src/index.ts src/index.html --bundle --sourcemap --loader:.html=copy --outdir=dist --allow-overwrite"
8+
"dist": "rm -fr dist && install -d dist && esbuild src/index.ts src/index.html --bundle --sourcemap --loader:.html=copy --outdir=dist --allow-overwrite",
9+
"clean": "rm -fr dist"
910
},
1011
"dependencies": {
1112
"tslib": "^2.5.2",

pkg/parser/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
1111
ast "github.com/mutablelogic/go-server/pkg/parser/ast"
1212
jsonparser "github.com/mutablelogic/go-server/pkg/parser/jsonparser"
13-
meta "github.com/mutablelogic/go-server/pkg/parser/meta"
13+
meta "github.com/mutablelogic/go-server/pkg/provider/meta"
1414
)
1515

1616
////////////////////////////////////////////////////////////////////////////////

pkg/provider/meta/meta_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"testing"
55

66
// Packages
7-
8-
"github.com/mutablelogic/go-server/pkg/parser/meta"
7+
meta "github.com/mutablelogic/go-server/pkg/provider/meta"
98
assert "github.com/stretchr/testify/assert"
109
)
1110

pkg/provider/plugin.go

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

33
import (
4-
"context"
54
"errors"
65
"os"
76
"path/filepath"
@@ -29,6 +28,25 @@ const (
2928
func NewWithPlugins(paths ...string) (server.Provider, error) {
3029
self := new(provider)
3130

31+
/*
32+
// Change directory to the executable directory
33+
wd, err := os.Getwd()
34+
if err != nil {
35+
return nil, err
36+
}
37+
defer func() {
38+
os.Chdir(wd)
39+
}()
40+
execpath, err := os.Executable()
41+
if err != nil {
42+
return nil, err
43+
}
44+
execdir := filepath.Dir(execpath)
45+
if err := os.Chdir(execdir); err != nil {
46+
return nil, err
47+
}
48+
*/
49+
3250
// Load plugins
3351
plugins, err := loadPluginsForPattern(paths...)
3452
if err != nil {
@@ -38,6 +56,7 @@ func NewWithPlugins(paths ...string) (server.Provider, error) {
3856
// Create the prototype map
3957
self.protos = make(map[string]*meta.Meta, len(plugins))
4058
self.plugin = make(map[string]server.Plugin, len(plugins))
59+
self.resolvers = make(map[string]server.PluginResolverFunc, len(plugins))
4160
self.task = make(map[string]*state, len(plugins))
4261
self.Logger = logger.New(os.Stderr, logger.Term, false)
4362
for _, plugin := range plugins {
@@ -59,36 +78,35 @@ func NewWithPlugins(paths ...string) (server.Provider, error) {
5978

6079
// Create a "concrete" plugin from a prototype and a label, using the function to "hook"
6180
// any values into the plugin
62-
func (provider *provider) Load(name, label string, fn func(context.Context, server.Plugin)) error {
81+
func (provider *provider) Load(name, label string, fn server.PluginResolverFunc) error {
6382
proto, exists := provider.protos[name]
6483
if !exists {
6584
return httpresponse.ErrBadRequest.Withf("Plugin not found: %q", name)
6685
}
6786

6887
// Make the label
88+
// TODO: use types package to manage labels
6989
if label != "" {
7090
if !types.IsIdentifier(label) {
7191
return httpresponse.ErrBadRequest.Withf("Invalid label: %q", label)
7292
} else {
73-
label = name
93+
label = strings.Join([]string{name, label}, ".")
7494
}
7595
} else {
76-
label = strings.Join([]string{label, name}, ".")
96+
label = name
7797
}
7898

7999
// Register the plugin with the label
80100
if _, exists := provider.plugin[label]; exists {
81101
return httpresponse.ErrBadRequest.Withf("Plugin already exists: %q", label)
82-
} else {
83-
// Create a plugin from the prototype
84-
plugin := proto.New()
85-
if fn != nil {
86-
fn(context.TODO(), plugin)
87-
}
88-
provider.plugin[label] = plugin
89-
provider.porder = append(provider.porder, label)
90102
}
91103

104+
// Create a plugin from the prototype and set the resolver
105+
plugin := proto.New()
106+
provider.plugin[label] = plugin
107+
provider.resolvers[label] = fn
108+
provider.porder = append(provider.porder, label)
109+
92110
// Return success
93111
return nil
94112
}

0 commit comments

Comments
 (0)