Skip to content

Commit

Permalink
feat: work on skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien Rigaud committed Feb 18, 2024
1 parent 3016352 commit a9dcb09
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ run:
update:
cd src && go mod tidy

test:
cd src/tests/integration && go test -v
unit-test:
cd src && go test -v -cover ./...
10 changes: 7 additions & 3 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/rs/zerolog/log"

"github.com/Golerplate/user-store-svc/internal/config"
serviceDatastore "github.com/Golerplate/user-store-svc/internal/datastore/planetscale"
handlers_grpc "github.com/Golerplate/user-store-svc/internal/handlers/grpc"
"github.com/Golerplate/user-store-svc/internal/service"
serviceDatastore "github.com/Golerplate/user-store-svc/internal/service/datastore/planetscale"
service "github.com/Golerplate/user-store-svc/internal/service/v1"
)

func main() {
Expand All @@ -28,7 +28,11 @@ func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)

userStoreServiceDatastore := serviceDatastore.NewPlanetScaleDatastore()
userStoreService := service.NewUserStoreService(userStoreServiceDatastore)
userStoreService, err := service.NewUserStoreService(ctx, userStoreServiceDatastore)
if err != nil {
log.Fatal().Err(err).
Msg("main: unable to create user store service")
}

grpcServer, err := handlers_grpc.NewServer(ctx, cfg.GRPCServerConfig, userStoreService)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module github.com/Golerplate/user-store-svc
go 1.21.5

