Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
refactor: use centralized proto
Browse files Browse the repository at this point in the history
  • Loading branch information
ImSoZRious committed Jun 30, 2023
1 parent 0ac4891 commit d9afba8
Show file tree
Hide file tree
Showing 62 changed files with 205 additions and 8,236 deletions.
148 changes: 67 additions & 81 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,11 @@ import (

"github.com/isd-sgcu/rpkm66-gateway/cfgldr"
_ "github.com/isd-sgcu/rpkm66-gateway/docs"
authHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/auth"
baanHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/baan"
ciHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/checkin"
eHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/estamp"
fileHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/file"
grpHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/group"
health_check "github.com/isd-sgcu/rpkm66-gateway/internal/handler/health-check"
usrHdr "github.com/isd-sgcu/rpkm66-gateway/internal/handler/user"
guard "github.com/isd-sgcu/rpkm66-gateway/internal/middleware/auth"
"github.com/isd-sgcu/rpkm66-gateway/internal/router"
authSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/auth"
baanSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/baan"
ciSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/checkin"
eSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/estamp"
fileSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/file"
grpSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/group"
usrSrv "github.com/isd-sgcu/rpkm66-gateway/internal/service/user"
"github.com/isd-sgcu/rpkm66-gateway/internal/validator"
"github.com/isd-sgcu/rpkm66-gateway/proto"
proto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/auth/auth/v1"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -85,21 +71,21 @@ func main() {
Msg("Failed to start service")
}

v, err := validator.NewValidator()
if err != nil {
log.Fatal().
Err(err).
Str("service", "validator").
Msg("Failed to start service")
}

backendConn, err := grpc.Dial(conf.Service.Backend, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal().
Err(err).
Str("service", "rnkm-backend").
Msg("Cannot connect to service")
}
// v, err := validator.NewValidator()
// if err != nil {
// log.Fatal().
// Err(err).
// Str("service", "validator").
// Msg("Failed to start service")
// }

// backendConn, err := grpc.Dial(conf.Service.Backend, grpc.WithTransportCredentials(insecure.NewCredentials()))
// if err != nil {
// log.Fatal().
// Err(err).
// Str("service", "rnkm-backend").
// Msg("Cannot connect to service")
// }

