Skip to content

Commit

Permalink
add request id middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jun 28, 2023
1 parent e22b463 commit 1f9440d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/paulmach/go.geojson v1.4.0
github.com/pkg/errors v0.9.1
github.com/ravilushqa/otelgqlgen v0.8.0
github.com/reearth/reearthx v0.0.0-20230628083626-a5efdcea5ff3
github.com/reearth/reearthx v0.0.0-20230628094334-9f836a79dabb
github.com/samber/lo v1.27.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ github.com/reearth/reearthx v0.0.0-20230530055440-86a8d4675853 h1:eO1u6WJ/B1j/uC
github.com/reearth/reearthx v0.0.0-20230530055440-86a8d4675853/go.mod h1:Rh7MJPKq43f+HZ/PwjZ5vEbGPpllNFvUrxn9sBn2b+s=
github.com/reearth/reearthx v0.0.0-20230628083626-a5efdcea5ff3 h1:TWAum5PQ1lNOg+TfBFL9lpw9Z0V7d0TCqxNQTQHOUoQ=
github.com/reearth/reearthx v0.0.0-20230628083626-a5efdcea5ff3/go.mod h1:Rh7MJPKq43f+HZ/PwjZ5vEbGPpllNFvUrxn9sBn2b+s=
github.com/reearth/reearthx v0.0.0-20230628094334-9f836a79dabb h1:tCYnqv0N2kwx90fMjUHEnsYfbiChJGeXdl+NMHPJCxY=
github.com/reearth/reearthx v0.0.0-20230628094334-9f836a79dabb/go.mod h1:O4zFr6ks8jDKfgy9NBpZKp31jpmAPnfOtMQK9B6xX54=
github.com/robertkrimen/godocdown v0.0.0-20130622164427-0bfa04905481/go.mod h1:C9WhFzY47SzYBIvzFqSvHIR6ROgDo4TtdTuRaOMjF/s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down
15 changes: 3 additions & 12 deletions server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
logger := log.NewEcho()
e.Logger = logger
e.Use(
logger.AccessLogger(),
middleware.Recover(),
otelecho.Middleware("reearth"),
echo.WrapMiddleware(appx.RequestIDMiddleware()),
logger.AccessLogger(),
)
if cfg.Config.HTTPSREDIRECT {
e.Use(middleware.HTTPSRedirectWithConfig(middleware.RedirectConfig{
Expand Down Expand Up @@ -139,7 +140,6 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {

func errorHandler(next func(error, echo.Context)) func(error, echo.Context) {
return func(err error, c echo.Context) {
ctx := c.Request().Context()
if c.Response().Committed {
return
}
Expand All @@ -148,17 +148,8 @@ func errorHandler(next func(error, echo.Context)) func(error, echo.Context) {
err = rerror.ErrNotFound
}

if err := rerror.UnwrapErrInternal(err); err != nil {
au := adapter.GetAuthInfo(ctx)
u := adapter.User(ctx)
op := adapter.Operator(ctx)
c.Echo().Logger.Errorf("AUTH: %#v", au)
c.Echo().Logger.Errorf("USER: %#v", u)
c.Echo().Logger.Errorf("OP: %#v", op)
}

code, msg := errorMessage(err, func(f string, args ...interface{}) {
c.Echo().Logger.Errorf(f, args...)
log.Errorfc(c.Request().Context(), f, args...)
})
if err := c.JSON(code, map[string]string{
"error": msg,
Expand Down
9 changes: 8 additions & 1 deletion server/internal/app/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearth/server/pkg/user"
"github.com/reearth/reearth/server/pkg/workspace"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/rerror"
"github.com/reearth/reearthx/util"
)
Expand Down Expand Up @@ -37,6 +38,9 @@ func attachOpMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
if u, ok := ctx.Value(contextUser).(string); ok {
userID = u
}
if au != nil {
log.Debugfc(ctx, "auth: info: %#v", au)
}

// debug mode
if cfg.Debug {
Expand Down Expand Up @@ -78,13 +82,16 @@ func attachOpMiddleware(cfg *ServerConfig) echo.MiddlewareFunc {
}

if u != nil {
ctx = adapter.AttachUser(ctx, u)
log.Debugfc(ctx, "auth: user: id=%s name=%s email=%s", u.ID(), u.Name(), u.Email())

op, err := generateOperator(ctx, cfg, u)
if err != nil {
return err
}

ctx = adapter.AttachUser(ctx, u)
ctx = adapter.AttachOperator(ctx, op)
log.Debugfc(ctx, "auth: op: %#v", op)
}

c.SetRequest(req.WithContext(ctx))
Expand Down

0 comments on commit 1f9440d

Please sign in to comment.