Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Jul 28, 2023
1 parent c10e079 commit d0857e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
5 changes: 3 additions & 2 deletions examples/nogenerics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"context"
"errors"
"net/http"

"github.com/googollee/go-espresso"
Expand All @@ -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
Expand Down
23 changes: 11 additions & 12 deletions examples/telemetry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ package main
import (
"context"
"net/http"
"os"

"github.com/googollee/go-espresso"
log "github.com/googollee/go-espresso/log"
prometheus "github.com/googollee/go-espresso/monitoring/prometheus"
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")
Expand All @@ -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)
Expand All @@ -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")
}

0 comments on commit d0857e5

Please sign in to comment.