Skip to content

Commit

Permalink
Migrated everything to Logger interface (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
ripls56 authored Oct 29, 2024
2 parents a9250ae + 09c9daa commit 3b9b0a3
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 102 deletions.
3 changes: 1 addition & 2 deletions apps/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0
github.com/h2non/bimg v1.1.9
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
Expand All @@ -37,7 +38,6 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/h2non/bimg v1.1.9 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
Expand All @@ -56,7 +56,6 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 0 additions & 10 deletions apps/server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand All @@ -134,8 +132,6 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/oauth2 v0.20.0 h1:4mQdhULixXKP1rwYBW0vAijoXnkTG0BLCDRzfe1idMo=
Expand All @@ -153,8 +149,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -168,16 +162,12 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 h1:e7S5W7MGGLaSu8j3YjdezkZ+m1/Nm0uRVRMEMGk26Xs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
Expand Down
35 changes: 15 additions & 20 deletions apps/server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/taskemapp/server/apps/server/internal/app/grpcfx"
"github.com/taskemapp/server/apps/server/internal/app/profilefx"
"github.com/taskemapp/server/apps/server/internal/logger"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"net/url"

"github.com/go-redis/redis/v8"
Expand All @@ -29,21 +29,9 @@ const (
)

var App = fx.Options(
//TODO(ripls56): temp, will be fixed in next pr
fx.Provide(fx.Annotate(func() (logger.Logger, error) {
c := zap.NewProductionConfig()
c.OutputPaths = []string{"stdout"}
c.ErrorOutputPaths = []string{"stderr"}

l, err := logger.New(&c)
if err != nil {
return nil, err
}
return l, err
}, fx.As(new(logger.Logger)))),
fx.Provide(fx.Annotate(setupLogger, fx.As(new(logger.Logger)))),

fx.Provide(setupConfig),
fx.Provide(setupLogger),
fx.Provide(setupPgPool),
fx.Provide(setupRabbitMq),
fx.Provide(setupRedisClient),
Expand Down Expand Up @@ -81,19 +69,26 @@ func setupConfig() (config.Config, error) {
return cfg, nil
}

func setupLogger(c config.Config) *zap.Logger {
var log *zap.Logger
func setupLogger(c config.Config) (logger.Logger, error) {
var zc zap.Config

switch c.AppEnv {
case envDev:
log, _ = zap.NewDevelopment()
zc = zap.NewDevelopmentConfig()
case envProd:
log, _ = zap.NewProduction()
zc = zap.NewProductionConfig()
default:
log, _ = zap.NewDevelopment()
zc = zap.NewDevelopmentConfig()
}

return log
zc.OutputPaths = []string{"stdout"}
zc.ErrorOutputPaths = []string{"stderr"}

l, err := logger.New(&zc)
if err != nil {
return nil, err
}
return l, err
}

func setupPgPool(c config.Config) (*pgxpool.Pool, error) {
Expand Down
11 changes: 10 additions & 1 deletion apps/server/internal/app/authfx/appfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package authfx
import (
"github.com/taskemapp/server/apps/server/internal/config"
authsrv "github.com/taskemapp/server/apps/server/internal/grpc/auth"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/pkg/notifier"
"github.com/taskemapp/server/apps/server/internal/repository/token"
"github.com/taskemapp/server/apps/server/internal/repository/user"
Expand All @@ -11,9 +12,17 @@ import (
"go.uber.org/fx"
)

const module = "auth"

var App = fx.Options(
fx.Module(
"auth",
module,
fx.Decorate(
func(l logger.Logger) logger.Logger {
return l.WithScope(module)
},
),

fx.Provide(
fx.Private,
fx.Annotate(
Expand Down
13 changes: 10 additions & 3 deletions apps/server/internal/app/grpcfx/appfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/selector"
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/grpc/interceptor"
"github.com/taskemapp/server/apps/server/internal/logger"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/repository/token"
v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1"
"go.uber.org/fx"
Expand All @@ -23,9 +23,16 @@ import (
"strconv"
)

const module = "grpc"

var App = fx.Options(
fx.Module(
"grpc",
module,
fx.Decorate(
func(l logger.Logger) logger.Logger {
return l.WithScope(module)
},
),

fx.Provide(
fx.Private,
Expand All @@ -36,7 +43,7 @@ var App = fx.Options(
),

fx.Invoke(
func(lc fx.Lifecycle, log *zap.Logger, c config.Config, server GrpcServer) {
func(lc fx.Lifecycle, log logger.Logger, c config.Config, server GrpcServer) {
lc.Append(
fx.Hook{
OnStart: func(ctx context.Context) error {
Expand Down
10 changes: 6 additions & 4 deletions apps/server/internal/app/profilefx/appfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ package profilefx

import (
profilesrv "github.com/taskemapp/server/apps/server/internal/grpc/profile"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/pkg/s3"
"github.com/taskemapp/server/apps/server/internal/repository/user"
"github.com/taskemapp/server/apps/server/internal/repository/user_file"
"github.com/taskemapp/server/apps/server/internal/service/profile"
"github.com/taskemapp/server/apps/server/internal/service/profile/image"
v1 "github.com/taskemapp/server/apps/server/tools/gen/grpc/v1"
"go.uber.org/fx"
"go.uber.org/zap"
)

const module = "profile"

var App = fx.Options(
fx.Module(
"profile",
module,
fx.Decorate(
func(l *zap.Logger) *zap.Logger {
return l.With(zap.String("scope", "profile"))
func(l logger.Logger) logger.Logger {
return l.WithScope(module)
},
),

Expand Down
10 changes: 9 additions & 1 deletion apps/server/internal/app/taskfx/appfx.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package taskfx

import (
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/repository/task"
"go.uber.org/fx"
)

const module = "task"

var App = fx.Options(
fx.Module(
"task",
module,
fx.Decorate(
func(l logger.Logger) logger.Logger {
return l.WithScope(module)
},
),
fx.Provide(
fx.Private,
fx.Annotate(task.NewPgx, fx.As(new(task.Repository))),
Expand Down
11 changes: 10 additions & 1 deletion apps/server/internal/app/teamfx/appfx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teamfx

import (
teamserver "github.com/taskemapp/server/apps/server/internal/grpc/team"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/repository/team"
"github.com/taskemapp/server/apps/server/internal/repository/team_member"
"github.com/taskemapp/server/apps/server/internal/repository/token"
Expand All @@ -10,9 +11,17 @@ import (
"go.uber.org/fx"
)

const module = "team"

var App = fx.Options(
fx.Module(
"team",
module,
fx.Decorate(
func(l logger.Logger) logger.Logger {
return l.WithScope(module)
},
),

fx.Provide(
fx.Private,
fx.Annotate(team.NewPgx, fx.As(new(team.Repository))),
Expand Down
9 changes: 5 additions & 4 deletions apps/server/internal/grpc/auth/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/grpc/interceptor"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/pkg/validation"
"github.com/taskemapp/server/apps/server/internal/repository/token"
"github.com/taskemapp/server/apps/server/internal/repository/user"
Expand All @@ -21,15 +22,15 @@ import (
type Opts struct {
fx.In
Auth auth.Service
Logger *zap.Logger
Logger logger.Logger
Config config.Config
TokenRepo token.Repository
}

type Server struct {
v1.UnimplementedAuthServer
auth auth.Service
logger *zap.Logger
logger logger.Logger
config config.Config
tokenRepo token.Repository
}
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *Server) Login(
})

if err != nil {
s.logger.Sugar().Error(err)
s.logger.Error("", zap.Error(err))
switch {
case errors.Is(err, user.ErrNotFound):
return nil, status.Error(codes.NotFound, "Not found")
Expand Down Expand Up @@ -115,7 +116,7 @@ func (s *Server) SignUp(
Password: req.Password,
})
if err != nil {
s.logger.Sugar().Error(err)
s.logger.Error("", zap.Error(err))
return nil, status.Error(codes.Internal, err.Error())
}

Expand Down
4 changes: 0 additions & 4 deletions apps/server/internal/grpc/interceptor/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"google.golang.org/grpc/status"
)

type CtxKey struct {
key string
}

// Auth get token from grpc request metadata
//
// Already throws formated grpc with status.Errorf
Expand Down
12 changes: 8 additions & 4 deletions apps/server/internal/grpc/interceptor/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"google.golang.org/grpc/status"
)

type ctxKey struct {
key string
}

func provideUserID(ctx context.Context, payload jwt.Claims) (context.Context, error) {
uid, err := uuid.Parse(payload["uid"].(string))
if err != nil {
Expand All @@ -21,12 +25,12 @@ func provideUserID(ctx context.Context, payload jwt.Claims) (context.Context, er
logging.Fields{"user_id", uid.String()},
)

return context.WithValue(ctx, CtxKey{"uid"}, uid), nil
return context.WithValue(ctx, ctxKey{"uid"}, uid), nil
}

// GetUserID from context, only throws ErrGetUserID if uid not found in context
func GetUserID(ctx context.Context) (uuid.UUID, error) {
if uid, ok := ctx.Value(CtxKey{"uid"}).(uuid.UUID); ok {
if uid, ok := ctx.Value(ctxKey{"uid"}).(uuid.UUID); ok {
return uid, nil
}

Expand All @@ -44,12 +48,12 @@ func provideReqID(ctx context.Context) (context.Context, error) {
logging.Fields{"request_id", rid.String()},
)

return context.WithValue(ctx, CtxKey{"rid"}, rid), nil
return context.WithValue(ctx, ctxKey{"rid"}, rid), nil
}

// GetRequestID from context, only throws ErrRequestID if uid not found in context
func GetRequestID(ctx context.Context) (uuid.UUID, error) {
if uid, ok := ctx.Value(CtxKey{"uid"}).(uuid.UUID); ok {
if uid, ok := ctx.Value(ctxKey{"uid"}).(uuid.UUID); ok {
return uid, nil
}

Expand Down
6 changes: 3 additions & 3 deletions apps/server/internal/grpc/interceptor/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package interceptor

import (
"github.com/taskemapp/server/apps/server/internal/config"
"github.com/taskemapp/server/apps/server/internal/pkg/logger"
"github.com/taskemapp/server/apps/server/internal/repository/token"
"go.uber.org/fx"
"go.uber.org/zap"
)

type Opts struct {
fx.In
Config config.Config
TokenRepo token.Repository
Logger *zap.Logger
Logger logger.Logger
}

type Interceptor struct {
c config.Config
tokenRepo token.Repository
logger *zap.Logger
logger logger.Logger
}

func New(opts Opts) *Interceptor {
Expand Down
Loading

0 comments on commit 3b9b0a3

Please sign in to comment.