Skip to content

Commit

Permalink
feat: add client
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Mar 31, 2023
1 parent 18cc1e8 commit f49f81c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
65 changes: 65 additions & 0 deletions internal/cmd/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"context"
"os"
"time"

"github.com/go-faster/errors"
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/go-faster/simon/internal/app"
"github.com/go-faster/simon/internal/oas"
"github.com/go-faster/simon/sdk/zctx"
)

func cmdClient() *cobra.Command {
return &cobra.Command{
Use: "client",
Short: "Run a HTTP client",
Run: func(cmd *cobra.Command, args []string) {
app.Run(func(ctx context.Context, logger *zap.Logger, m *app.Metrics) error {
addr := os.Getenv("SERVER_ADDR")
if addr == "" {
addr = "http://localhost:8080"
}
c, err := oas.NewClient(addr,
oas.WithMeterProvider(m.MeterProvider()),
oas.WithTracerProvider(m.TracerProvider()),
)
if err != nil {
return errors.Wrap(err, "client")
}
ticker := time.NewTicker(time.Second)
tracer := m.TracerProvider().Tracer("")
tick := func() {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

ctx, span := tracer.Start(ctx, "client.tick")
defer span.End()

lg := zctx.From(ctx)
lg.Info("Sending request")

status, err := c.Status(ctx)
if err != nil {
lg.Error("Request failed", zap.Error(err))
return
}
lg.Info("Request succeeded", zap.String("message", status.Message))
}
tick()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
tick()
}
}
})
},
}
}
1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Root() *cobra.Command {
}
cmd.AddCommand(
cmdServer(),
cmdClient(),
)
return cmd
}

0 comments on commit f49f81c

Please sign in to comment.