Skip to content

Commit

Permalink
Merge pull request #11 from isd-sgcu/refactor/project-structure
Browse files Browse the repository at this point in the history
[Refactor] Project structure
  • Loading branch information
Nitiwat-owen authored Jan 1, 2024
2 parents 606d179 + fbf16df commit 16fbfbf
Show file tree
Hide file tree
Showing 56 changed files with 174 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Test
run: |
go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/...
go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/...
go tool cover -func="./coverage.out"
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
- name: Test
run: |
go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/...
go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/...
go tool cover -func="./coverage.out"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ coverage.html

.DS_store

src/internal/repository/user/memory:
internal/repository/user/memory:
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ COPY go.mod go.sum ./
RUN go mod download

# Copy the source code
COPY ./src ./src
COPY . .

# Build the application
RUN go build -o server ./src/.
RUN go build -o server ./cmd/main.go

# Copy config files
COPY ./config ./config
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
server:
go run ./src/.
go run ./cmd/.

mock-gen:
mockgen -source ./src/pkg/repository/cache/cache.repository.go -destination ./src/mocks/repository/cache/cache.mock.go
mockgen -source ./src/pkg/repository/auth/auth.repository.go -destination ./src/mocks/repository/auth/auth.mock.go
mockgen -source ./pkg/repository/cache/cache.repository.go -destination ./mocks/repository/cache/cache.mock.go
mockgen -source ./pkg/repository/auth/auth.repository.go -destination ./mocks/repository/auth/auth.mock.go

test:
go vet ./...
go test -v -coverpkg ./src/internal/... -coverprofile coverage.out -covermode count ./src/internal/...
go test -v -coverpkg ./internal/... -coverprofile coverage.out -covermode count ./internal/...
go tool cover -func=coverage.out
go tool cover -html=coverage.out -o coverage.html

Expand Down
2 changes: 1 addition & 1 deletion src/config/config.go → cfgldr/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package config
package cfgldr

