Skip to content

Commit

Permalink
feat(rest/auth): add new example
Browse files Browse the repository at this point in the history
  • Loading branch information
jochumdev committed Feb 24, 2025
1 parent a7eef09 commit 0bc7d68
Show file tree
Hide file tree
Showing 23 changed files with 1,415 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ func (m *GoOrb) Update(ctx context.Context, root *dagger.Directory) (*AllResult,
WithEnvVariable("GOPROXY", "direct").
WithEnvVariable("GOSUMDB", "off").
WithExec([]string{"go", "get", "-u", "-t", "./..."}).
WithExec([]string{"go", "get", "-u", "github.com/go-orb/go-orb@main"}).
WithExec([]string{"bash", "-c", "for m in $(grep github.com/go-orb/plugins go.mod | grep -E -v \"^module\" | awk '{ print $1 }'); do go get -u \"${m}@main\"; done"}).
WithExec([]string{"go", "get", "-u", "github.com/go-orb/go-orb@latest"}).
WithExec([]string{"bash", "-c", "for m in $(grep github.com/go-orb/plugins go.mod | grep -E -v \"^module\" | awk '{ print $1 }'); do go get -u \"${m}@latest\"; done"}).
WithExec([]string{"go", "mod", "tidy", "-go=1.23.6"})

stdout, err := c.Stdout(ctx)
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ Contains examples and benchmarks for go-orb.

## Available examples

### [benchmarks/event](benchmarks/event)
### [benchmarks](benchmarks/)

#### [benchmarks/event](benchmarks/event)

A benchmark for running RPC requests over the event plugins, currently theres only the natsjs backend.

### [benchmarks/rps](benchmarks/rps)
#### [benchmarks/rps](benchmarks/rps)

A benchmark for running requests-per-second (rps) for a go-orb/server.

The rps benchmark sends X bytes (default `1000`) to server which echoes it to the client.

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

### [event/simple](event/simple)

A simple example of RPC requests over the event plugins, currently theres only the natsjs backend.

### [rest/auth](rest/auth)

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

This example doesn't utilize the go-orb config system, just to show it's possible to configure a go-orb service without it.

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

A simple example of a go-orb service and client with a REST middleware.
Expand Down
1 change: 1 addition & 0 deletions rest/auth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
72 changes: 72 additions & 0 deletions rest/auth/cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Package main contains a go-orb client which uses a fake login server.
package main

import (
"context"
"errors"

"github.com/go-orb/go-orb/client"
"github.com/go-orb/go-orb/log"
"github.com/go-orb/go-orb/types"
"github.com/go-orb/go-orb/util/metadata"
"google.golang.org/protobuf/types/known/emptypb"

// Own imports.
authproto "github.com/go-orb/examples/rest/auth/proto/auth"

_ "github.com/go-orb/plugins/codecs/json"
_ "github.com/go-orb/plugins/codecs/proto"
_ "github.com/go-orb/plugins/log/slog"

_ "github.com/go-orb/plugins-experimental/registry/mdns"

_ "github.com/go-orb/plugins/client/middleware/log"
_ "github.com/go-orb/plugins/client/orb"
_ "github.com/go-orb/plugins/client/orb/transport/drpc"
)

func runner(
logger log.Logger,
clientWire client.Type,
) error {
// Create a request.
req := &authproto.LoginRequest{Username: "someUserName", Password: "changeMe"}

// Run the query.
authClient := authproto.NewAuthClient(clientWire)
tokenResp, err := authClient.Login(context.Background(), "orb.examples.rest.auth.server", req)

if err != nil {
logger.Error("while requesting", "error", err)
return err
}

ctx, md := metadata.WithOutgoing(context.Background())
md["authorization"] = "Bearer " + tokenResp.GetToken()

introspectResponse, err := authClient.Introspect(ctx, "orb.examples.rest.auth.server", &emptypb.Empty{})
if err != nil {
logger.Error("while requesting", "error", err)
return err
}

if introspectResponse.GetUsername() != req.GetUsername() {
logger.Error("while requesting", "expected", req.GetUsername(), "got", introspectResponse.GetUsername())
return errors.New("bad response")
}

logger.Info("all good")

return nil
}

func main() {
var (
serviceName = types.ServiceName("orb.examples.rest.auth.client")
serviceVersion = types.ServiceVersion("v0.0.1")
)

if _, err := run(serviceName, serviceVersion, runner); err != nil {
log.Error("while running", "err", err)
}
}
100 changes: 100 additions & 0 deletions rest/auth/cmd/client/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build wireinject
// +build wireinject

package main

import (
"context"
"os"

"github.com/go-orb/go-orb/client"
"github.com/go-orb/go-orb/log"
"github.com/go-orb/go-orb/registry"
"github.com/go-orb/go-orb/types"
"github.com/go-orb/wire"
)

// provideLoggerOpts returns the logger options.
func provideLoggerOpts() ([]log.Option, error) {
return []log.Option{log.WithLevel("TRACE")}, nil
}

func provideClientOpts() ([]client.Option, error) {
return []client.Option{client.WithClientMiddleware(client.MiddlewareConfig{Name: "log"})}, nil
}

// provideComponents creates a slice of components out of the arguments.
func provideComponents(
logger log.Logger,
client client.Type,
) ([]types.Component, error) {
components := []types.Component{}
components = append(components, logger)
components = append(components, client)

return components, nil
}

type wireRunResult string

type wireRunCallback func(
logger log.Logger,
client client.Type,
) error

func wireRun(
_ types.ServiceName,
components []types.Component,
_ types.ConfigData,
logger log.Logger,
client client.Type,
cb wireRunCallback,
) (wireRunResult, error) {
//
// Orb start
for _, c := range components {
err := c.Start()
if err != nil {
log.Error("Failed to start", err, "component", c.Type())
os.Exit(1)
}
}

//
// Actual code
runErr := cb(logger, client)

//
// Orb shutdown.
ctx := context.Background()

for k := range components {
c := components[len(components)-1-k]

err := c.Stop(ctx)
if err != nil {
log.Error("Failed to stop", err, "component", c.Type())
}
}

return "", runErr
}

// run combines everything above and
func run(
serviceName types.ServiceName,
serviceVersion types.ServiceVersion,
cb wireRunCallback,
) (wireRunResult, error) {
panic(wire.Build(
wire.Value([]types.ConfigData{}),
provideLoggerOpts,
log.Provide,
wire.Value([]registry.Option{}),
registry.Provide,
provideClientOpts,
client.Provide,
provideComponents,
wireRun,
))
}
131 changes: 131 additions & 0 deletions rest/auth/cmd/client/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0bc7d68

Please sign in to comment.