require (
github.com/Golerplate/contracts v0.0.2
github.com/Golerplate/pkg v0.0.2
github.com/bufbuild/connect-go v1.10.0
github.com/bufbuild/connect-grpcreflect-go v1.1.0
github.com/caarlos0/env/v8 v8.0.0
Expand All @@ -16,9 +14,20 @@ require (
)

require (
github.com/Golerplate/pkg v0.0.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/oklog/ulid/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/Golerplate/contracts v0.0.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.4.0
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
18 changes: 18 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
github.com/Golerplate/contracts v0.0.2 h1:nII6q/QV2QjeTYw4g21G8eT2lzIA6ZXGFHKGQkGiL2k=
github.com/Golerplate/contracts v0.0.2/go.mod h1:ALtjMgMcSLE6wGcB2zGIWand81y0HsU+MIvn1mmBP+0=
github.com/Golerplate/contracts v0.0.6 h1:nyi3f+VOe8KBq8tNpesGApxxDn09AU8XVnoSzlc/k5E=
github.com/Golerplate/contracts v0.0.6/go.mod h1:Oe4DvVdK81pPH9sAMmJLw0wd3k8HhmuqHAG1aC3x4Zo=
github.com/Golerplate/pkg v0.0.2 h1:b1T2Rky4BaJ5c9bHeh0cVII8M1LWcvKfzDJkGmT/G5w=
github.com/Golerplate/pkg v0.0.2/go.mod h1:xj+tuGKV3MtOpy/B6SMBJ0EkbaXMaivufhce/SzwqRk=
github.com/Golerplate/pkg v0.0.7 h1:RBF4dgVcBhKTAWUOQeJZPC7/10UpoFXB9q5F/aYk/oQ=
github.com/Golerplate/pkg v0.0.7/go.mod h1:CGbdv/CDLgQnQJau3oHJHu3cdA4U2yZR1T0zJ4tbyBQ=
github.com/bufbuild/connect-go v1.10.0 h1:QAJ3G9A1OYQW2Jbk3DeoJbkCxuKArrvZgDt47mjdTbg=
github.com/bufbuild/connect-go v1.10.0/go.mod h1:CAIePUgkDR5pAFaylSMtNK45ANQjp9JvpluG20rhpV8=
github.com/bufbuild/connect-grpcreflect-go v1.1.0 h1:T0FKu1y9zZW4cjHuF+Q7jIN6ek8HTpCxOP8ZsORZICg=
github.com/bufbuild/connect-grpcreflect-go v1.1.0/go.mod h1:AxcY2fSAr+oQQuu+K35qy2VDtX+LWr7SrS2SvfjY898=
github.com/caarlos0/env/v8 v8.0.0 h1:POhxHhSpuxrLMIdvTGARuZqR4Jjm8AYmoi/JKlcScs0=
github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand All @@ -24,11 +30,20 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -43,3 +58,6 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package planetscale

import (
"github.com/Golerplate/user-store-svc/internal/service/datastore"
"github.com/Golerplate/user-store-svc/internal/datastore"
_ "github.com/go-sql-driver/mysql"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package planetscale

import (
"context"
"time"

entities_user_v1 "github.com/Golerplate/user-store-svc/internal/entities/user/v1"
)

func (d *dbClient) CreateUser(ctx context.Context, req *entities_user_v1.CreateUserRequest) (*entities_user_v1.User, error) {
return nil, nil
return &entities_user_v1.User{
ID: "1",
Username: req.Username,
Email: req.Email,
IsAdmin: false,
IsBanned: false,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}, nil
}
6 changes: 3 additions & 3 deletions src/internal/handlers/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"net/http"

"github.com/Golerplate/contracts/generated/services/servicesconnect"
"github.com/Golerplate/contracts/generated/services/user/store/v1/storev1connect"
"github.com/Golerplate/contracts/generated/services/user/store/svc/v1/svcv1connect"
"github.com/Golerplate/pkg/grpc"
pkghandlers "github.com/Golerplate/pkg/grpc/handlers"
sharedmidlewares "github.com/Golerplate/pkg/grpc/interceptors"
"github.com/Golerplate/user-store-svc/internal/handlers"
handlers_grpc_user_v1 "github.com/Golerplate/user-store-svc/internal/handlers/grpc/user/v1"
"github.com/Golerplate/user-store-svc/internal/service"
service "github.com/Golerplate/user-store-svc/internal/service/v1"
connectgo "github.com/bufbuild/connect-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -47,7 +47,7 @@ func (s *grpcServer) Setup(ctx context.Context) error {

mux := http.NewServeMux()
mux.Handle(servicesconnect.NewHealthServiceHandler(pkghandlers.NewHealthHandler()))
mux.Handle(storev1connect.NewUserStoreSvcHandler(userStoreServiceHandler, interceptors))
mux.Handle(svcv1connect.NewUserStoreSvcHandler(userStoreServiceHandler, interceptors))
mux.Handle(grpcreflect.NewHandlerV1(reflector))
mux.Handle(grpcreflect.NewHandlerV1Alpha(reflector))

Expand Down
8 changes: 4 additions & 4 deletions src/internal/handlers/grpc/user/v1/init.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package handlers_grpc_user_v1

import (
"github.com/Golerplate/contracts/generated/services/user/store/v1/storev1connect"
"github.com/Golerplate/user-store-svc/internal/service"
"github.com/Golerplate/contracts/generated/services/user/store/svc/v1/svcv1connect"
service_v1 "github.com/Golerplate/user-store-svc/internal/service/v1"
)

type handler struct {
userStoreService service.UserStoreService
userStoreService service_v1.UserStoreService
}

func NewUserStoreServiceHandler(userStoreService service.UserStoreService) storev1connect.UserStoreSvcHandler {
func NewUserStoreServiceHandler(userStoreService service_v1.UserStoreService) svcv1connect.UserStoreSvcHandler {
return &handler{
userStoreService: userStoreService,
}
Expand Down
72 changes: 70 additions & 2 deletions src/internal/handlers/grpc/user/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"

userv1 "github.com/Golerplate/contracts/generated/services/user/store/v1"
userv1 "github.com/Golerplate/contracts/generated/services/user/store/svc/v1"
"github.com/Golerplate/pkg/grpc"
entities_user_v1 "github.com/Golerplate/user-store-svc/internal/entities/user/v1"
connectgo "github.com/bufbuild/connect-go"
Expand All @@ -13,7 +13,6 @@ import (
)

func (h *handler) CreateUser(ctx context.Context, c *connectgo.Request[userv1.CreateUserRequest]) (*connectgo.Response[userv1.CreateUserResponse], error) {

if c.Msg.GetUsername() == nil || c.Msg.GetUsername().GetValue() == "" {
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid username"))
}
Expand Down Expand Up @@ -45,3 +44,72 @@ func (h *handler) CreateUser(ctx context.Context, c *connectgo.Request[userv1.Cr
},
}), nil
}

func (h *handler) GetUserByEmail(ctx context.Context, c *connectgo.Request[userv1.GetUserByEmailRequest]) (*connectgo.Response[userv1.GetUserByEmailResponse], error) {
if c.Msg.GetEmail() == nil || c.Msg.GetEmail().GetValue() == "" {
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid email"))
}

user, err := h.userStoreService.GetUserByEmail(ctx, c.Msg.GetEmail().GetValue())
if err != nil {
return nil, grpc.TranslateToGRPCError(ctx, err)
}

return connectgo.NewResponse(&userv1.GetUserByEmailResponse{
User: &userv1.User{
Id: &wrappers.StringValue{Value: user.ID},
Username: &wrappers.StringValue{Value: user.Username},
Email: &wrappers.StringValue{Value: user.Email},
IsAdmin: &wrappers.BoolValue{Value: user.IsAdmin},
IsBanned: &wrappers.BoolValue{Value: user.IsBanned},
CreatedAt: &timestamppb.Timestamp{Seconds: int64(user.CreatedAt.Second()), Nanos: int32(user.CreatedAt.Nanosecond())},
UpdatedAt: &timestamppb.Timestamp{Seconds: int64(user.UpdatedAt.Second()), Nanos: int32(user.UpdatedAt.Nanosecond())},
},
}), nil
}

func (h *handler) GetUserByID(ctx context.Context, c *connectgo.Request[userv1.GetUserByIDRequest]) (*connectgo.Response[userv1.GetUserByIDResponse], error) {
if c.Msg.GetId() == nil || c.Msg.GetId().GetValue() == "" {
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid id"))
}

user, err := h.userStoreService.GetUserByID(ctx, c.Msg.GetId().GetValue())
if err != nil {
return nil, grpc.TranslateToGRPCError(ctx, err)
}

return connectgo.NewResponse(&userv1.GetUserByIDResponse{
User: &userv1.User{
Id: &wrappers.StringValue{Value: user.ID},
Username: &wrappers.StringValue{Value: user.Username},
Email: &wrappers.StringValue{Value: user.Email},
IsAdmin: &wrappers.BoolValue{Value: user.IsAdmin},
IsBanned: &wrappers.BoolValue{Value: user.IsBanned},
CreatedAt: &timestamppb.Timestamp{Seconds: int64(user.CreatedAt.Second()), Nanos: int32(user.CreatedAt.Nanosecond())},
UpdatedAt: &timestamppb.Timestamp{Seconds: int64(user.UpdatedAt.Second()), Nanos: int32(user.UpdatedAt.Nanosecond())},
},
}), nil
}

func (h *handler) GetUserByUsername(ctx context.Context, c *connectgo.Request[userv1.GetUserByUsernameRequest]) (*connectgo.Response[userv1.GetUserByUsernameResponse], error) {
if c.Msg.GetUsername() == nil || c.Msg.GetUsername().GetValue() == "" {
return nil, connectgo.NewError(connectgo.CodeInvalidArgument, errors.New("invalid username"))
}

user, err := h.userStoreService.GetUserByUsername(ctx, c.Msg.GetUsername().GetValue())
if err != nil {
return nil, grpc.TranslateToGRPCError(ctx, err)
}

return connectgo.NewResponse(&userv1.GetUserByUsernameResponse{
User: &userv1.User{
Id: &wrappers.StringValue{Value: user.ID},
Username: &wrappers.StringValue{Value: user.Username},
Email: &wrappers.StringValue{Value: user.Email},
IsAdmin: &wrappers.BoolValue{Value: user.IsAdmin},
IsBanned: &wrappers.BoolValue{Value: user.IsBanned},
CreatedAt: &timestamppb.Timestamp{Seconds: int64(user.CreatedAt.Second()), Nanos: int32(user.CreatedAt.Nanosecond())},
UpdatedAt: &timestamppb.Timestamp{Seconds: int64(user.UpdatedAt.Second()), Nanos: int32(user.UpdatedAt.Nanosecond())},
},
}), nil
}
59 changes: 59 additions & 0 deletions src/internal/handlers/grpc/user/v1/user_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
package handlers_grpc_user_v1

import (
"context"
"testing"
"time"

user_store_svc_v1_entities "github.com/Golerplate/contracts/clients/user-store-svc/v1/entities"
user_store_svc_v1_mocks "github.com/Golerplate/contracts/clients/user-store-svc/v1/mocks"
"github.com/Golerplate/pkg/constants"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

entities_user_v1 "github.com/Golerplate/user-store-svc/internal/entities/user/v1"
service_v1 "github.com/Golerplate/user-store-svc/internal/service/v1"
)

func Test_CreateUser(t *testing.T) {
t.Run("ok - username", func(t *testing.T) {
ctrl := gomock.NewController(t)
m := user_store_svc_v1_mocks.NewMockUserStoreSvc(ctrl)

userid := constants.GenerateDataPrefixWithULID(constants.User)
created := time.Now()

m.EXPECT().CreateUser(gomock.Any(), &entities_user_v1.CreateUserRequest{
Username: "testuser",
Email: "[email protected]",
Password: "123",
}).Return(&user_store_svc_v1_entities.User{
ID: userid,
Username: "testuser",
Email: "[email protected]",
IsAdmin: false,
IsBanned: false,
CreatedAt: created,
UpdatedAt: created,
}, nil)

s, err := service_v1.NewUserStoreService(context.Background(), m)
assert.NotNil(t, s)
assert.NoError(t, err)

user, err := s.CreateUser(context.Background(), &entities_user_v1.CreateUserRequest{
Username: "testuser",
Email: "[email protected]",
Password: "123",
})
assert.NotNil(t, user)
assert.NoError(t, err)

assert.EqualValues(t, &entities_user_v1.User{
ID: userid,
Username: "testuser",
Email: "[email protected]",
CreatedAt: created,
UpdatedAt: created,
}, user)
})
}
13 changes: 0 additions & 13 deletions src/internal/service/init.go

This file was deleted.

17 changes: 17 additions & 0 deletions src/internal/service/v1/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package service_v1

import (
"context"

"github.com/Golerplate/user-store-svc/internal/datastore"
)

type service struct {
store datastore.UserStoreServiceDatastore
}

func NewUserStoreService(ctx context.Context, store datastore.UserStoreServiceDatastore) (*service, error) {
return &service{
store: store,
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package service_v1

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package service_v1

import (
"context"
Expand Down

0 comments on commit a9dcb09

Please sign in to comment.