import (
"github.com/pkg/errors"
Expand Down
24 changes: 12 additions & 12 deletions src/main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ package main
import (
"context"
"fmt"
"github.com/isd-sgcu/johnjud-auth/cfgldr"
"github.com/isd-sgcu/johnjud-auth/database"
authRp "github.com/isd-sgcu/johnjud-auth/internal/repository/auth"
cacheRp "github.com/isd-sgcu/johnjud-auth/internal/repository/cache"
userRp "github.com/isd-sgcu/johnjud-auth/internal/repository/user"
authSvc "github.com/isd-sgcu/johnjud-auth/internal/service/auth"
jwtSvc "github.com/isd-sgcu/johnjud-auth/internal/service/jwt"
tokenSvc "github.com/isd-sgcu/johnjud-auth/internal/service/token"
userSvc "github.com/isd-sgcu/johnjud-auth/internal/service/user"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/isd-sgcu/johnjud-auth/src/config"
"github.com/isd-sgcu/johnjud-auth/src/database"
"github.com/isd-sgcu/johnjud-auth/src/internal/strategy"
"github.com/isd-sgcu/johnjud-auth/src/internal/utils"
authRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/auth"
cacheRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/cache"
userRp "github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user"
authSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/auth"
jwtSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/jwt"
tokenSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/token"
userSvc "github.com/isd-sgcu/johnjud-auth/src/pkg/service/user"
"github.com/isd-sgcu/johnjud-auth/internal/strategy"
"github.com/isd-sgcu/johnjud-auth/internal/utils"
authPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1"
userPb "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -87,7 +87,7 @@ func gracefulShutdown(ctx context.Context, timeout time.Duration, ops map[string
}

func main() {
conf, err := config.LoadConfig()
conf, err := cfgldr.LoadConfig()
if err != nil {
log.Fatal().
Err(err).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package database

import (
"fmt"
"github.com/isd-sgcu/johnjud-auth/src/config"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/cfgldr"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
"strconv"
)

func InitPostgresDatabase(conf *config.Database, isDebug bool) (db *gorm.DB, err error) {
func InitPostgresDatabase(conf *cfgldr.Database, isDebug bool) (db *gorm.DB, err error) {
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", conf.Host, strconv.Itoa(conf.Port), conf.Username, conf.Password, conf.Name, conf.SSL)

gormConf := &gorm.Config{TranslateError: true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package database

import (
"fmt"
"github.com/isd-sgcu/johnjud-auth/src/config"
"github.com/isd-sgcu/johnjud-auth/cfgldr"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
)

func InitRedisConnection(conf *config.Redis) (*redis.Client, error) {
func InitRedisConnection(conf *cfgldr.Redis) (*redis.Client, error) {
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)

cache := redis.NewClient(&redis.Options{
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package token

import (
"github.com/golang-jwt/jwt/v4"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
)

type UserCredential struct {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import "github.com/isd-sgcu/johnjud-auth/src/internal/constant"
import (
"github.com/isd-sgcu/johnjud-auth/internal/constant"
)

type User struct {
Base
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package auth

import (
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/auth"
"gorm.io/gorm"
)

type repositoryImpl struct {
Db *gorm.DB
}

func NewRepository(db *gorm.DB) *repositoryImpl {
func NewRepository(db *gorm.DB) auth.Repository {
return &repositoryImpl{Db: db}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package auth
import (
"fmt"
"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/driver/postgres"
Expand All @@ -14,7 +15,7 @@ import (
type AuthRepositoryTest struct {
suite.Suite
db *gorm.DB
authRepo *repositoryImpl
authRepo auth.Repository
}

func TestAuthRepository(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"context"
"encoding/json"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/cache"
"github.com/redis/go-redis/v9"
"time"
)
Expand All @@ -11,7 +12,7 @@ type repositoryImpl struct {
client *redis.Client
}

func NewRepository(client *redis.Client) *repositoryImpl {
func NewRepository(client *redis.Client) cache.Repository {
return &repositoryImpl{client: client}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"context"
"fmt"
"github.com/go-faker/faker/v4"
tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token"
tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/cache"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
Expand All @@ -15,7 +16,7 @@ import (
type CacheRepositoryTest struct {
suite.Suite
cacheDb *redis.Client
cacheRepo *repositoryImpl
cacheRepo cache.Repository
key string
value *tokenDto.AccessTokenCache
}
Expand All @@ -41,7 +42,7 @@ func (t *CacheRepositoryTest) SetupTest() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

err := cacheRepo.client.FlushDB(ctx).Err()
err := cacheDb.FlushDB(ctx).Err()
assert.Nil(t.T(), err)

err = cacheRepo.SetValue(key, value, 60)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package user

import (
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/user"
"gorm.io/gorm"
)

type repositoryImpl struct {
Db *gorm.DB
}

func NewRepository(db *gorm.DB) *repositoryImpl {
func NewRepository(db *gorm.DB) user.Repository {
return &repositoryImpl{Db: db}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package user
import (
"fmt"
"github.com/go-faker/faker/v4"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gorm.io/driver/postgres"
Expand All @@ -15,7 +16,7 @@ import (
type UserRepositoryTest struct {
suite.Suite
db *gorm.DB
userRepo *repositoryImpl
userRepo user.Repository
initialUser *model.User
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package auth

import (
"context"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/internal/utils"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/auth"
"github.com/isd-sgcu/johnjud-auth/pkg/repository/user"
"github.com/isd-sgcu/johnjud-auth/pkg/service/token"

"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/src/internal/utils"
"github.com/isd-sgcu/johnjud-auth/src/pkg/repository/auth"
"github.com/isd-sgcu/johnjud-auth/src/pkg/repository/user"
"github.com/isd-sgcu/johnjud-auth/src/pkg/service/token"
authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
Expand All @@ -25,7 +25,7 @@ type serviceImpl struct {
bcryptUtil utils.IBcryptUtil
}

func NewService(authRepo auth.Repository, userRepo user.Repository, tokenService token.Service, bcryptUtil utils.IBcryptUtil) *serviceImpl {
func NewService(authRepo auth.Repository, userRepo user.Repository, tokenService token.Service, bcryptUtil utils.IBcryptUtil) authProto.AuthServiceServer {
return &serviceImpl{
authRepo: authRepo,
userRepo: userRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package auth

import (
"context"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/internal/domain/model"
"github.com/isd-sgcu/johnjud-auth/mocks/repository/auth"
"github.com/isd-sgcu/johnjud-auth/mocks/repository/user"
"github.com/isd-sgcu/johnjud-auth/mocks/service/token"
"github.com/isd-sgcu/johnjud-auth/mocks/utils"
"testing"
"time"

"github.com/go-faker/faker/v4"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
mock_auth "github.com/isd-sgcu/johnjud-auth/src/mocks/repository/auth"
"github.com/isd-sgcu/johnjud-auth/src/mocks/repository/user"
"github.com/isd-sgcu/johnjud-auth/src/mocks/service/token"
"github.com/isd-sgcu/johnjud-auth/src/mocks/utils"
authProto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/auth/v1"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package jwt
import (
"fmt"
_jwt "github.com/golang-jwt/jwt/v4"
"github.com/isd-sgcu/johnjud-auth/src/config"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/src/internal/utils"
"github.com/isd-sgcu/johnjud-auth/src/pkg/strategy"
"github.com/isd-sgcu/johnjud-auth/cfgldr"
"github.com/isd-sgcu/johnjud-auth/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/internal/utils"
"github.com/isd-sgcu/johnjud-auth/pkg/service/jwt"
"github.com/isd-sgcu/johnjud-auth/pkg/strategy"
"github.com/pkg/errors"
"time"
)

type serviceImpl struct {
config config.Jwt
config cfgldr.Jwt
strategy strategy.JwtStrategy
jwtUtil utils.IJwtUtil
}

func NewService(config config.Jwt, strategy strategy.JwtStrategy, jwtUtil utils.IJwtUtil) *serviceImpl {
func NewService(config cfgldr.Jwt, strategy strategy.JwtStrategy, jwtUtil utils.IJwtUtil) jwt.Service {
return &serviceImpl{config: config, strategy: strategy, jwtUtil: jwtUtil}
}

Expand Down Expand Up @@ -47,6 +48,6 @@ func (s *serviceImpl) VerifyAuth(token string) (*_jwt.Token, error) {
return s.jwtUtil.ParseToken(token, s.strategy.AuthDecode)
}

func (s *serviceImpl) GetConfig() *config.Jwt {
func (s *serviceImpl) GetConfig() *cfgldr.Jwt {
return &s.config
}
Loading

0 comments on commit 16fbfbf

Please sign in to comment.