authConn, err := grpc.Dial(conf.Service.Auth, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand All @@ -109,74 +95,74 @@ func main() {
Msg("Cannot connect to service")
}

fileConn, err := grpc.Dial(conf.Service.File, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal().
Err(err).
Str("service", "rnkm-file").
Msg("Cannot connect to service")
}
// fileConn, err := grpc.Dial(conf.Service.File, grpc.WithTransportCredentials(insecure.NewCredentials()))
// if err != nil {
// log.Fatal().
// Err(err).
// Str("service", "rnkm-file").
// Msg("Cannot connect to service")
// }

hc := health_check.NewHandler()

usrClient := proto.NewUserServiceClient(backendConn)
userSrv := usrSrv.NewService(usrClient)
userHdr := usrHdr.NewHandler(userSrv, v)
// usrClient := proto.NewUserServiceClient(backendConn)
// userSrv := usrSrv.NewService(usrClient)
// userHdr := usrHdr.NewHandler(userSrv, v)

authClient := proto.NewAuthServiceClient(authConn)
athSrv := authSrv.NewService(authClient)
athHdr := authHdr.NewHandler(athSrv, userSrv, v)
// athHdr := authHdr.NewHandler(athSrv, userSrv, v)

fileClient := proto.NewFileServiceClient(fileConn)
fleSrv := fileSrv.NewService(fileClient)
fleHdr := fileHdr.NewHandler(fleSrv, userSrv, conf.App.MaxFileSize)
// fileClient := proto.NewFileServiceClient(fileConn)
// fleSrv := fileSrv.NewService(fileClient)
// fleHdr := fileHdr.NewHandler(fleSrv, userSrv, conf.App.MaxFileSize)

gClient := proto.NewGroupServiceClient(backendConn)
gSrv := grpSrv.NewService(gClient)
gHdr := grpHdr.NewHandler(gSrv, v)
// gClient := proto.NewGroupServiceClient(backendConn)
// gSrv := grpSrv.NewService(gClient)
// gHdr := grpHdr.NewHandler(gSrv, v)

bnClient := proto.NewBaanServiceClient(backendConn)
bnSrv := baanSrv.NewService(bnClient)
bnHdr := baanHdr.NewHandler(bnSrv, userSrv)
// bnClient := proto.NewBaanServiceClient(backendConn)
// bnSrv := baanSrv.NewService(bnClient)
// bnHdr := baanHdr.NewHandler(bnSrv, userSrv)

checkinClient := proto.NewCheckinServiceClient(backendConn)
checkinSrv := ciSrv.NewService(checkinClient)
// checkinClient := proto.NewCheckinServiceClient(backendConn)
// checkinSrv := ciSrv.NewService(checkinClient)

estampClient := proto.NewEventServiceClient(backendConn)
estampSrv := eSrv.NewService(estampClient)
estampHdr := eHdr.NewHandler(estampSrv, v)
// estampClient := proto.NewEventServiceClient(backendConn)
// estampSrv := eSrv.NewService(estampClient)
// estampHdr := eHdr.NewHandler(estampSrv, v)

ciHandler := ciHdr.NewHandler(checkinSrv, v)
// ciHandler := ciHdr.NewHandler(checkinSrv, v)
authGuard := guard.NewAuthGuard(athSrv)

r := router.NewGinRouter(&authGuard, conf.App)

r.SetHandler("GET /", hc.HealthCheck)
r.SetHandler("PUT /user", userHdr.CreateOrUpdate)
r.SetHandler("PATCH /user", userHdr.Update)
r.SetHandler("GET /auth/me", athHdr.Validate)
r.SetHandler("POST /auth/verify", athHdr.VerifyTicket)
r.SetHandler("POST /auth/refreshToken", athHdr.RefreshToken)
r.SetHandler("PUT /file/upload", fleHdr.Upload)
r.SetHandler("GET /baan", bnHdr.FindAll)
r.SetHandler("GET /baan/:id", bnHdr.FindOne)
r.SetHandler("GET /group", gHdr.FindOne)
r.SetHandler("GET /group/:token", gHdr.FindByToken)
r.SetHandler("POST /group/:token", gHdr.Join)
r.SetHandler("DELETE /group/leave", gHdr.Leave)
r.SetHandler("PUT /group/select", gHdr.SelectBaan)
r.SetHandler("DELETE /members/:member_id", gHdr.DeleteMember)
r.SetHandler("POST /qr/checkin/verify", ciHandler.CheckinVerify)
r.SetHandler("POST /qr/checkin/confirm", ciHandler.CheckinConfirm)
r.SetHandler("POST /qr/estamp/verify", estampHdr.VerifyEstamp)
r.SetHandler("POST /qr/estamp/confirm", userHdr.ConfirmEstamp)
r.SetHandler("POST /qr/ticket", userHdr.VerifyTicket)
r.SetHandler("GET /estamp/:id", estampHdr.FindEventByID)
r.SetHandler("GET /estamp", estampHdr.FindAllEventWithType)
r.SetHandler("GET /estamp/user", userHdr.GetUserEstamp)
r.SetHandler("GET /user/:id", userHdr.FindOne)
r.SetHandler("POST /user", userHdr.Create)
r.SetHandler("DELETE /user/:id", userHdr.Delete)
// r.SetHandler("PUT /user", userHdr.CreateOrUpdate)
// r.SetHandler("PATCH /user", userHdr.Update)
// r.SetHandler("GET /auth/me", athHdr.Validate)
// r.SetHandler("POST /auth/verify", athHdr.VerifyTicket)
// r.SetHandler("POST /auth/refreshToken", athHdr.RefreshToken)
// r.SetHandler("PUT /file/upload", fleHdr.Upload)
// r.SetHandler("GET /baan", bnHdr.FindAll)
// r.SetHandler("GET /baan/:id", bnHdr.FindOne)
// r.SetHandler("GET /group", gHdr.FindOne)
// r.SetHandler("GET /group/:token", gHdr.FindByToken)
// r.SetHandler("POST /group/:token", gHdr.Join)
// r.SetHandler("DELETE /group/leave", gHdr.Leave)
// r.SetHandler("PUT /group/select", gHdr.SelectBaan)
// r.SetHandler("DELETE /members/:member_id", gHdr.DeleteMember)
// r.SetHandler("POST /qr/checkin/verify", ciHandler.CheckinVerify)
// r.SetHandler("POST /qr/checkin/confirm", ciHandler.CheckinConfirm)
// r.SetHandler("POST /qr/estamp/verify", estampHdr.VerifyEstamp)
// r.SetHandler("POST /qr/estamp/confirm", userHdr.ConfirmEstamp)
// r.SetHandler("POST /qr/ticket", userHdr.VerifyTicket)
// r.SetHandler("GET /estamp/:id", estampHdr.FindEventByID)
// r.SetHandler("GET /estamp", estampHdr.FindAllEventWithType)
// r.SetHandler("GET /estamp/user", userHdr.GetUserEstamp)
// r.SetHandler("GET /user/:id", userHdr.FindOne)
// r.SetHandler("POST /user", userHdr.Create)
// r.SetHandler("DELETE /user/:id", userHdr.Delete)

server := http.Server{
Addr: fmt.Sprintf(":%v", conf.App.Port),
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
github.com/google/uuid v1.3.0
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630055326-ebe9af180145
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.1
github.com/spf13/viper v1.16.0
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/bxcodec/faker/v3 v3.8.0 h1:F59Qqnsh0BOtZRC+c4cXoB/VNYDMS3R5mlSpxIap1oU=
github.com/bxcodec/faker/v3 v3.8.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down Expand Up @@ -179,6 +181,14 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230627203936-ae1895fe6157 h1:mLuuY992iH6JFHQa0+ApsY9r/K02ryQzIvFDU7+r/Ic=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230627203936-ae1895fe6157/go.mod h1:eG1p2DVZixBxFze36PEyKFIZEQpfc6ZvYn+dTN6g6jk=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630031453-328e11ffa092 h1:GtJspUXU0K8pDeWcMNuMiVkzD2QW6e2jhChOT5B+wyQ=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630031453-328e11ffa092/go.mod h1:qxIb+IJeGg8MHhRfjs69b7/8+ZUfwrMkXBwu0TtfI1Q=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630035718-638b93cad427 h1:3t+x45SrY/rdsd/Zxu6B4HLNlOMTZb80EVWSFsBOXOI=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630035718-638b93cad427/go.mod h1:qxIb+IJeGg8MHhRfjs69b7/8+ZUfwrMkXBwu0TtfI1Q=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630055326-ebe9af180145 h1:46wil4nkyWjk5DwJPrHwV+C/BnI4fR51fJ6co1ScFzQ=
github.com/isd-sgcu/rpkm66-go-proto v0.0.0-20230630055326-ebe9af180145/go.mod h1:qxIb+IJeGg8MHhRfjs69b7/8+ZUfwrMkXBwu0TtfI1Q=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/auth/auth.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
validate "github.com/isd-sgcu/rpkm66-gateway/internal/validator"
"github.com/isd-sgcu/rpkm66-gateway/pkg/rctx"
"github.com/isd-sgcu/rpkm66-gateway/pkg/service/auth"
"github.com/isd-sgcu/rpkm66-gateway/proto"
usrProto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/backend/user/v1"
)

type Handler struct {
Expand All @@ -17,7 +17,7 @@ type Handler struct {
}

type IUserService interface {
FindOne(string) (*proto.User, *dto.ResponseErr)
FindOne(string) (*usrProto.User, *dto.ResponseErr)
}

func NewHandler(service auth.Service, usrService IUserService, validate *validate.DtoValidator) *Handler {
Expand Down
7 changes: 4 additions & 3 deletions internal/handler/auth/auth.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
mock "github.com/isd-sgcu/rpkm66-gateway/mocks/auth"
"github.com/isd-sgcu/rpkm66-gateway/mocks/rctx"
"github.com/isd-sgcu/rpkm66-gateway/mocks/user"
"github.com/isd-sgcu/rpkm66-gateway/proto"
proto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/auth/auth/v1"
userProto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/backend/user/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type AuthHandlerTest struct {
suite.Suite
UserDto *proto.User
UserDto *userProto.User
Credential *proto.Credential
Payload *dto.TokenPayloadAuth
BadRequestErr *dto.ResponseErr
Expand All @@ -31,7 +32,7 @@ func TestAuthHandler(t *testing.T) {
}

func (t *AuthHandlerTest) SetupTest() {
t.UserDto = &proto.User{
t.UserDto = &userProto.User{
Id: faker.UUIDDigit(),
Firstname: faker.FirstName(),
Lastname: faker.LastName(),
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/baan/baan.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/isd-sgcu/rpkm66-gateway/internal/dto"
"github.com/isd-sgcu/rpkm66-gateway/pkg/rctx"
"github.com/isd-sgcu/rpkm66-gateway/pkg/service/baan"
"github.com/isd-sgcu/rpkm66-gateway/proto"
userProto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/backend/user/v1"
)

type Handler struct {
Expand All @@ -15,7 +15,7 @@ type Handler struct {
}

type IUserService interface {
FindOne(string) (*proto.User, *dto.ResponseErr)
FindOne(string) (*userProto.User, *dto.ResponseErr)
}

func NewHandler(service baan.Service, userService IUserService) *Handler {
Expand Down
21 changes: 11 additions & 10 deletions internal/handler/baan/baan.handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
mock "github.com/isd-sgcu/rpkm66-gateway/mocks/baan"
"github.com/isd-sgcu/rpkm66-gateway/mocks/rctx"
mockUsr "github.com/isd-sgcu/rpkm66-gateway/mocks/user"
"github.com/isd-sgcu/rpkm66-gateway/proto"
baanProto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/backend/baan/v1"
userProto "github.com/isd-sgcu/rpkm66-go-proto/rpkm66/backend/user/v1"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type BaanHandlerTest struct {
suite.Suite
Baan *proto.Baan
User *proto.User
Baan *baanProto.Baan
User *userProto.User
userId string
BindErr *dto.ResponseErr
NotFoundErr *dto.ResponseErr
Expand All @@ -30,13 +31,13 @@ func TestBaanHandler(t *testing.T) {
}

func (t *BaanHandlerTest) SetupTest() {
t.Baan = &proto.Baan{
t.Baan = &baanProto.Baan{
Id: faker.UUIDDigit(),
NameTH: faker.Word(),
DescriptionTH: faker.Paragraph(),
NameEN: faker.Word(),
DescriptionEN: faker.Paragraph(),
Size: proto.BaanSize_M,
Size: baanProto.BaanSize_M,
Facebook: faker.URL(),
FacebookUrl: faker.URL(),
Instagram: faker.URL(),
Expand All @@ -46,7 +47,7 @@ func (t *BaanHandlerTest) SetupTest() {
ImageUrl: faker.URL(),
}

t.User = &proto.User{
t.User = &userProto.User{
Id: faker.UUIDDigit(),
Title: faker.Word(),
Firstname: faker.FirstName(),
Expand Down Expand Up @@ -86,18 +87,18 @@ func (t *BaanHandlerTest) SetupTest() {
}
}

func createBaans(baan *proto.Baan) []*proto.Baan {
var baans []*proto.Baan
func createBaans(baan *baanProto.Baan) []*baanProto.Baan {
var baans []*baanProto.Baan
baans = append(baans, baan)

for i := 0; i < 2; i++ {
b := proto.Baan{
b := baanProto.Baan{
Id: faker.UUIDDigit(),
NameTH: faker.Word(),
DescriptionTH: faker.Paragraph(),
NameEN: faker.Word(),
DescriptionEN: faker.Paragraph(),
Size: proto.BaanSize_M,
Size: baanProto.BaanSize_M,
Facebook: faker.URL(),
FacebookUrl: faker.URL(),
Instagram: faker.URL(),
Expand Down
Loading

0 comments on commit d9afba8

Please sign in to comment.