Skip to content

Commit e2b025c

Browse files
committed
chore(config/source/cli): Replace it with "cli", refs issue go-orb/go-orb#17
1 parent 72fcb61 commit e2b025c

File tree

57 files changed

+2340
-1295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2340
-1295
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ The rps benchmark sends X bytes (default `1000`) to server which echoes it to th
1818

1919
See the [WIKI](https://github.com/go-orb/go-orb/wiki/RPC-Benchmarks) for results.
2020

21+
### [cmd/foobar](cmd/foobar)
22+
23+
A example on howto run multiple services in a monolith.
24+
2125
### [event/simple](event/simple)
2226

2327
A simple example of RPC requests over the event plugins, currently theres only the natsjs backend.
@@ -26,14 +30,26 @@ A simple example of RPC requests over the event plugins, currently theres only t
2630

2731
A simple example of a go-orb service and client with an auth REST API.
2832

29-
This example doesn't utilize the go-orb config system, just to show it's possible to configure a go-orb service without it.
33+
This example doesn't utilize the go-orb config system, just to show it's possible to configure a go-orb service without it. It has the smallest binary size of all examples.
3034

3135
### [rest/middleware](rest/middleware)
3236

3337
A simple example of a go-orb service and client with a REST middleware.
3438

3539
In it's [config](rest/middleware/config) folder you can find a variaty of config files for different transports as well as logging options and registries. All of them run with the same code/binary.
3640

41+
Run the server with:
42+
43+
```bash
44+
go run ./cmd/server/... --config ./config/grpc.yaml
45+
```
46+
47+
And the client with:
48+
49+
```bash
50+
go run ./cmd/client/... --config ./config/grpc.yaml
51+
```
52+
3753
## Running
3854

3955
To run an example you need to have `go1.23.6` or later installed. You can get it with [gvm](https://github.com/moovweb/gvm).
Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
// Package main contains a handler which acts as server for the request client.
1+
// Package main contains a simple handler/server example for a event run.
22
package main
33

44
import (
5-
"context"
5+
"fmt"
66
"os"
77

8-
"github.com/go-orb/examples/benchmarks/event/pb/echo"
9-
"github.com/go-orb/go-orb/event"
10-
"github.com/go-orb/go-orb/log"
11-
"github.com/go-orb/go-orb/types"
12-
_ "github.com/go-orb/plugins/codecs/goccyjson"
8+
"github.com/go-orb/go-orb/cli"
9+
_ "github.com/go-orb/plugins/codecs/json"
1310
_ "github.com/go-orb/plugins/codecs/proto"
14-
_ "github.com/go-orb/plugins/config/source/cli/urfave"
1511
_ "github.com/go-orb/plugins/event/natsjs"
1612
_ "github.com/go-orb/plugins/log/slog"
1713
)
1814

19-
func runner(
20-
serviceName types.ServiceName,
21-
serviceVersion types.ServiceVersion,
22-
logger log.Logger,
23-
eventWire event.Handler,
24-
done chan os.Signal,
25-
) error {
26-
echoHandler := func(_ context.Context, req *echo.Req) (*echo.Resp, error) {
27-
return &echo.Resp{Payload: req.GetPayload()}, nil
15+
func main() {
16+
app := cli.App{
17+
Name: "benchmarks.event.handler",
18+
Version: "",
19+
Usage: "A event benchmarking handler",
20+
NoAction: false,
21+
Flags: []*cli.Flag{
22+
{
23+
Name: "log_level",
24+
Default: "INFO",
25+
EnvVars: []string{"LOG_LEVEL"},
26+
ConfigPaths: []cli.FlagConfigPath{{Path: []string{"logger", "level"}}},
27+
Usage: "Set the log level, one of TRACE, DEBUG, INFO, WARN, ERROR",
28+
},
29+
},
30+
Commands: []*cli.Command{},
2831
}
2932

30-
ctx, cancel := context.WithCancel(context.Background())
31-
32-
event.HandleRequest(ctx, eventWire, "echo.echo", echoHandler)
33-
34-
logger.Info("Started", "name", serviceName, "version", serviceVersion)
35-
36-
// Blocks until sigterm/sigkill.
37-
<-done
38-
39-
logger.Info("Stopping", "name", serviceName, "version", serviceVersion)
40-
41-
cancel()
42-
43-
return nil
44-
}
45-
46-
func main() {
47-
var (
48-
serviceName = types.ServiceName("benchmarks.event.handler")
49-
serviceVersion = types.ServiceVersion("v0.0.1")
50-
)
33+
appContext := cli.NewAppContext(&app)
5134

52-
if _, err := run(serviceName, serviceVersion, runner); err != nil {
53-
log.Error("while running", "err", err)
35+
_, err := run(appContext, os.Args)
36+
if err != nil {
37+
fmt.Printf("run error: %s\n", err)
38+
os.Exit(1)
5439
}
5540
}

benchmarks/event/cmd/handler/wire.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,80 @@ package main
66
import (
77
"context"
88
"fmt"
9-
"os"
10-
"os/signal"
11-
"syscall"
129

10+
"github.com/go-orb/examples/benchmarks/event/pb/echo"
11+
"github.com/go-orb/go-orb/cli"
1312
"github.com/go-orb/go-orb/event"
1413
"github.com/go-orb/go-orb/log"
1514
"github.com/go-orb/go-orb/types"
16-
"github.com/go-orb/plugins/config/source/cli/urfave"
15+
"github.com/go-orb/plugins/cli/urfave"
1716
"github.com/go-orb/wire"
1817
)
1918

2019
// wireRunResult is here so "wire" has a type for the return value of wireRun.
21-
// wire needs a explicit type for each provider including "wireRun".
22-
type wireRunResult string
23-
24-
// wireRunCallback is the actual code that runs the business logic.
25-
type wireRunCallback func(
26-
serviceName types.ServiceName,
27-
serviceVersion types.ServiceVersion,
28-
logger log.Logger,
29-
event event.Handler,
30-
done chan os.Signal,
31-
) error
20+
type wireRunResult struct{}
3221

3322
func wireRun(
34-
serviceName types.ServiceName,
35-
serviceVersion types.ServiceVersion,
23+
serviceContext *cli.ServiceContext,
3624
components *types.Components,
3725
logger log.Logger,
38-
event event.Handler,
39-
cb wireRunCallback,
26+
eventHandler event.Handler,
4027
) (wireRunResult, error) {
4128
// Orb start
4229
for _, c := range components.Iterate(false) {
43-
err := c.Start()
30+
logger.Debug("Starting", "component", fmt.Sprintf("%s/%s", c.Type(), c.String()))
31+
32+
err := c.Start(serviceContext.Context())
4433
if err != nil {
4534
logger.Error("Failed to start", "error", err, "component", fmt.Sprintf("%s/%s", c.Type(), c.String()))
46-
return "", err
35+
return wireRunResult{}, fmt.Errorf("failed to start component %s/%s: %w", c.Type(), c.String(), err)
4736
}
4837
}
4938

50-
done := make(chan os.Signal, 1)
51-
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
52-
5339
//
5440
// Actual code
55-
runErr := cb(serviceName, serviceVersion, logger, event, done)
41+
echoHandler := func(_ context.Context, req *echo.Req) (*echo.Resp, error) {
42+
return &echo.Resp{Payload: req.GetPayload()}, nil
43+
}
44+
45+
event.HandleRequest(serviceContext.Context(), eventHandler, "echo.echo", echoHandler)
46+
47+
// Blocks until interrupt
48+
<-serviceContext.Context().Done()
5649

5750
// Orb shutdown.
5851
ctx := context.Background()
5952

6053
for _, c := range components.Iterate(true) {
54+
logger.Debug("Stopping", "component", fmt.Sprintf("%s/%s", c.Type(), c.String()))
55+
6156
err := c.Stop(ctx)
6257
if err != nil {
6358
logger.Error("Failed to stop", "error", err, "component", fmt.Sprintf("%s/%s", c.Type(), c.String()))
6459
}
6560
}
6661

67-
return "", runErr
62+
return wireRunResult{}, nil
6863
}
6964

70-
// run combines everything above and
7165
func run(
72-
serviceName types.ServiceName,
73-
serviceVersion types.ServiceVersion,
74-
cb wireRunCallback,
66+
appContext *cli.AppContext,
67+
args []string,
7568
) (wireRunResult, error) {
7669
panic(wire.Build(
70+
urfave.ProvideParser,
71+
cli.ProvideParsedFlagsFromArgs,
72+
73+
cli.ProvideSingleServiceContext,
7774
types.ProvideComponents,
78-
urfave.ProvideConfigData,
79-
wire.Value([]log.Option{}),
80-
log.Provide,
81-
wire.Value([]event.Option{}),
82-
event.Provide,
75+
76+
cli.ProvideConfigData,
77+
cli.ProvideServiceName,
78+
79+
log.ProvideNoOpts,
80+
81+
event.ProvideNoOpts,
82+
8383
wireRun,
8484
))
8585
}

benchmarks/event/cmd/handler/wire_gen.go

Lines changed: 46 additions & 46 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)