From d0857e5f9be5f8fbf9024e5ca2a2308505ea8bf7 Mon Sep 17 00:00:00 2001 From: Googol Lee Date: Fri, 28 Jul 2023 17:14:22 +0200 Subject: [PATCH] update examples --- examples/nogenerics/main.go | 5 +++-- examples/telemetry/main.go | 23 +++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/nogenerics/main.go b/examples/nogenerics/main.go index b9e4d51..93ede76 100644 --- a/examples/nogenerics/main.go +++ b/examples/nogenerics/main.go @@ -4,6 +4,7 @@ package main import ( "context" + "errors" "net/http" "github.com/googollee/go-espresso" @@ -27,14 +28,14 @@ type Book struct { type BookService struct{} -func (b *BookService) bindToBook(user *User, book *Book) func(ctx context.Context, id string) error { +func (b *BookService) bindToBook(user *User, book *Book) espresso.BindFunc { return func(ctx context.Context, id string) error { book.ID = id book.Name = "existed_book" book.OwnedBy = "owner" if book.OwnedBy != user.ID { - return espresso.ErrWithStatus(http.StatusUnauthorized, "unauth") + return espresso.ErrWithStatus(http.StatusUnauthorized, errors.New("unauth")) } return nil diff --git a/examples/telemetry/main.go b/examples/telemetry/main.go index 64975bf..5816016 100644 --- a/examples/telemetry/main.go +++ b/examples/telemetry/main.go @@ -5,7 +5,6 @@ package main import ( "context" "net/http" - "os" "github.com/googollee/go-espresso" log "github.com/googollee/go-espresso/log" @@ -13,6 +12,7 @@ import ( openapi "github.com/googollee/go-espresso/openapi" tracing "github.com/googollee/go-espresso/tracing" opentelemetry "github.com/googollee/go-espresso/tracing/opentelemetry" + "golang.org/x/exp/slog" ) var rpcCalls = prometheus.IntGauge("rpcCalls") @@ -21,15 +21,15 @@ type Data struct{} type Service struct{} -func (s *Service) HandleCreate(ctx espresso.Context[Data]) error { - if err := ctx.Endpoint(http.MethodPost, "/service").End(); err != nil { - return espresso.ErrWithStatus(http.StatusBadRequest, err) - } - +func (s *Service) Create(ctx espresso.Context[Data]) error { return espresso.Procedure(ctx, s.Create) } -func (s *Service) Create(ctx context.Context, arg int) (string, error) { +func (s *Service) create(ctx context.Context, arg int) (string, error) { + if err := ctx.Endpoint(http.MethodPost, "/service").End(); err != nil { + return "", espresso.ErrWithStatus(http.StatusBadRequest, err) + } + rpcCalls.Add(1) ctx, span := tracing.Start(ctx) @@ -52,14 +52,13 @@ func (s *Service) Create(ctx context.Context, arg int) (string, error) { func main() { svc := &Service{} - eng := espresso.NewEngine( - log.New(os.Stderr, log.DEBUG), + svr := espresso.Default().With( + log.New(slog.Default()), prometheus.New("/metrics"), opentelemetry.New("https://url"), openapi.New("/spec"), ) - eng.HandleAll(svc) - - eng.ListenAndServe(":8080") + svr.HandleAll(svc) + svr.ListenAndServe(":8080") }