Skip to content

Commit 93b4a2d

Browse files
authored
Merge pull request #96 from mutablelogic/v5
Updated endpoint semantics
2 parents 88a182d + 7bd5c39 commit 93b4a2d

File tree

9 files changed

+51
-198
lines changed

9 files changed

+51
-198
lines changed

cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Cmd interface {
2323
GetDebug() DebugLevel
2424

2525
// Return the endpoint
26-
GetEndpoint(paths ...string) *url.URL
26+
GetEndpoint(paths ...string) (*url.URL, error)
2727

2828
// Return the HTTP client options
2929
GetClientOpts() []client.ClientOpt

cmd/server/service.go

Lines changed: 7 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
6767
return err
6868
}
6969

70+
endpoint, err := app.GetEndpoint()
71+
if err != nil {
72+
return err
73+
}
74+
7075
// Set the configuration
7176
err = errors.Join(err, provider.Load("log", "main", func(ctx context.Context, label string, config server.Plugin) error {
7277
logger := config.(*logger.Config)
@@ -75,14 +80,14 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
7580
}))
7681
err = errors.Join(err, provider.Load("httprouter", "main", func(ctx context.Context, label string, config server.Plugin) error {
7782
httprouter := config.(*httprouter.Config)
78-
httprouter.Prefix = types.NormalisePath(app.GetEndpoint().Path)
83+
httprouter.Prefix = types.NormalisePath(endpoint.Path)
7984
httprouter.Origin = "*"
8085
httprouter.Middleware = []string{"log.main"}
8186
return nil
8287
}))
8388
err = errors.Join(err, provider.Load("httpserver", "main", func(ctx context.Context, label string, config server.Plugin) error {
8489
httpserver := config.(*httpserver.Config)
85-
httpserver.Listen = app.GetEndpoint()
90+
httpserver.Listen = endpoint
8691

8792
// Set router
8893
if router, ok := provider.Task(ctx, "httprouter.main").(http.Handler); !ok || router == nil {
@@ -232,169 +237,3 @@ func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
232237
// Run the provider
233238
return provider.Run(app.Context())
234239
}
235-
236-
/*
237-
type ServiceRunCommand struct {
238-
Router struct {
239-
httprouter.Config `embed:"" prefix:"router."` // Router configuration
240-
} `embed:"" prefix:""`
241-
Server struct {
242-
httpserver.Config `embed:"" prefix:"server."` // Server configuration
243-
} `embed:""`
244-
Auth struct {
245-
auth.Config `embed:"" prefix:"auth."` // Auth configuration
246-
} `embed:""`
247-
PGPool struct {
248-
pg.Config `embed:"" prefix:"pg."` // Postgresql configuration
249-
} `embed:""`
250-
PGQueue struct {
251-
pgqueue.Config `embed:"" prefix:"pgqueue."` // Postgresql queue configuration
252-
} `embed:""`
253-
CertManager struct {
254-
cert.Config `embed:"" prefix:"cert."` // Certificate manager configuration
255-
} `embed:""`
256-
Log struct {
257-
logger.Config `embed:"" prefix:"log."` // Logger configuration
258-
} `embed:""`
259-
}
260-
*/
261-
262-
/*
263-
func (cmd *ServiceRunCommand) Run(app server.Cmd) error {
264-
// Set the server listener and router prefix
265-
cmd.Server.Listen = app.GetEndpoint()
266-
cmd.Router.Prefix = types.NormalisePath(cmd.Server.Listen.Path)
267-
268-
// Create a provider and resolve references
269-
provider, err := provider.New(func(ctx context.Context, label string, plugin server.Plugin) (server.Plugin, error) {
270-
ref.Log(ctx).Debugf(ctx, "Resolving %q", label)
271-
switch label {
272-
case "log":
273-
config := plugin.(logger.Config)
274-
config.Debug = app.GetDebug() >= server.Debug
275-
return config, nil
276-
277-
case "certmanager":
278-
config := plugin.(cert.Config)
279-
280-
// Set the router
281-
if router, ok := ref.Provider(ctx).Task(ctx, "httprouter").(server.HTTPRouter); !ok || router == nil {
282-
return nil, httpresponse.ErrInternalError.Withf("Invalid router %q", "httprouter")
283-
} else {
284-
config.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 nil, httpresponse.ErrInternalError.Withf("Invalid connection pool %q", "pgpool")
290-
} else {
291-
config.Pool = pool
292-
}
293-
294-
// Set the queue
295-
if queue, ok := ref.Provider(ctx).Task(ctx, "pgqueue").(server.PGQueue); !ok || queue == nil {
296-
return nil, httpresponse.ErrInternalError.Withf("Invalid connection pool %q", "pgqueue")
297-
} else {
298-
config.Queue = queue
299-
}
300-
301-
return config, nil
302-
303-
case "httpserver":
304-
config := plugin.(httpserver.Config)
305-
306-
// Set the router
307-
if router, ok := ref.Provider(ctx).Task(ctx, "httprouter").(http.Handler); !ok || router == nil {
308-
return nil, httpresponse.ErrInternalError.Withf("Invalid router %q", "httprouter")
309-
} else {
310-
config.Router = router
311-
}
312-
313-
// Return the new configuration with the router
314-
return config, nil
315-
316-
case "httprouter":
317-
config := plugin.(httprouter.Config)
318-
319-
// Set the middleware
320-
config.Middleware = []string{}
321-
322-
// Return the new configuration with the router
323-
return config, nil
324-
325-
case "auth":
326-
config := plugin.(auth.Config)
327-
328-
// Set the router
329-
if router, ok := ref.Provider(ctx).Task(ctx, "httprouter").(server.HTTPRouter); !ok || router == nil {
330-
return nil, httpresponse.ErrInternalError.Withf("Invalid router %q", "httprouter")
331-
} else {
332-
config.Router = router
333-
}
334-
335-
// Set the connection pool
336-
if pool, ok := ref.Provider(ctx).Task(ctx, "pgpool").(server.PG); !ok || pool == nil {
337-
return nil, httpresponse.ErrInternalError.Withf("Invalid connection pool %q", "pgpool")
338-
} else {
339-
config.Pool = pool
340-
}
341-
342-
// Return the new configuration
343-
return config, nil
344-
345-
case "pgqueue":
346-
config := plugin.(pgqueue.Config)
347-
348-
// Set the router
349-
if router, ok := ref.Provider(ctx).Task(ctx, "httprouter").(server.HTTPRouter); !ok || router == nil {
350-
return nil, httpresponse.ErrInternalError.Withf("Invalid router %q", "httprouter")
351-
} else {
352-
config.Router = router
353-
}
354-
355-
// Set the connection pool
356-
if pool, ok := ref.Provider(ctx).Task(ctx, "pgpool").(server.PG); !ok || pool == nil {
357-
return nil, httpresponse.ErrInternalError.Withf("Invalid connection pool %q", "pgpool")
358-
} else {
359-
config.Pool = pool
360-
}
361-
362-
// Return the new configuration
363-
return config, nil
364-
365-
case "pgpool":
366-
config := plugin.(pg.Config)
367-
368-
// Set the router
369-
if router, ok := ref.Provider(ctx).Task(ctx, "httprouter").(server.HTTPRouter); !ok || router == nil {
370-
return nil, httpresponse.ErrInternalError.Withf("Invalid router %q", "httprouter")
371-
} else {
372-
config.Router = router
373-
}
374-
375-
// Set trace
376-
if app.GetDebug() == server.Trace {
377-
config.Trace = func(ctx context.Context, query string, args any, err error) {
378-
if err != nil {
379-
ref.Log(ctx).With("args", args).Print(ctx, err, " ON ", query)
380-
} else {
381-
ref.Log(ctx).With("args", args).Debug(ctx, query)
382-
}
383-
}
384-
}
385-
386-
// Return the new configuration with the router
387-
return config, nil
388-
}
389-
390-
// No-op
391-
return plugin, nil
392-
}, cmd.Log.Config, cmd.Router.Config, cmd.Server.Config, cmd.Auth.Config, cmd.PGPool.Config, cmd.PGQueue.Config, cmd.CertManager.Config)
393-
if err != nil {
394-
return err
395-
}
396-
397-
// Run the provider
398-
return provider.Run(app.Context())
399-
}
400-
*/

etc/docker/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ RUN apt update -y && apt install -y ca-certificates
2525
LABEL org.opencontainers.image.source=https://${SOURCE}
2626

2727
# Entrypoint when running the server
28-
ENV \
29-
PLUGIN_PATH="/usr/local/bin/*.plugin" \
30-
ENDPOINT="http://0.0.0.0:80/"
28+
ENV PLUGIN_PATH="/usr/local/bin/*.plugin" \
29+
ENDPOINT="http://:80/"
3130
EXPOSE 80 443
3231
ENTRYPOINT [ "/usr/local/bin/server" ]

pkg/auth/cmd/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313

1414
func run(ctx server.Cmd, fn func(context.Context, *client.Client) error) error {
1515
// Create a client
16-
provider, err := client.New(ctx.GetEndpoint("${AUTH_PREFIX}").String(), ctx.GetClientOpts()...)
17-
if err != nil {
16+
if endpoint, err := ctx.GetEndpoint(); err != nil {
1817
return err
18+
} else if provider, err := client.New(endpoint.String(), ctx.GetClientOpts()...); err != nil {
19+
return err
20+
} else {
21+
// Run the function
22+
return fn(ctx.Context(), provider)
1923
}
20-
// Run the function
21-
return fn(ctx.Context(), provider)
2224
}

pkg/cert/cmd/run.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@ import (
66
// Packages
77
server "github.com/mutablelogic/go-server"
88
client "github.com/mutablelogic/go-server/pkg/cert/client"
9+
schema "github.com/mutablelogic/go-server/pkg/cert/schema"
910
)
1011

1112
///////////////////////////////////////////////////////////////////////////////
1213
// PRIVATE METHODS
1314

1415
func run(ctx server.Cmd, fn func(context.Context, *client.Client) error) error {
1516
// Create a client
16-
provider, err := client.New(ctx.GetEndpoint("${CERT_PREFIX}").String(), ctx.GetClientOpts()...)
17-
if err != nil {
17+
if endpoint, err := ctx.GetEndpoint(schema.APIPrefix); err != nil {
1818
return err
19+
} else if provider, err := client.New(endpoint.String(), ctx.GetClientOpts()...); err != nil {
20+
return err
21+
} else {
22+
// Run the function
23+
return fn(ctx.Context(), provider)
1924
}
20-
// Run the function
21-
return fn(ctx.Context(), provider)
2225
}

pkg/cmd/app.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,26 @@ func (app *app) Context() context.Context {
7575
}
7676

7777
func (app *app) GetDebug() server.DebugLevel {
78-
if app.Debug {
79-
return server.Debug
80-
}
8178
if app.Trace {
8279
return server.Trace
8380
}
81+
if app.Debug {
82+
return server.Debug
83+
}
8484
return server.None
8585
}
8686

87-
func (app *app) GetEndpoint(paths ...string) *url.URL {
87+
func (app *app) GetEndpoint(paths ...string) (*url.URL, error) {
8888
url, err := url.Parse(app.Endpoint)
8989
if err != nil {
90-
return nil
90+
return nil, err
9191
}
9292
for _, path := range paths {
9393
url.Path = types.JoinPath(url.Path, os.Expand(path, func(key string) string {
9494
return app.vars[key]
9595
}))
9696
}
97-
return url
97+
return url, nil
9898
}
9999

100100
func (app *app) GetClientOpts() []client.ClientOpt {

pkg/ldap/cmd/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414

1515
func run(ctx server.Cmd, fn func(context.Context, *client.Client) error) error {
1616
// Create a client
17-
provider, err := client.New(ctx.GetEndpoint(schema.APIPrefix).String(), ctx.GetClientOpts()...)
18-
if err != nil {
17+
if endpoint, err := ctx.GetEndpoint(schema.APIPrefix); err != nil {
1918
return err
19+
} else if provider, err := client.New(endpoint.String(), ctx.GetClientOpts()...); err != nil {
20+
return err
21+
} else {
22+
// Run the function
23+
return fn(ctx.Context(), provider)
2024
}
21-
// Run the function
22-
return fn(ctx.Context(), provider)
2325
}

pkg/pgmanager/cmd/run.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import (
66
// Packages
77
server "github.com/mutablelogic/go-server"
88
client "github.com/mutablelogic/go-server/pkg/pgmanager/client"
9+
schema "github.com/mutablelogic/go-server/pkg/pgmanager/schema"
910
)
1011

1112
///////////////////////////////////////////////////////////////////////////////
1213
// PRIVATE METHODS
1314

1415
func run(ctx server.Cmd, fn func(context.Context, *client.Client) error) error {
1516
// Create a client
16-
provider, err := client.New(ctx.GetEndpoint("${PG_PREFIX}").String(), ctx.GetClientOpts()...)
17-
if err != nil {
17+
if endpoint, err := ctx.GetEndpoint(schema.APIPrefix); err != nil {
1818
return err
19+
} else if provider, err := client.New(endpoint.String(), ctx.GetClientOpts()...); err != nil {
20+
return err
21+
} else {
22+
// Run the function
23+
return fn(ctx.Context(), provider)
1924
}
20-
// Run the function
21-
return fn(ctx.Context(), provider)
25+
2226
}

pkg/pgqueue/cmd/run.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import (
66
// Packages
77
server "github.com/mutablelogic/go-server"
88
client "github.com/mutablelogic/go-server/pkg/pgqueue/client"
9+
schema "github.com/mutablelogic/go-server/pkg/pgqueue/schema"
910
)
1011

1112
///////////////////////////////////////////////////////////////////////////////
1213
// PRIVATE METHODS
1314

1415
func run(ctx server.Cmd, fn func(context.Context, *client.Client) error) error {
1516
// Create a client
16-
provider, err := client.New(ctx.GetEndpoint("${QUEUE_PREFIX}").String(), ctx.GetClientOpts()...)
17-
if err != nil {
17+
if endpoint, err := ctx.GetEndpoint(schema.APIPrefix); err != nil {
1818
return err
19+
} else if provider, err := client.New(endpoint.String(), ctx.GetClientOpts()...); err != nil {
20+
return err
21+
} else {
22+
// Run the function
23+
return fn(ctx.Context(), provider)
1924
}
20-
// Run the function
21-
return fn(ctx.Context(), provider)
25+
2226
}

0 commit comments

Comments
 (0)