Skip to content

Commit

Permalink
Merge pull request #14 from dung13890/generate_project
Browse files Browse the repository at this point in the history
Change registry
  • Loading branch information
dung13890 authored Jun 8, 2023
2 parents 608d8ff + b585460 commit 27d98db
Show file tree
Hide file tree
Showing 25 changed files with 119 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ USAGE:
go-base-gen [global options] command [command options] [arguments...]

VERSION:
v1.0.9
v1.0.10

COMMANDS:
project Generate base code for go project use clean architecture
Expand Down
6 changes: 3 additions & 3 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var (
"pkg/cache",
"pkg/errors",
"pkg/logger",
"pkg/postgres",
"pkg/database",
"pkg/redis",
"pkg/utils",
"pkg/validate",
Expand Down Expand Up @@ -96,8 +96,8 @@ var (
"pkg/errors/code.go.tmpl",
"pkg/errors/errors.go.tmpl",
"pkg/logger/logger.go.tmpl",
"pkg/postgres/migrate.go.tmpl",
"pkg/postgres/postgres.go.tmpl",
"pkg/database/migrate.go.tmpl",
"pkg/database/postgres.go.tmpl",
"pkg/redis/redis.go.tmpl",
"pkg/utils/bcrypt.go.tmpl",
"pkg/utils/md5.go.tmpl",
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
version string = "v1.0.9"
version string = "v1.0.10"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions template/tmpl/cmd/migrate/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"time"

"{{.Project}}/config"
"{{.Project}}/pkg/database"
"{{.Project}}/pkg/logger"
"{{.Project}}/pkg/postgres"
)

func main() {
Expand All @@ -30,7 +30,7 @@ func main() {

db := config.GetDBConfig()

if err := postgres.Migrate(db); err != nil {
if err := database.Migrate(db); err != nil {
logger.Error().Fatal(err)
}
}
Expand Down
1 change: 0 additions & 1 deletion template/tmpl/config/redis.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ func GetRedisConfig() Redis {

return redisConf
}

{{end}}
4 changes: 2 additions & 2 deletions template/tmpl/internal/app/app.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"{{.Project}}/internal/constants"
"{{.Project}}/internal/registry"
"{{.Project}}/pkg/cache"
"{{.Project}}/pkg/database"
"{{.Project}}/pkg/errors"
"{{.Project}}/pkg/logger"
"{{.Project}}/pkg/postgres"
"{{.Project}}/pkg/redis"

"github.com/labstack/echo/v4"
Expand All @@ -31,7 +31,7 @@ func Run(conf config.AppConfig) error {
var db *gorm.DB
var err error
for {
db, err = postgres.NewGormDB(dbConf)
db, err = database.NewGormDB(dbConf)
if err != nil {
logger.Info().Printf("Wait for starting db: %v", err)
time.Sleep(constants.ConnectWaitDuration)
Expand Down
12 changes: 8 additions & 4 deletions template/tmpl/internal/app/seed.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"{{.Project}}/config"
"{{.Project}}/internal/domain"
authModule "{{.Project}}/internal/modules/auth/repository"
"{{.Project}}/pkg/database"
"{{.Project}}/pkg/errors"
"{{.Project}}/pkg/postgres"

"github.com/spf13/viper"
)
Expand All @@ -26,12 +26,16 @@ type seedData struct {

// Seed is function that seed data
func Seed(dbConfig config.Database) error {
db, err := postgres.NewGormDB(dbConfig)
db, err := database.NewGormDB(dbConfig)
if err != nil {
return errors.ErrInternalServerError.Wrap(err)
}

authModule := authModule.NewRepository(db)
authMD := &authModule.Repository{
RoleR: authModule.NewRoleRepository(db),
UserR: authModule.NewUserRepository(db),
PasswordR: authModule.NewPasswordResetRepository(db),
}

viper.SetConfigFile(pathJSON)
if err = viper.ReadInConfig(); err != nil {
Expand All @@ -43,7 +47,7 @@ func Seed(dbConfig config.Database) error {
return errors.ErrInternalServerError.Wrap(err)
}

if err := data.seedAuth(authModule); err != nil {
if err := data.seedAuth(authMD); err != nil {
return errors.Throw(err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type PasswordResetRepository struct {
*gorm.DB
}

// NewPasswordResetRepository will implement of domain.PasswordResetRepository interface
func NewPasswordResetRepository(db *gorm.DB) *PasswordResetRepository {
return &PasswordResetRepository{
DB: db,
}
}

// StoreOrUpdate will store or update password reset by email
func (rp *PasswordResetRepository) StoreOrUpdate(ctx context.Context, email, token string) error {
dao := &PasswordReset{
Expand Down
11 changes: 0 additions & 11 deletions template/tmpl/internal/modules/auth/repository/repository.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Mit License (MIT)

import (
"{{.Project}}/internal/domain"

"gorm.io/gorm"
)

// Repository for module auth
Expand All @@ -17,13 +15,4 @@ type Repository struct {
UserR domain.UserRepository
PasswordR domain.PasswordResetRepository
}

// NewRepository will create new postgres object
func NewRepository(db *gorm.DB) *Repository {
return &Repository{
RoleR: &RoleRepository{DB: db},
UserR: &UserRepository{DB: db},
PasswordR: &PasswordResetRepository{DB: db},
}
}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type RoleRepository struct {
*gorm.DB
}

// NewRoleRepository will implement of domain.RoleRepository interface
func NewRoleRepository(db *gorm.DB) *RoleRepository {
return &RoleRepository{
DB: db,
}
}

// Fetch will fetch content from db
func (rp *RoleRepository) Fetch(ctx context.Context) ([]domain.Role, error) {
dao := []Role{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type UserRepository struct {
*gorm.DB
}

// NewUserRepository will implement of domain.UserRepository interface
func NewUserRepository(db *gorm.DB) *UserRepository {
return &UserRepository{
DB: db,
}
}

// Fetch will fetch content from db
func (rp *UserRepository) Fetch(ctx context.Context) ([]domain.User, error) {
dao := []User{}
Expand Down
14 changes: 0 additions & 14 deletions template/tmpl/internal/modules/auth/usecase/usecase.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Mit License (MIT)

import (
"{{.Project}}/internal/domain"
"{{.Project}}/internal/modules/auth/repository"
)

// Usecase for module auth
Expand All @@ -16,17 +15,4 @@ type Usecase struct {
UserUC domain.UserUsecase
AuthUC domain.AuthUsecase
}

// NewUsecase implements from interface
func NewUsecase(
repo *repository.Repository,
jwtSvc domain.JWTService,
thrSvc domain.ThrottleService,
) *Usecase {
return &Usecase{
RoleUC: NewRoleUsecase(repo.RoleR),
UserUC: NewUserUsecase(repo.UserR),
AuthUC: NewAuthUsecase(jwtSvc, thrSvc, repo.UserR, repo.PasswordR),
}
}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type {{.Domain | capitalize}}Repository struct {
*gorm.DB
}

// New{{.Domain | capitalize}}Repository will implement of domain.{{.Domain | capitalize}}Repository
func New{{.Domain | capitalize}}Repository(db *gorm.DB) *{{.Domain | capitalize}}Repository {
return &{{.Domain | capitalize}}Repository{
DB: db,
}
}

// Fetch will fetch content from db
func (rp *{{.Domain | capitalize}}Repository) Fetch(ctx context.Context) ([]domain.{{.Domain | capitalize}}, error) {
dao := []{{.Domain | capitalize}}{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ Mit License (MIT)

import (
"{{.Project}}/internal/domain"

"gorm.io/gorm"
)

// Repository for module auth
type Repository struct {
{{.Domain | capitalize}}R domain.{{.Domain | capitalize}}Repository
}

// NewRepository will create new postgres object
func NewRepository(db *gorm.DB) *Repository {
return &Repository{
{{.Domain | capitalize}}R: &{{.Domain | capitalize}}Repository{DB: db},
}
}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type {{.Domain | capitalize}}Usecase struct {
repo domain.{{.Domain | capitalize}}Repository
}

// New{{.Domain | capitalize}}Usecase will create new an {{.Domain}}Usecase object representation of domain.{{.Domain | capitalize}}Usecase interface
// New{{.Domain | capitalize}}Usecase will implement of domain.{{.Domain | capitalize}}Usecase interface
func New{{.Domain | capitalize}}Usecase(repo domain.{{.Domain | capitalize}}Repository) *{{.Domain | capitalize}}Usecase {
return &{{.Domain | capitalize}}Usecase{
repo: repo,
Expand Down
10 changes: 0 additions & 10 deletions template/tmpl/internal/modules/name/usecase/usecase.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ Mit License (MIT)

import (
"{{.Project}}/internal/domain"
"{{.Project}}/internal/modules/{{.Module}}/repository"
)

// Usecase for module auth
type Usecase struct {
{{.Domain | capitalize}}UC domain.{{.Domain | capitalize}}Usecase
}

// NewUsecase implements from interface
func NewUsecase(
repo *repository.Repository,
) *Usecase {
return &Usecase{
{{.Domain | capitalize}}UC: New{{.Domain | capitalize}}Usecase(repo.{{.Domain | capitalize}}R),
}
}
{{end}}
6 changes: 5 additions & 1 deletion template/tmpl/internal/registry/repository.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ type Repository struct {
// NewRepository implements from interface for modules
func NewRepository(db *gorm.DB) *Repository {
return &Repository{
AuthModule: authRepo.NewRepository(db),
AuthModule: &authRepo.Repository{
RoleR: authRepo.NewRoleRepository(db),
UserR: authRepo.NewUserRepository(db),
PasswordR: authRepo.NewPasswordResetRepository(db),
},
}
}
{{end}}
19 changes: 14 additions & 5 deletions template/tmpl/internal/registry/usecase.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ type Usecase struct {
// NewUsecase implements from interface for modules
func NewUsecase(repo *Repository, svc *Service) *Usecase {
return &Usecase{
AuthModule: authUC.NewUsecase(
repo.AuthModule,
svc.JWTSvc,
svc.ThrottleSvc,
),
AuthModule: &authUC.Usecase{
RoleUC: authUC.NewRoleUsecase(
repo.AuthModule.RoleR,
),
UserUC: authUC.NewUserUsecase(
repo.AuthModule.UserR,
),
AuthUC: authUC.NewAuthUsecase(
svc.JWTSvc,
svc.ThrottleSvc,
repo.AuthModule.UserR,
repo.AuthModule.PasswordR,
),
},
}
}
{{end}}
7 changes: 7 additions & 0 deletions template/tmpl/internal/repository/domain_repo.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ type {{.Domain | capitalize}}Repository struct {
*gorm.DB
}

// New{{.Domain | capitalize}}Repository will implement of domain.{{.Domain | capitalize}}Repository interface
func New{{.Domain | capitalize}}Repository(db *gorm.DB) *{{.Domain | capitalize}}Repository {
return &{{.Domain | capitalize}}Repository{
DB: db,
}
}

// Fetch will fetch content from db
func (rp *{{.Domain | capitalize}}Repository) Fetch(ctx context.Context) ([]domain.{{.Domain | capitalize}}, error) {
dao := []{{.Domain | capitalize}}{}
Expand Down
7 changes: 0 additions & 7 deletions template/tmpl/internal/repository/repository.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,4 @@ import (
type Repository struct {
{{.Domain | capitalize}}R domain.{{.Domain | capitalize}}Repository
}

// NewRepository will create new postgres object
func NewRepository() *Repository {
return &Repository{
{{.Domain | capitalize}}R: &{{.Domain | capitalize}}Repository{},
}
}
{{end}}
2 changes: 1 addition & 1 deletion template/tmpl/internal/usecase/domain_uc.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type {{.Domain | capitalize}}Usecase struct {
repo domain.{{.Domain | capitalize}}Repository
}

// New{{.Domain | capitalize}}Usecase will create new an {{.Domain}}Usecase object representation of domain.{{.Domain | capitalize}}Usecase interface
// New{{.Domain | capitalize}}Usecase will implement of domain.{{.Domain | capitalize}}Usecase interface
func New{{.Domain | capitalize}}Usecase(repo domain.{{.Domain | capitalize}}Repository) *{{.Domain | capitalize}}Usecase {
return &{{.Domain | capitalize}}Usecase{
repo: repo,
Expand Down
10 changes: 0 additions & 10 deletions template/tmpl/internal/usecase/usecase.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ Mit License (MIT)

import (
"{{.Project}}/internal/domain"
"{{.Project}}/internal/repository"
)

// Usecase for all usecase
type Usecase struct {
{{.Domain | capitalize}}UC domain.{{.Domain | capitalize}}Usecase
}

// NewUsecase implements from interface
func NewUsecase(
repo *repository.Repository,
) *Usecase {
return &Usecase{
{{.Domain | capitalize}}UC: New{{.Domain | capitalize}}Usecase(repo.{{.Domain | capitalize}}R),
}
}
{{end}}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "pkg/postgres/migrate.go"}}package postgres
{{define "pkg/database/migrate.go"}}package database

import (
"flag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Copyright (c) 2023 dung13890. All rights reserved.
Mit License (MIT)
*/}}

{{define "pkg/postgres/postgres.go"}}package postgres
{{define "pkg/database/postgres.go"}}package database

import (
"fmt"
Expand Down
Loading

0 comments on commit 27d98db

Please sign in to comment.