├── docker ├── air │ └── .gitignore ├── postgresql │ └── .gitignore ├── Dockerfile └── nginx │ └── default.conf ├── config ├── logs │ └── query_log │ │ └── .gitignore ├── email.go ├── logger.go └── database.go ├── .gitignore ├── .github ├── issue_template.md ├── issue-branch.yml ├── workflows │ ├── issue-autolink.yml │ ├── create_branch.yml │ └── go.yml └── pull_request_template.md ├── .air.toml ├── database ├── seeder.go ├── entities │ ├── migration_entity.go │ ├── common.go │ ├── refresh_token_entity.go │ └── user_entity.go ├── migration.go ├── seeders │ ├── json │ │ └── users.json │ └── seeds │ │ └── user_seed.go ├── migrations │ ├── 20240101000000_create_users_table.go │ └── 20240101000001_create_refresh_tokens_table.go └── manager.go ├── pkg ├── constants │ └── common.go ├── helpers │ └── password.go └── utils │ ├── response.go │ ├── email.go │ ├── file.go │ ├── email-template │ └── base_mail.html │ └── aes.go ├── script ├── script.go ├── example_script.go └── command.go ├── .env.example ├── middlewares ├── cors.go └── authentication.go ├── modules ├── auth │ ├── routes.go │ ├── dto │ │ └── auth_dto.go │ ├── validation │ │ └── auth_validation.go │ ├── repository │ │ └── refresh_token_repository.go │ ├── tests │ │ └── auth_validation_test.go │ ├── service │ │ ├── jwt_service.go │ │ └── auth_service.go │ └── controller │ │ └── auth_controller.go └── user │ ├── routes.go │ ├── validation │ └── user_validation.go │ ├── query │ └── user_query.go │ ├── tests │ └── user_validation_test.go │ ├── service │ └── user_service.go │ ├── repository │ └── user_repository.go │ ├── controller │ └── user_controller.go │ └── dto │ └── user_dto.go ├── LICENSE ├── docker-compose.yml ├── cmd └── main.go ├── providers └── core.go ├── go.mod ├── Makefile ├── create_module.sh ├── logs.html ├── README.md └── go.sum /docker/air/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /docker/postgresql/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /config/logs/query_log/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.env 2 | storage/ 3 | assets/ 4 | volumes/ 5 | .idea/ -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | 3 | ## Screenshoot 4 | 5 | (Optional) -------------------------------------------------------------------------------- /.github/issue-branch.yml: -------------------------------------------------------------------------------- 1 | branchName: "is-${issue.number}-${issue.title}" 2 | commentMessage: "Branch ${branchName} created for issue: ${issue.title}" -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.23-alpine 2 | 3 | WORKDIR /app 4 | 5 | RUN go install github.com/air-verse/air@latest 6 | 7 | COPY . . 8 | 9 | RUN go mod tidy 10 | 11 | CMD ["air"] -------------------------------------------------------------------------------- /.air.toml: -------------------------------------------------------------------------------- 1 | root = "." 2 | tmp_dir = "docker/air/tmp" 3 | 4 | [build] 5 | bin = "docker/air/tmp/main" 6 | cmd = "go build -o docker/air/tmp/main cmd/main.go" 7 | include_ext = ["go"] 8 | exclude_dir = ["vendor", "tmp"] 9 | 10 | [log] 11 | level = "debug" 12 | -------------------------------------------------------------------------------- /database/seeder.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/database/seeders/seeds" 5 | "gorm.io/gorm" 6 | ) 7 | 8 | func Seeder(db *gorm.DB) error { 9 | if err := seeds.ListUserSeeder(db); err != nil { 10 | return err 11 | } 12 | 13 | return nil 14 | } 15 | -------------------------------------------------------------------------------- /pkg/constants/common.go: -------------------------------------------------------------------------------- 1 | package constants 2 | 3 | const ( 4 | ENUM_ROLE_ADMIN = "admin" 5 | ENUM_ROLE_USER = "user" 6 | 7 | ENUM_RUN_PRODUCTION = "production" 8 | ENUM_RUN_TESTING = "testing" 9 | 10 | ENUM_PAGINATION_PER_PAGE = 10 11 | ENUM_PAGINATION_PAGE = 1 12 | 13 | DB = "db" 14 | JWTService = "JWTService" 15 | ) 16 | -------------------------------------------------------------------------------- /script/script.go: -------------------------------------------------------------------------------- 1 | package script 2 | 3 | import ( 4 | "errors" 5 | 6 | "gorm.io/gorm" 7 | ) 8 | 9 | func Script(scriptName string, db *gorm.DB) error { 10 | switch scriptName { 11 | case "example_script": 12 | exampleScript := NewExampleScript(db) 13 | return exampleScript.Run() 14 | default: 15 | return errors.New("script not found") 16 | } 17 | } -------------------------------------------------------------------------------- /.github/workflows/issue-autolink.yml: -------------------------------------------------------------------------------- 1 | name: "Issue AutoLink" 2 | 3 | on: 4 | pull_request: 5 | types: [opened] 6 | 7 | jobs: 8 | issue-links: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: tkt-actions/add-issue-links@v1.6.0 12 | with: 13 | repo-token: ${{ secrets.GITHUB_TOKEN }} 14 | branch-prefix: "is-" 15 | resolve: true -------------------------------------------------------------------------------- /.github/workflows/create_branch.yml: -------------------------------------------------------------------------------- 1 | name: Create Branch from Issue 2 | 3 | on: 4 | issues: 5 | types: [assigned] 6 | 7 | jobs: 8 | create_issue_branch_job: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - name: Create Issue Branch 12 | uses: robvanderleek/create-issue-branch@main 13 | env: 14 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 15 | -------------------------------------------------------------------------------- /database/entities/migration_entity.go: -------------------------------------------------------------------------------- 1 | package entities 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | type Migration struct { 8 | ID uint `gorm:"primaryKey;autoIncrement" json:"id"` 9 | Name string `gorm:"type:varchar(255);uniqueIndex;not null" json:"name"` 10 | Batch int `gorm:"not null;index" json:"batch"` 11 | CreatedAt time.Time `gorm:"type:timestamp with time zone" json:"created_at"` 12 | } 13 | -------------------------------------------------------------------------------- /database/migration.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 5 | "gorm.io/gorm" 6 | ) 7 | 8 | func Migrate(db *gorm.DB) error { 9 | if err := db.AutoMigrate( 10 | &entities.Migration{}, 11 | &entities.User{}, 12 | &entities.RefreshToken{}, 13 | ); err != nil { 14 | return err 15 | } 16 | 17 | manager := NewMigrationManager(db) 18 | return manager.Run() 19 | } 20 | -------------------------------------------------------------------------------- /script/example_script.go: -------------------------------------------------------------------------------- 1 | package script 2 | 3 | import ( 4 | "fmt" 5 | 6 | "gorm.io/gorm" 7 | ) 8 | 9 | type ( 10 | ExampleScript struct { 11 | db *gorm.DB 12 | } 13 | ) 14 | 15 | func NewExampleScript(db *gorm.DB) *ExampleScript { 16 | return &ExampleScript{ 17 | db: db, 18 | } 19 | } 20 | 21 | func (s *ExampleScript) Run() error { 22 | // your script here 23 | fmt.Println("example script running") 24 | return nil 25 | } -------------------------------------------------------------------------------- /database/entities/common.go: -------------------------------------------------------------------------------- 1 | package entities 2 | 3 | import ( 4 | "time" 5 | ) 6 | 7 | type Timestamp struct { 8 | CreatedAt time.Time `gorm:"type:timestamp with time zone" json:"created_at"` 9 | UpdatedAt time.Time `gorm:"type:timestamp with time zone" json:"updated_at"` 10 | } 11 | 12 | type Authorization struct { 13 | Token string `json:"token" binding:"required"` 14 | Role string `json:"role" binding:"required,oneof=user admin"` 15 | } 16 | -------------------------------------------------------------------------------- /database/seeders/json/users.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "admin", 4 | "telp_number": "08123456789", 5 | "email": "admin1234@gmail.com", 6 | "password": "admin1234", 7 | "role": "admin", 8 | "is_verified": true 9 | }, 10 | { 11 | "name": "user", 12 | "telp_number": "08123456789", 13 | "email": "user1234@gmail.com", 14 | "password": "user1234", 15 | "role": "user", 16 | "is_verified": true 17 | } 18 | ] -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | APP_NAME=Go.Gin.Template 2 | IS_LOGGER=true 3 | 4 | DB_HOST=postgres 5 | DB_USER=postgres 6 | DB_PASS= 7 | DB_NAME= 8 | DB_PORT=5432 9 | 10 | NGINX_PORT=80 11 | GOLANG_PORT=8888 12 | APP_ENV=localhost 13 | JWT_SECRET= 14 | 15 | SMTP_HOST=smtp.gmail.com 16 | SMTP_PORT=587 17 | SMTP_SENDER_NAME="Go.Gin.Template " 18 | SMTP_AUTH_EMAIL= 19 | SMTP_AUTH_PASSWORD= -------------------------------------------------------------------------------- /docker/nginx/default.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name localhost; 4 | 5 | location / { 6 | proxy_pass http://app:8888; 7 | proxy_http_version 1.1; 8 | proxy_set_header Upgrade $http_upgrade; 9 | proxy_set_header Connection keep-alive; 10 | proxy_set_header Host $host; 11 | proxy_cache_bypass $http_upgrade; 12 | } 13 | 14 | error_page 404 /404.html; 15 | location = /404.html { 16 | root /usr/share/nginx/html; 17 | } 18 | } -------------------------------------------------------------------------------- /pkg/helpers/password.go: -------------------------------------------------------------------------------- 1 | package helpers 2 | 3 | import ( 4 | "golang.org/x/crypto/bcrypt" 5 | ) 6 | 7 | func HashPassword(password string) (string, error) { 8 | bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) 9 | return string(bytes), err 10 | } 11 | 12 | func CheckPassword(hashPassword string, plainPassword []byte) (bool, error) { 13 | hashPW := []byte(hashPassword) 14 | if err := bcrypt.CompareHashAndPassword(hashPW, plainPassword); err != nil { 15 | return false, err 16 | } 17 | return true, nil 18 | } 19 | -------------------------------------------------------------------------------- /database/entities/refresh_token_entity.go: -------------------------------------------------------------------------------- 1 | package entities 2 | 3 | import ( 4 | "time" 5 | 6 | "github.com/google/uuid" 7 | ) 8 | 9 | type RefreshToken struct { 10 | ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"` 11 | UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"` 12 | Token string `gorm:"type:varchar(255);not null;uniqueIndex" json:"token"` 13 | ExpiresAt time.Time `gorm:"type:timestamp with time zone;not null" json:"expires_at"` 14 | User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-"` 15 | 16 | Timestamp 17 | } 18 | -------------------------------------------------------------------------------- /database/migrations/20240101000000_create_users_table.go: -------------------------------------------------------------------------------- 1 | package migrations 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/database" 5 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 6 | "gorm.io/gorm" 7 | ) 8 | 9 | func init() { 10 | database.RegisterMigration("20240101000000_create_users_table", Up20240101000000CreateUsersTable, Down20240101000000CreateUsersTable) 11 | } 12 | 13 | func Up20240101000000CreateUsersTable(db *gorm.DB) error { 14 | return db.AutoMigrate(&entities.User{}) 15 | } 16 | 17 | func Down20240101000000CreateUsersTable(db *gorm.DB) error { 18 | return db.Migrator().DropTable(&entities.User{}) 19 | } 20 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Go 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v3 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v3 21 | with: 22 | go-version: 1.21 23 | 24 | - name: Build 25 | # run go mod tidy and go build -v ./... 26 | run: go mod tidy && go build -v ./... -------------------------------------------------------------------------------- /database/migrations/20240101000001_create_refresh_tokens_table.go: -------------------------------------------------------------------------------- 1 | package migrations 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/database" 5 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 6 | "gorm.io/gorm" 7 | ) 8 | 9 | func init() { 10 | database.RegisterMigration("20240101000001_create_refresh_tokens_table", Up20240101000001CreateRefreshTokensTable, Down20240101000001CreateRefreshTokensTable) 11 | } 12 | 13 | func Up20240101000001CreateRefreshTokensTable(db *gorm.DB) error { 14 | return db.AutoMigrate(&entities.RefreshToken{}) 15 | } 16 | 17 | func Down20240101000001CreateRefreshTokensTable(db *gorm.DB) error { 18 | return db.Migrator().DropTable(&entities.RefreshToken{}) 19 | } 20 | -------------------------------------------------------------------------------- /middlewares/cors.go: -------------------------------------------------------------------------------- 1 | package middlewares 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/gin-gonic/gin" 7 | ) 8 | 9 | func CORSMiddleware() gin.HandlerFunc { 10 | return func(c *gin.Context) { 11 | 12 | c.Header("Access-Control-Allow-Origin", "*") 13 | c.Header("Access-Control-Allow-Credentials", "true") 14 | c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") 15 | c.Header("Access-Control-Allow-Methods", "POST, HEAD, PATCH, OPTIONS, GET, PUT, DELETE") 16 | 17 | if c.Request.Method == http.MethodOptions { 18 | c.AbortWithStatus(204) 19 | return 20 | } 21 | 22 | c.Next() 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /pkg/utils/response.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | type Response struct { 4 | Status bool `json:"status"` 5 | Message string `json:"message"` 6 | Error any `json:"error,omitempty"` 7 | Data any `json:"data,omitempty"` 8 | Meta any `json:"meta,omitempty"` 9 | } 10 | 11 | type EmptyObj struct{} 12 | 13 | func BuildResponseSuccess(message string, data any) Response { 14 | res := Response{ 15 | Status: true, 16 | Message: message, 17 | Data: data, 18 | } 19 | return res 20 | } 21 | 22 | func BuildResponseFailed(message string, err string, data any) Response { 23 | res := Response{ 24 | Status: false, 25 | Message: message, 26 | Error: err, 27 | Data: data, 28 | } 29 | return res 30 | } 31 | -------------------------------------------------------------------------------- /config/email.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "github.com/spf13/viper" 5 | ) 6 | 7 | type EmailConfig struct { 8 | Host string `mapstructure:"SMTP_HOST"` 9 | Port int `mapstructure:"SMTP_PORT"` 10 | SenderName string `mapstructure:"SMTP_SENDER_NAME"` 11 | AuthEmail string `mapstructure:"SMTP_AUTH_EMAIL"` 12 | AuthPassword string `mapstructure:"SMTP_AUTH_PASSWORD"` 13 | } 14 | 15 | func NewEmailConfig() (*EmailConfig, error) { 16 | viper.SetConfigFile(".env") 17 | 18 | if err := viper.ReadInConfig(); err != nil { 19 | return nil, err 20 | } 21 | 22 | viper.AutomaticEnv() 23 | 24 | var config EmailConfig 25 | if err := viper.Unmarshal(&config); err != nil { 26 | return nil, err 27 | } 28 | 29 | return &config, nil 30 | } -------------------------------------------------------------------------------- /pkg/utils/email.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/config" 5 | 6 | "gopkg.in/gomail.v2" 7 | ) 8 | 9 | func SendMail(toEmail string, subject string, body string) error { 10 | emailConfig, err := config.NewEmailConfig() 11 | if err != nil { 12 | return err 13 | } 14 | 15 | mailer := gomail.NewMessage() 16 | mailer.SetHeader("From", emailConfig.AuthEmail) 17 | mailer.SetHeader("To", toEmail) 18 | mailer.SetHeader("Subject", subject) 19 | mailer.SetBody("text/html", body) 20 | 21 | dialer := gomail.NewDialer( 22 | emailConfig.Host, 23 | emailConfig.Port, 24 | emailConfig.AuthEmail, 25 | emailConfig.AuthPassword, 26 | ) 27 | 28 | err = dialer.DialAndSend(mailer) 29 | if err != nil { 30 | return err 31 | } 32 | 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | # Description 2 | 3 | Please include a summary of the changes and the related issue. 4 | 5 | Fixes #(issue) 6 | 7 | ## Type of change 8 | 9 | Please check all options that are relevant. 10 | 11 | - [ ] Bug fix (non-breaking change which fixes an issue) 12 | - [ ] New feature (non-breaking change which adds functionality) 13 | - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) 14 | 15 | # Screenshot 16 | 17 | (Opsional) 18 | 19 | # Checklist: 20 | 21 | - [ ] I have performed a self-review of my code 22 | - [ ] I have commented my code, particularly in hard-to-understand areas 23 | - [ ] My changes generate no new warnings 24 | - [ ] I have added tests that prove my fix is effective or that my feature works 25 | - [ ] I have made API documentation and example response in Postman -------------------------------------------------------------------------------- /modules/auth/routes.go: -------------------------------------------------------------------------------- 1 | package auth 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/controller" 5 | "github.com/gin-gonic/gin" 6 | "github.com/samber/do" 7 | ) 8 | 9 | func RegisterRoutes(server *gin.Engine, injector *do.Injector) { 10 | authController := do.MustInvoke[controller.AuthController](injector) 11 | 12 | authRoutes := server.Group("/api/auth") 13 | { 14 | authRoutes.POST("/register", authController.Register) 15 | authRoutes.POST("/login", authController.Login) 16 | authRoutes.POST("/refresh", authController.RefreshToken) 17 | authRoutes.POST("/logout", authController.Logout) 18 | authRoutes.POST("/send-verification-email", authController.SendVerificationEmail) 19 | authRoutes.POST("/verify-email", authController.VerifyEmail) 20 | authRoutes.POST("/send-password-reset", authController.SendPasswordReset) 21 | authRoutes.POST("/reset-password", authController.ResetPassword) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /config/logger.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "log" 5 | "os" 6 | "strings" 7 | "time" 8 | 9 | "gorm.io/gorm/logger" 10 | ) 11 | 12 | const ( 13 | LOG_DIR = "./config/logs/query_log" 14 | ) 15 | 16 | func SetupLogger() logger.Interface { 17 | err := os.MkdirAll(LOG_DIR, os.ModePerm) 18 | if err != nil { 19 | log.Fatalf("failed to create log directory: %v", err) 20 | } 21 | 22 | currentMonth := time.Now().Format("January") 23 | currentMonth = strings.ToLower(currentMonth) 24 | logFileName := currentMonth + "_query.log" 25 | 26 | logFile, err := os.OpenFile(LOG_DIR+"/"+logFileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) 27 | if err != nil { 28 | log.Fatalf("failed to open log file: %v", err) 29 | } 30 | 31 | newLogger := logger.New( 32 | log.New(logFile, "\r\n", log.LstdFlags), 33 | logger.Config{ 34 | SlowThreshold: time.Second, 35 | LogLevel: logger.Info, 36 | Colorful: false, 37 | }, 38 | ) 39 | 40 | return newLogger 41 | } 42 | -------------------------------------------------------------------------------- /modules/user/routes.go: -------------------------------------------------------------------------------- 1 | package user 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/middlewares" 5 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/controller" 7 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 8 | "github.com/gin-gonic/gin" 9 | "github.com/samber/do" 10 | ) 11 | 12 | func RegisterRoutes(server *gin.Engine, injector *do.Injector) { 13 | userController := do.MustInvoke[controller.UserController](injector) 14 | jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService) 15 | 16 | userRoutes := server.Group("/api/user") 17 | { 18 | userRoutes.GET("", userController.GetAllUser) 19 | userRoutes.GET("/me", middlewares.Authenticate(jwtService), userController.Me) 20 | userRoutes.PUT("/:id", middlewares.Authenticate(jwtService), userController.Update) 21 | userRoutes.DELETE("/:id", middlewares.Authenticate(jwtService), userController.Delete) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 M Naufal Badruttamam 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pkg/utils/file.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "mime/multipart" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | const PATH = "assets" 12 | 13 | func UploadFile(file *multipart.FileHeader, path string) error { 14 | parts := strings.Split(path, "/") 15 | fileID := parts[1] 16 | dirPath := fmt.Sprintf("%s/%s", PATH, parts[0]) 17 | 18 | if _, err := os.Stat(dirPath); os.IsNotExist(err) { 19 | if err := os.MkdirAll(dirPath, 0777); err != nil { 20 | return err 21 | } 22 | } 23 | 24 | filePath := fmt.Sprintf("%s/%s", dirPath, fileID) 25 | 26 | uploadedFile, err := file.Open() 27 | if err != nil { 28 | return err 29 | } 30 | defer uploadedFile.Close() 31 | 32 | // Using os.Create to open the file with appropriate permissions 33 | targetFile, err := os.Create(filePath) 34 | if err != nil { 35 | return err 36 | } 37 | defer targetFile.Close() 38 | 39 | // Copy file contents from uploadedFile to targetFile 40 | _, err = io.Copy(targetFile, uploadedFile) 41 | if err != nil { 42 | return err 43 | } 44 | 45 | return nil 46 | } 47 | 48 | func GetExtensions(filename string) string { 49 | return strings.Split(filename, ".")[len(strings.Split(filename, "."))-1] 50 | } 51 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | app: 3 | build: 4 | context: . 5 | dockerfile: ./docker/Dockerfile 6 | container_name: ${APP_NAME:-go-gin-clean-starter}-app 7 | volumes: 8 | - .:/app 9 | ports: 10 | - ${GOLANG_PORT:-8888}:8888 11 | networks: 12 | - app-network 13 | 14 | nginx: 15 | image: nginx:latest 16 | container_name: ${APP_NAME:-go-gin-clean-starter}-nginx 17 | ports: 18 | - ${NGINX_PORT:-81}:80 19 | volumes: 20 | - .:/var/www/html 21 | - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf 22 | depends_on: 23 | - app 24 | networks: 25 | - app-network 26 | 27 | postgres: 28 | hostname: postgres 29 | container_name: ${APP_NAME:-go-gin-clean-starter}-db 30 | image: postgres:latest 31 | ports: 32 | - ${DB_PORT}:5432 33 | volumes: 34 | - ./docker/postgresql/tmp:/var/lib/postgresql/data 35 | - app-data:/var/lib/postgresql/data 36 | environment: 37 | - POSTGRES_USER=${DB_USER} 38 | - POSTGRES_PASSWORD=${DB_PASS} 39 | - POSTGRES_DB=${DB_NAME} 40 | networks: 41 | - app-network 42 | 43 | volumes: 44 | app-data: 45 | 46 | networks: 47 | app-network: 48 | driver: bridge 49 | -------------------------------------------------------------------------------- /database/seeders/seeds/user_seed.go: -------------------------------------------------------------------------------- 1 | package seeds 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "io" 7 | "os" 8 | 9 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 10 | "gorm.io/gorm" 11 | ) 12 | 13 | func ListUserSeeder(db *gorm.DB) error { 14 | jsonFile, err := os.Open("./database/seeders/json/users.json") 15 | if err != nil { 16 | return err 17 | } 18 | 19 | jsonData, err := io.ReadAll(jsonFile) 20 | if err != nil { 21 | return err 22 | } 23 | 24 | var listUser []entities.User 25 | if err := json.Unmarshal(jsonData, &listUser); err != nil { 26 | return err 27 | } 28 | 29 | hasTable := db.Migrator().HasTable(&entities.User{}) 30 | if !hasTable { 31 | if err := db.Migrator().CreateTable(&entities.User{}); err != nil { 32 | return err 33 | } 34 | } 35 | 36 | for _, data := range listUser { 37 | var user entities.User 38 | err := db.Where(&entities.User{Email: data.Email}).First(&user).Error 39 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { 40 | return err 41 | } 42 | 43 | isData := db.Find(&user, "email = ?", data.Email).RowsAffected 44 | if isData == 0 { 45 | if err := db.Create(&data).Error; err != nil { 46 | return err 47 | } 48 | } 49 | } 50 | 51 | return nil 52 | } 53 | -------------------------------------------------------------------------------- /modules/user/validation/user_validation.go: -------------------------------------------------------------------------------- 1 | package validation 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 5 | "github.com/go-playground/validator/v10" 6 | ) 7 | 8 | type UserValidation struct { 9 | validate *validator.Validate 10 | } 11 | 12 | func NewUserValidation() *UserValidation { 13 | validate := validator.New() 14 | 15 | validate.RegisterValidation("telp_number", validateTelpNumber) 16 | validate.RegisterValidation("name", validateName) 17 | 18 | return &UserValidation{ 19 | validate: validate, 20 | } 21 | } 22 | 23 | func (v *UserValidation) ValidateUserCreateRequest(req dto.UserCreateRequest) error { 24 | return v.validate.Struct(req) 25 | } 26 | 27 | func (v *UserValidation) ValidateUserUpdateRequest(req dto.UserUpdateRequest) error { 28 | return v.validate.Struct(req) 29 | } 30 | 31 | func validateTelpNumber(fl validator.FieldLevel) bool { 32 | telp := fl.Field().String() 33 | // Basic phone number validation - should be numeric and have reasonable length 34 | if len(telp) < 8 || len(telp) > 15 { 35 | return false 36 | } 37 | // Add more phone validation rules as needed 38 | return true 39 | } 40 | 41 | func validateName(fl validator.FieldLevel) bool { 42 | name := fl.Field().String() 43 | // Name should not be empty and not too long 44 | return len(name) > 0 && len(name) <= 100 45 | } 46 | -------------------------------------------------------------------------------- /database/entities/user_entity.go: -------------------------------------------------------------------------------- 1 | package entities 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 5 | "github.com/Caknoooo/go-gin-clean-starter/pkg/helpers" 6 | "github.com/google/uuid" 7 | "gorm.io/gorm" 8 | ) 9 | 10 | type User struct { 11 | ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"` 12 | Name string `gorm:"type:varchar(100);not null" json:"name"` 13 | Email string `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"` 14 | TelpNumber string `gorm:"type:varchar(20);index" json:"telp_number"` 15 | Password string `gorm:"type:varchar(255);not null" json:"password"` 16 | Role string `gorm:"type:varchar(50);not null;default:'user'" json:"role"` 17 | ImageUrl string `gorm:"type:varchar(255)" json:"image_url"` 18 | IsVerified bool `gorm:"default:false" json:"is_verified"` 19 | 20 | Timestamp 21 | } 22 | 23 | // BeforeCreate hook to hash password and set defaults 24 | func (u *User) BeforeCreate(_ *gorm.DB) (err error) { 25 | // Hash password 26 | if u.Password != "" { 27 | u.Password, err = helpers.HashPassword(u.Password) 28 | if err != nil { 29 | return err 30 | } 31 | } 32 | 33 | // Ensure UUID is set 34 | if u.ID == uuid.Nil { 35 | u.ID = uuid.New() 36 | } 37 | 38 | // Set default role if not specified 39 | if u.Role == "" { 40 | u.Role = constants.ENUM_ROLE_USER 41 | } 42 | 43 | return nil 44 | } -------------------------------------------------------------------------------- /modules/user/query/user_query.go: -------------------------------------------------------------------------------- 1 | package query 2 | 3 | import ( 4 | "github.com/Caknoooo/go-pagination" 5 | "gorm.io/gorm" 6 | ) 7 | 8 | type User struct { 9 | ID string `json:"id"` 10 | Name string `json:"name"` 11 | Email string `json:"email"` 12 | TelpNumber string `json:"telp_number"` 13 | Role string `json:"role"` 14 | ImageUrl string `json:"image_url"` 15 | IsVerified bool `json:"is_verified"` 16 | } 17 | 18 | type UserFilter struct { 19 | pagination.BaseFilter 20 | } 21 | 22 | func (f *UserFilter) ApplyFilters(query *gorm.DB) *gorm.DB { 23 | // Apply your filters here 24 | return query 25 | } 26 | 27 | func (f *UserFilter) GetTableName() string { 28 | return "users" 29 | } 30 | 31 | func (f *UserFilter) GetSearchFields() []string { 32 | return []string{"name"} 33 | } 34 | 35 | func (f *UserFilter) GetDefaultSort() string { 36 | return "id asc" 37 | } 38 | 39 | func (f *UserFilter) GetIncludes() []string { 40 | return f.Includes 41 | } 42 | 43 | func (f *UserFilter) GetPagination() pagination.PaginationRequest { 44 | return f.Pagination 45 | } 46 | 47 | func (f *UserFilter) Validate() { 48 | var validIncludes []string 49 | allowedIncludes := f.GetAllowedIncludes() 50 | for _, include := range f.Includes { 51 | if allowedIncludes[include] { 52 | validIncludes = append(validIncludes, include) 53 | } 54 | } 55 | f.Includes = validIncludes 56 | } 57 | 58 | func (f *UserFilter) GetAllowedIncludes() map[string]bool { 59 | return map[string]bool{} 60 | } 61 | -------------------------------------------------------------------------------- /modules/auth/dto/auth_dto.go: -------------------------------------------------------------------------------- 1 | package dto 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | const ( 8 | MESSAGE_FAILED_REFRESH_TOKEN = "failed refresh token" 9 | MESSAGE_SUCCESS_REFRESH_TOKEN = "success refresh token" 10 | MESSAGE_FAILED_LOGOUT = "failed logout" 11 | MESSAGE_SUCCESS_LOGOUT = "success logout" 12 | MESSAGE_FAILED_SEND_PASSWORD_RESET = "failed send password reset" 13 | MESSAGE_SUCCESS_SEND_PASSWORD_RESET = "success send password reset" 14 | MESSAGE_FAILED_RESET_PASSWORD = "failed reset password" 15 | MESSAGE_SUCCESS_RESET_PASSWORD = "success reset password" 16 | ) 17 | 18 | var ( 19 | ErrRefreshTokenNotFound = errors.New("refresh token not found") 20 | ErrRefreshTokenExpired = errors.New("refresh token expired") 21 | ErrInvalidCredentials = errors.New("invalid credentials") 22 | ErrPasswordResetToken = errors.New("password reset token invalid") 23 | ) 24 | 25 | type ( 26 | RefreshTokenRequest struct { 27 | RefreshToken string `json:"refresh_token" binding:"required"` 28 | } 29 | 30 | TokenResponse struct { 31 | AccessToken string `json:"access_token"` 32 | RefreshToken string `json:"refresh_token"` 33 | Role string `json:"role"` 34 | } 35 | 36 | SendPasswordResetRequest struct { 37 | Email string `json:"email" binding:"required,email"` 38 | } 39 | 40 | ResetPasswordRequest struct { 41 | Token string `json:"token" binding:"required"` 42 | NewPassword string `json:"new_password" binding:"required,min=8"` 43 | } 44 | ) 45 | -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "os" 6 | 7 | "github.com/Caknoooo/go-gin-clean-starter/middlewares" 8 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth" 9 | "github.com/Caknoooo/go-gin-clean-starter/modules/user" 10 | "github.com/Caknoooo/go-gin-clean-starter/providers" 11 | "github.com/Caknoooo/go-gin-clean-starter/script" 12 | "github.com/samber/do" 13 | 14 | "github.com/common-nighthawk/go-figure" 15 | "github.com/gin-gonic/gin" 16 | ) 17 | 18 | func args(injector *do.Injector) bool { 19 | if len(os.Args) > 1 { 20 | flag := script.Commands(injector) 21 | return flag 22 | } 23 | 24 | return true 25 | } 26 | 27 | func run(server *gin.Engine) { 28 | server.Static("/assets", "./assets") 29 | 30 | port := os.Getenv("GOLANG_PORT") 31 | if port == "" { 32 | port = "8888" 33 | } 34 | 35 | var serve string 36 | if os.Getenv("APP_ENV") == "localhost" { 37 | serve = "0.0.0.0:" + port 38 | } else { 39 | serve = ":" + port 40 | } 41 | 42 | myFigure := figure.NewColorFigure("Caknoo", "", "green", true) 43 | myFigure.Print() 44 | 45 | if err := server.Run(serve); err != nil { 46 | log.Fatalf("error running server: %v", err) 47 | } 48 | } 49 | 50 | func main() { 51 | var ( 52 | injector = do.New() 53 | ) 54 | 55 | providers.RegisterDependencies(injector) 56 | 57 | if !args(injector) { 58 | return 59 | } 60 | 61 | server := gin.Default() 62 | server.Use(middlewares.CORSMiddleware()) 63 | 64 | // Register module routes 65 | user.RegisterRoutes(server, injector) 66 | auth.RegisterRoutes(server, injector) 67 | 68 | run(server) 69 | } 70 | -------------------------------------------------------------------------------- /pkg/utils/email-template/base_mail.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Verify Your Account 7 | 37 | 38 | 39 |
40 |

Verify Your Account

41 |

Hello, {{ .Email }}

42 |

43 | Thank you for using my template! To complete your registration and 44 | activate your account, please click the link below: 45 |

46 |
47 | Verify My Account 59 |
60 |

61 | If you are unable to click the link above, please copy and paste the 62 | following URL into your web browser: 63 |

64 |

{{ .Verify }}

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /middlewares/authentication.go: -------------------------------------------------------------------------------- 1 | package middlewares 2 | 3 | import ( 4 | "net/http" 5 | "strings" 6 | 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" 8 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 9 | "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" 10 | "github.com/gin-gonic/gin" 11 | ) 12 | 13 | func Authenticate(jwtService service.JWTService) gin.HandlerFunc { 14 | return func(ctx *gin.Context) { 15 | authHeader := ctx.GetHeader("Authorization") 16 | 17 | if authHeader == "" { 18 | response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_FOUND, nil) 19 | ctx.AbortWithStatusJSON(http.StatusUnauthorized, response) 20 | return 21 | } 22 | 23 | if !strings.Contains(authHeader, "Bearer ") { 24 | response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_VALID, nil) 25 | ctx.AbortWithStatusJSON(http.StatusUnauthorized, response) 26 | return 27 | } 28 | 29 | authHeader = strings.Replace(authHeader, "Bearer ", "", -1) 30 | token, err := jwtService.ValidateToken(authHeader) 31 | if err != nil { 32 | response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_TOKEN_NOT_VALID, nil) 33 | ctx.AbortWithStatusJSON(http.StatusUnauthorized, response) 34 | return 35 | } 36 | 37 | if !token.Valid { 38 | response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, dto.MESSAGE_FAILED_DENIED_ACCESS, nil) 39 | ctx.AbortWithStatusJSON(http.StatusUnauthorized, response) 40 | return 41 | } 42 | 43 | userId, err := jwtService.GetUserIDByToken(authHeader) 44 | if err != nil { 45 | response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_PROSES_REQUEST, err.Error(), nil) 46 | ctx.AbortWithStatusJSON(http.StatusUnauthorized, response) 47 | return 48 | } 49 | 50 | ctx.Set("token", authHeader) 51 | ctx.Set("user_id", userId) 52 | ctx.Next() 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /providers/core.go: -------------------------------------------------------------------------------- 1 | package providers 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/config" 5 | authController "github.com/Caknoooo/go-gin-clean-starter/modules/auth/controller" 6 | authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository" 7 | authService "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" 8 | userController "github.com/Caknoooo/go-gin-clean-starter/modules/user/controller" 9 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/repository" 10 | userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service" 11 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 12 | "github.com/samber/do" 13 | "gorm.io/gorm" 14 | ) 15 | 16 | func InitDatabase(injector *do.Injector) { 17 | do.ProvideNamed(injector, constants.DB, func(i *do.Injector) (*gorm.DB, error) { 18 | return config.SetUpDatabaseConnection(), nil 19 | }) 20 | } 21 | 22 | func RegisterDependencies(injector *do.Injector) { 23 | InitDatabase(injector) 24 | 25 | do.ProvideNamed(injector, constants.JWTService, func(i *do.Injector) (authService.JWTService, error) { 26 | return authService.NewJWTService(), nil 27 | }) 28 | 29 | db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB) 30 | jwtService := do.MustInvokeNamed[authService.JWTService](injector, constants.JWTService) 31 | 32 | userRepository := repository.NewUserRepository(db) 33 | refreshTokenRepository := authRepo.NewRefreshTokenRepository(db) 34 | 35 | userService := userService.NewUserService(userRepository, db) 36 | authService := authService.NewAuthService(userRepository, refreshTokenRepository, jwtService, db) 37 | 38 | do.Provide( 39 | injector, func(i *do.Injector) (userController.UserController, error) { 40 | return userController.NewUserController(i, userService), nil 41 | }, 42 | ) 43 | 44 | do.Provide( 45 | injector, func(i *do.Injector) (authController.AuthController, error) { 46 | return authController.NewAuthController(i, authService), nil 47 | }, 48 | ) 49 | } 50 | -------------------------------------------------------------------------------- /modules/auth/validation/auth_validation.go: -------------------------------------------------------------------------------- 1 | package validation 2 | 3 | import ( 4 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto" 5 | userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 6 | "github.com/go-playground/validator/v10" 7 | ) 8 | 9 | type AuthValidation struct { 10 | validate *validator.Validate 11 | } 12 | 13 | func NewAuthValidation() *AuthValidation { 14 | validate := validator.New() 15 | 16 | validate.RegisterValidation("password", validatePassword) 17 | validate.RegisterValidation("email", validateEmail) 18 | 19 | return &AuthValidation{ 20 | validate: validate, 21 | } 22 | } 23 | 24 | func (v *AuthValidation) ValidateRegisterRequest(req userDto.UserCreateRequest) error { 25 | return v.validate.Struct(req) 26 | } 27 | 28 | func (v *AuthValidation) ValidateLoginRequest(req userDto.UserLoginRequest) error { 29 | return v.validate.Struct(req) 30 | } 31 | 32 | func (v *AuthValidation) ValidateRefreshTokenRequest(req dto.RefreshTokenRequest) error { 33 | return v.validate.Struct(req) 34 | } 35 | 36 | func (v *AuthValidation) ValidateSendPasswordResetRequest(req dto.SendPasswordResetRequest) error { 37 | return v.validate.Struct(req) 38 | } 39 | 40 | func (v *AuthValidation) ValidateResetPasswordRequest(req dto.ResetPasswordRequest) error { 41 | return v.validate.Struct(req) 42 | } 43 | 44 | func (v *AuthValidation) ValidateSendVerificationEmailRequest(req userDto.SendVerificationEmailRequest) error { 45 | return v.validate.Struct(req) 46 | } 47 | 48 | func (v *AuthValidation) ValidateVerifyEmailRequest(req userDto.VerifyEmailRequest) error { 49 | return v.validate.Struct(req) 50 | } 51 | 52 | // Custom validators 53 | func validatePassword(fl validator.FieldLevel) bool { 54 | password := fl.Field().String() 55 | if len(password) < 8 { 56 | return false 57 | } 58 | // Add more password validation rules as needed 59 | return true 60 | } 61 | 62 | func validateEmail(fl validator.FieldLevel) bool { 63 | email := fl.Field().String() 64 | // Basic email validation - you can use regex for more complex validation 65 | return len(email) > 0 && len(email) < 255 66 | } 67 | -------------------------------------------------------------------------------- /modules/user/tests/user_validation_test.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/validation" 8 | "github.com/stretchr/testify/assert" 9 | ) 10 | 11 | func TestUserValidation_ValidateUserCreateRequest_Success(t *testing.T) { 12 | userValidation := validation.NewUserValidation() 13 | 14 | req := dto.UserCreateRequest{ 15 | Name: "Test User", 16 | Email: "test@example.com", 17 | TelpNumber: "12345678", 18 | Password: "password123", 19 | } 20 | 21 | err := userValidation.ValidateUserCreateRequest(req) 22 | 23 | assert.NoError(t, err) 24 | } 25 | 26 | func TestUserValidation_ValidateUserCreateRequest_InvalidName(t *testing.T) { 27 | userValidation := validation.NewUserValidation() 28 | 29 | req := dto.UserCreateRequest{ 30 | Name: "", // This will be caught by binding:"required,min=2,max=100" in DTO 31 | Email: "test@example.com", 32 | TelpNumber: "12345678", 33 | Password: "password123", 34 | } 35 | 36 | err := userValidation.ValidateUserCreateRequest(req) 37 | 38 | // The validation should pass because DTO binding handles name validation 39 | // Custom validation only adds extra checks beyond DTO binding 40 | assert.NoError(t, err) 41 | } 42 | 43 | func TestUserValidation_ValidateUserUpdateRequest_Success(t *testing.T) { 44 | userValidation := validation.NewUserValidation() 45 | 46 | req := dto.UserUpdateRequest{ 47 | Name: "Updated Name", 48 | TelpNumber: "87654321", 49 | Email: "updated@example.com", 50 | } 51 | 52 | err := userValidation.ValidateUserUpdateRequest(req) 53 | 54 | assert.NoError(t, err) 55 | } 56 | 57 | func TestUserValidation_ValidateUserUpdateRequest_InvalidTelp(t *testing.T) { 58 | userValidation := validation.NewUserValidation() 59 | 60 | req := dto.UserUpdateRequest{ 61 | Name: "Updated Name", 62 | TelpNumber: "123", // This will be caught by binding:"omitempty,min=8,max=20" in DTO 63 | Email: "updated@example.com", 64 | } 65 | 66 | err := userValidation.ValidateUserUpdateRequest(req) 67 | 68 | // The validation should pass because DTO binding handles telp validation 69 | // Custom validation only adds extra checks beyond DTO binding 70 | assert.NoError(t, err) 71 | } 72 | -------------------------------------------------------------------------------- /config/database.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | 7 | "github.com/joho/godotenv" 8 | "gorm.io/driver/postgres" 9 | "gorm.io/driver/sqlite" 10 | "gorm.io/gorm" 11 | ) 12 | 13 | func RunExtension(db *gorm.DB) { 14 | db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";") 15 | } 16 | 17 | func SetUpDatabaseConnection() *gorm.DB { 18 | err := godotenv.Load(".env") 19 | if err != nil { 20 | panic(err) 21 | } 22 | 23 | dbUser := os.Getenv("DB_USER") 24 | dbPass := os.Getenv("DB_PASS") 25 | dbHost := os.Getenv("DB_HOST") 26 | dbName := os.Getenv("DB_NAME") 27 | dbPort := os.Getenv("DB_PORT") 28 | 29 | dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v", dbHost, dbUser, dbPass, dbName, dbPort) 30 | 31 | db, err := gorm.Open(postgres.New(postgres.Config{ 32 | DSN: dsn, 33 | PreferSimpleProtocol: true, 34 | }), &gorm.Config{ 35 | Logger: SetupLogger(), 36 | }) 37 | if err != nil { 38 | panic(err) 39 | } 40 | 41 | RunExtension(db) 42 | 43 | return db 44 | } 45 | 46 | func SetUpTestDatabaseConnection() *gorm.DB { 47 | dbUser := getEnvOrDefault("DB_USER", "postgres") 48 | dbPass := getEnvOrDefault("DB_PASS", "password") 49 | dbHost := getEnvOrDefault("DB_HOST", "localhost") 50 | dbName := getEnvOrDefault("DB_NAME", "test_db") 51 | dbPort := getEnvOrDefault("DB_PORT", "5432") 52 | 53 | dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v", dbHost, dbUser, dbPass, dbName, dbPort) 54 | 55 | db, err := gorm.Open(postgres.New(postgres.Config{ 56 | DSN: dsn, 57 | PreferSimpleProtocol: true, 58 | }), &gorm.Config{}) 59 | if err != nil { 60 | panic(err) 61 | } 62 | 63 | RunExtension(db) 64 | 65 | return db 66 | } 67 | 68 | func SetUpInMemoryDatabase() *gorm.DB { 69 | db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) 70 | if err != nil { 71 | panic(err) 72 | } 73 | return db 74 | } 75 | 76 | func SetUpTestSQLiteDatabase() *gorm.DB { 77 | db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) 78 | if err != nil { 79 | panic(err) 80 | } 81 | return db 82 | } 83 | 84 | func getEnvOrDefault(key, defaultValue string) string { 85 | if value := os.Getenv(key); value != "" { 86 | return value 87 | } 88 | return defaultValue 89 | } 90 | 91 | func CloseDatabaseConnection(db *gorm.DB) { 92 | dbSQL, err := db.DB() 93 | if err != nil { 94 | panic(err) 95 | } 96 | dbSQL.Close() 97 | } 98 | -------------------------------------------------------------------------------- /modules/user/service/user_service.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/repository" 8 | "gorm.io/gorm" 9 | ) 10 | 11 | type UserService interface { 12 | GetUserById(ctx context.Context, userId string) (dto.UserResponse, error) 13 | Update(ctx context.Context, req dto.UserUpdateRequest, userId string) (dto.UserUpdateResponse, error) 14 | Delete(ctx context.Context, userId string) error 15 | } 16 | 17 | type userService struct { 18 | userRepository repository.UserRepository 19 | db *gorm.DB 20 | } 21 | 22 | func NewUserService( 23 | userRepo repository.UserRepository, 24 | db *gorm.DB, 25 | ) UserService { 26 | return &userService{ 27 | userRepository: userRepo, 28 | db: db, 29 | } 30 | } 31 | 32 | func (s *userService) GetUserById(ctx context.Context, userId string) (dto.UserResponse, error) { 33 | user, err := s.userRepository.GetUserById(ctx, s.db, userId) 34 | if err != nil { 35 | return dto.UserResponse{}, err 36 | } 37 | 38 | return dto.UserResponse{ 39 | ID: user.ID.String(), 40 | Name: user.Name, 41 | Email: user.Email, 42 | TelpNumber: user.TelpNumber, 43 | Role: user.Role, 44 | ImageUrl: user.ImageUrl, 45 | IsVerified: user.IsVerified, 46 | }, nil 47 | } 48 | 49 | func (s *userService) Update(ctx context.Context, req dto.UserUpdateRequest, userId string) (dto.UserUpdateResponse, error) { 50 | user, err := s.userRepository.GetUserById(ctx, s.db, userId) 51 | if err != nil { 52 | return dto.UserUpdateResponse{}, dto.ErrUserNotFound 53 | } 54 | 55 | if req.Name != "" { 56 | user.Name = req.Name 57 | } 58 | if req.Email != "" { 59 | user.Email = req.Email 60 | } 61 | if req.TelpNumber != "" { 62 | user.TelpNumber = req.TelpNumber 63 | } 64 | 65 | updatedUser, err := s.userRepository.Update(ctx, s.db, user) 66 | if err != nil { 67 | return dto.UserUpdateResponse{}, err 68 | } 69 | 70 | return dto.UserUpdateResponse{ 71 | ID: updatedUser.ID.String(), 72 | Name: updatedUser.Name, 73 | TelpNumber: updatedUser.TelpNumber, 74 | Role: updatedUser.Role, 75 | Email: updatedUser.Email, 76 | IsVerified: updatedUser.IsVerified, 77 | }, nil 78 | } 79 | 80 | func (s *userService) Delete(ctx context.Context, userId string) error { 81 | return s.userRepository.Delete(ctx, s.db, userId) 82 | } 83 | -------------------------------------------------------------------------------- /pkg/utils/aes.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "crypto/aes" 5 | "crypto/cipher" 6 | "crypto/rand" 7 | "encoding/hex" 8 | "errors" 9 | "fmt" 10 | "io" 11 | ) 12 | 13 | const ( 14 | // todo 15 | KEY = "8e71bbce7451ba2835de5aea73e4f3f96821455240823d2fd8174975b8321bfc!" 16 | ) 17 | 18 | // https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes 19 | 20 | func AESEncrypt(stringToEncrypt string) (encryptedString string, err error) { 21 | //Since the key is in string, we need to convert decode it to bytes 22 | key, err := hex.DecodeString(KEY) 23 | if err != nil { 24 | return "", err 25 | } 26 | plaintext := []byte(stringToEncrypt) 27 | 28 | //Create a new Cipher Block from the key 29 | block, err := aes.NewCipher(key) 30 | if err != nil { 31 | return "", err 32 | } 33 | 34 | //Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode 35 | //https://golang.org/pkg/crypto/cipher/#NewGCM 36 | aesGCM, err := cipher.NewGCM(block) 37 | if err != nil { 38 | return "", err 39 | } 40 | 41 | //Create a nonce. Nonce should be from GCM 42 | nonce := make([]byte, aesGCM.NonceSize()) 43 | if _, err = io.ReadFull(rand.Reader, nonce); err != nil { 44 | return "", err 45 | } 46 | 47 | //Encrypt the data using aesGCM.Seal 48 | //Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix. 49 | ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil) 50 | return fmt.Sprintf("%x", ciphertext), nil 51 | } 52 | 53 | func AESDecrypt(encryptedString string) (decryptedString string, err error) { 54 | defer func() { 55 | if r := recover(); r != nil { 56 | decryptedString = "" 57 | err = errors.New("error in decrypting") 58 | } 59 | }() 60 | 61 | key, err := hex.DecodeString(KEY) 62 | if err != nil { 63 | return "", errors.New("error in decoding key") 64 | } 65 | 66 | enc, err := hex.DecodeString(encryptedString) 67 | if err != nil { 68 | return "", errors.New("error in decoding encrypted string") 69 | } 70 | 71 | //Create a new Cipher Block from the key 72 | block, err := aes.NewCipher(key) 73 | if err != nil { 74 | return "", err 75 | } 76 | 77 | //Create a new GCM 78 | aesGCM, err := cipher.NewGCM(block) 79 | if err != nil { 80 | return "", err 81 | } 82 | 83 | //Get the nonce size 84 | nonceSize := aesGCM.NonceSize() 85 | 86 | //Extract the nonce from the encrypted data 87 | nonce, ciphertext := enc[:nonceSize], enc[nonceSize:] 88 | 89 | //Decrypt the data 90 | plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil) 91 | if err != nil { 92 | return "", nil 93 | } 94 | 95 | return string(plaintext), nil 96 | } 97 | -------------------------------------------------------------------------------- /modules/auth/repository/refresh_token_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "context" 5 | "time" 6 | 7 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 8 | "gorm.io/gorm" 9 | ) 10 | 11 | type RefreshTokenRepository interface { 12 | Create(ctx context.Context, tx *gorm.DB, token entities.RefreshToken) (entities.RefreshToken, error) 13 | FindByToken(ctx context.Context, tx *gorm.DB, token string) (entities.RefreshToken, error) 14 | DeleteByUserID(ctx context.Context, tx *gorm.DB, userID string) error 15 | DeleteByToken(ctx context.Context, tx *gorm.DB, token string) error 16 | DeleteExpired(ctx context.Context, tx *gorm.DB) error 17 | } 18 | 19 | type refreshTokenRepository struct { 20 | db *gorm.DB 21 | } 22 | 23 | func NewRefreshTokenRepository(db *gorm.DB) RefreshTokenRepository { 24 | return &refreshTokenRepository{ 25 | db: db, 26 | } 27 | } 28 | 29 | func (r *refreshTokenRepository) Create( 30 | ctx context.Context, 31 | tx *gorm.DB, 32 | token entities.RefreshToken, 33 | ) (entities.RefreshToken, error) { 34 | if tx == nil { 35 | tx = r.db 36 | } 37 | 38 | if err := tx.WithContext(ctx).Create(&token).Error; err != nil { 39 | return entities.RefreshToken{}, err 40 | } 41 | 42 | return token, nil 43 | } 44 | 45 | func (r *refreshTokenRepository) FindByToken(ctx context.Context, tx *gorm.DB, token string) ( 46 | entities.RefreshToken, 47 | error, 48 | ) { 49 | if tx == nil { 50 | tx = r.db 51 | } 52 | 53 | var refreshToken entities.RefreshToken 54 | if err := tx.WithContext(ctx).Where("token = ?", token).Preload("User").Take(&refreshToken).Error; err != nil { 55 | return entities.RefreshToken{}, err 56 | } 57 | 58 | return refreshToken, nil 59 | } 60 | 61 | func (r *refreshTokenRepository) DeleteByUserID(ctx context.Context, tx *gorm.DB, userID string) error { 62 | if tx == nil { 63 | tx = r.db 64 | } 65 | 66 | if err := tx.WithContext(ctx).Where("user_id = ?", userID).Delete(&entities.RefreshToken{}).Error; err != nil { 67 | return err 68 | } 69 | 70 | return nil 71 | } 72 | 73 | func (r *refreshTokenRepository) DeleteByToken(ctx context.Context, tx *gorm.DB, token string) error { 74 | if tx == nil { 75 | tx = r.db 76 | } 77 | 78 | if err := tx.WithContext(ctx).Where("token = ?", token).Delete(&entities.RefreshToken{}).Error; err != nil { 79 | return err 80 | } 81 | 82 | return nil 83 | } 84 | 85 | func (r *refreshTokenRepository) DeleteExpired(ctx context.Context, tx *gorm.DB) error { 86 | if tx == nil { 87 | tx = r.db 88 | } 89 | 90 | if err := tx.WithContext(ctx).Where("expires_at < ?", time.Now()).Delete(&entities.RefreshToken{}).Error; err != nil { 91 | return err 92 | } 93 | 94 | return nil 95 | } 96 | -------------------------------------------------------------------------------- /modules/auth/tests/auth_validation_test.go: -------------------------------------------------------------------------------- 1 | package tests 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/validation" 8 | userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestAuthValidation_ValidateRegisterRequest_Success(t *testing.T) { 13 | authValidation := validation.NewAuthValidation() 14 | 15 | req := userDto.UserCreateRequest{ 16 | Name: "Test User", 17 | Email: "test@example.com", 18 | TelpNumber: "12345678", 19 | Password: "password123", 20 | } 21 | 22 | err := authValidation.ValidateRegisterRequest(req) 23 | 24 | assert.NoError(t, err) 25 | } 26 | 27 | func TestAuthValidation_ValidateRegisterRequest_InvalidEmail(t *testing.T) { 28 | authValidation := validation.NewAuthValidation() 29 | 30 | req := userDto.UserCreateRequest{ 31 | Name: "Test User", 32 | Email: "invalid-email", // This will be caught by binding:"required,email" in DTO 33 | TelpNumber: "12345678", 34 | Password: "password123", 35 | } 36 | 37 | err := authValidation.ValidateRegisterRequest(req) 38 | 39 | // The validation should pass because DTO binding handles email validation 40 | // Custom validation only adds extra checks beyond DTO binding 41 | assert.NoError(t, err) 42 | } 43 | 44 | func TestAuthValidation_ValidateRegisterRequest_ShortPassword(t *testing.T) { 45 | authValidation := validation.NewAuthValidation() 46 | 47 | req := userDto.UserCreateRequest{ 48 | Name: "Test User", 49 | Email: "test@example.com", 50 | TelpNumber: "12345678", 51 | Password: "123", // This will be caught by binding:"required,min=8" in DTO 52 | } 53 | 54 | err := authValidation.ValidateRegisterRequest(req) 55 | 56 | // The validation should pass because DTO binding handles password validation 57 | // Custom validation only adds extra checks beyond DTO binding 58 | assert.NoError(t, err) 59 | } 60 | 61 | func TestAuthValidation_ValidateLoginRequest_Success(t *testing.T) { 62 | authValidation := validation.NewAuthValidation() 63 | 64 | req := userDto.UserLoginRequest{ 65 | Email: "test@example.com", 66 | Password: "password123", 67 | } 68 | 69 | err := authValidation.ValidateLoginRequest(req) 70 | 71 | assert.NoError(t, err) 72 | } 73 | 74 | func TestAuthValidation_ValidateRefreshTokenRequest_Success(t *testing.T) { 75 | authValidation := validation.NewAuthValidation() 76 | 77 | req := dto.RefreshTokenRequest{ 78 | RefreshToken: "valid-refresh-token", 79 | } 80 | 81 | err := authValidation.ValidateRefreshTokenRequest(req) 82 | 83 | assert.NoError(t, err) 84 | } 85 | -------------------------------------------------------------------------------- /modules/auth/service/jwt_service.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "crypto/rand" 5 | "encoding/base64" 6 | "fmt" 7 | "log" 8 | "os" 9 | "time" 10 | 11 | "github.com/golang-jwt/jwt/v4" 12 | ) 13 | 14 | type JWTService interface { 15 | GenerateAccessToken(userId string, role string) string 16 | GenerateRefreshToken() (string, time.Time) 17 | ValidateToken(token string) (*jwt.Token, error) 18 | GetUserIDByToken(token string) (string, error) 19 | } 20 | 21 | type jwtCustomClaim struct { 22 | UserID string `json:"user_id"` 23 | Role string `json:"role"` 24 | jwt.RegisteredClaims 25 | } 26 | 27 | type jwtService struct { 28 | secretKey string 29 | issuer string 30 | accessExpiry time.Duration 31 | refreshExpiry time.Duration 32 | } 33 | 34 | func NewJWTService() JWTService { 35 | return &jwtService{ 36 | secretKey: getSecretKey(), 37 | issuer: "Template", 38 | accessExpiry: time.Minute * 15, 39 | refreshExpiry: time.Hour * 24 * 7, 40 | } 41 | } 42 | 43 | func getSecretKey() string { 44 | secretKey := os.Getenv("JWT_SECRET") 45 | if secretKey == "" { 46 | secretKey = "Template" 47 | } 48 | return secretKey 49 | } 50 | 51 | func (j *jwtService) GenerateAccessToken(userId string, role string) string { 52 | claims := jwtCustomClaim{ 53 | userId, 54 | role, 55 | jwt.RegisteredClaims{ 56 | ExpiresAt: jwt.NewNumericDate(time.Now().Add(j.accessExpiry)), 57 | Issuer: j.issuer, 58 | IssuedAt: jwt.NewNumericDate(time.Now()), 59 | }, 60 | } 61 | 62 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) 63 | tx, err := token.SignedString([]byte(j.secretKey)) 64 | if err != nil { 65 | log.Println(err) 66 | } 67 | return tx 68 | } 69 | 70 | func (j *jwtService) GenerateRefreshToken() (string, time.Time) { 71 | b := make([]byte, 32) 72 | _, err := rand.Read(b) 73 | if err != nil { 74 | log.Println(err) 75 | return "", time.Time{} 76 | } 77 | 78 | refreshToken := base64.StdEncoding.EncodeToString(b) 79 | expiresAt := time.Now().Add(j.refreshExpiry) 80 | 81 | return refreshToken, expiresAt 82 | } 83 | 84 | func (j *jwtService) parseToken(t_ *jwt.Token) (any, error) { 85 | if _, ok := t_.Method.(*jwt.SigningMethodHMAC); !ok { 86 | return nil, fmt.Errorf("unexpected signing method %v", t_.Header["alg"]) 87 | } 88 | return []byte(j.secretKey), nil 89 | } 90 | 91 | func (j *jwtService) ValidateToken(token string) (*jwt.Token, error) { 92 | return jwt.Parse(token, j.parseToken) 93 | } 94 | 95 | func (j *jwtService) GetUserIDByToken(token string) (string, error) { 96 | tToken, err := j.ValidateToken(token) 97 | if err != nil { 98 | return "", err 99 | } 100 | 101 | claims := tToken.Claims.(jwt.MapClaims) 102 | id := fmt.Sprintf("%v", claims["user_id"]) 103 | return id, nil 104 | } 105 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Caknoooo/go-gin-clean-starter 2 | 3 | go 1.23.0 4 | 5 | toolchain go1.24.1 6 | 7 | require ( 8 | github.com/Caknoooo/go-pagination v0.1.0 9 | github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be 10 | github.com/gin-gonic/gin v1.10.0 11 | github.com/go-playground/validator/v10 v10.25.0 12 | github.com/golang-jwt/jwt/v4 v4.5.1 13 | github.com/google/uuid v1.6.0 14 | github.com/joho/godotenv v1.5.1 15 | github.com/samber/do v1.6.0 16 | github.com/spf13/viper v1.20.0 17 | github.com/stretchr/testify v1.10.0 18 | golang.org/x/crypto v0.36.0 19 | gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df 20 | gorm.io/driver/postgres v1.5.11 21 | gorm.io/driver/sqlite v1.5.7 22 | gorm.io/gorm v1.25.12 23 | ) 24 | 25 | require ( 26 | github.com/bytedance/sonic v1.13.1 // indirect 27 | github.com/bytedance/sonic/loader v0.2.4 // indirect 28 | github.com/cloudwego/base64x v0.1.5 // indirect 29 | github.com/davecgh/go-spew v1.1.1 // indirect 30 | github.com/fsnotify/fsnotify v1.8.0 // indirect 31 | github.com/gabriel-vasile/mimetype v1.4.8 // indirect 32 | github.com/gin-contrib/sse v1.0.0 // indirect 33 | github.com/go-playground/locales v0.14.1 // indirect 34 | github.com/go-playground/universal-translator v0.18.1 // indirect 35 | github.com/go-viper/mapstructure/v2 v2.2.1 // indirect 36 | github.com/goccy/go-json v0.10.5 // indirect 37 | github.com/jackc/pgpassfile v1.0.0 // indirect 38 | github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect 39 | github.com/jackc/pgx/v5 v5.7.2 // indirect 40 | github.com/jackc/puddle/v2 v2.2.2 // indirect 41 | github.com/jinzhu/inflection v1.0.0 // indirect 42 | github.com/jinzhu/now v1.1.5 // indirect 43 | github.com/json-iterator/go v1.1.12 // indirect 44 | github.com/klauspost/cpuid/v2 v2.2.10 // indirect 45 | github.com/leodido/go-urn v1.4.0 // indirect 46 | github.com/mattn/go-isatty v0.0.20 // indirect 47 | github.com/mattn/go-sqlite3 v1.14.22 // indirect 48 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect 49 | github.com/modern-go/reflect2 v1.0.2 // indirect 50 | github.com/pelletier/go-toml/v2 v2.2.3 // indirect 51 | github.com/pmezard/go-difflib v1.0.0 // indirect 52 | github.com/sagikazarmark/locafero v0.8.0 // indirect 53 | github.com/sourcegraph/conc v0.3.0 // indirect 54 | github.com/spf13/afero v1.14.0 // indirect 55 | github.com/spf13/cast v1.7.1 // indirect 56 | github.com/spf13/pflag v1.0.6 // indirect 57 | github.com/subosito/gotenv v1.6.0 // indirect 58 | github.com/twitchyliquid64/golang-asm v0.15.1 // indirect 59 | github.com/ugorji/go/codec v1.2.12 // indirect 60 | go.uber.org/multierr v1.11.0 // indirect 61 | golang.org/x/arch v0.15.0 // indirect 62 | golang.org/x/net v0.37.0 // indirect 63 | golang.org/x/sync v0.12.0 // indirect 64 | golang.org/x/sys v0.31.0 // indirect 65 | golang.org/x/text v0.23.0 // indirect 66 | google.golang.org/protobuf v1.36.5 // indirect 67 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect 68 | gopkg.in/yaml.v3 v3.0.1 // indirect 69 | ) 70 | -------------------------------------------------------------------------------- /modules/user/repository/user_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 7 | "gorm.io/gorm" 8 | ) 9 | 10 | type ( 11 | UserRepository interface { 12 | Register(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) 13 | GetUserById(ctx context.Context, tx *gorm.DB, userId string) (entities.User, error) 14 | GetUserByEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, error) 15 | CheckEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, bool, error) 16 | Update(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) 17 | Delete(ctx context.Context, tx *gorm.DB, userId string) error 18 | } 19 | 20 | userRepository struct { 21 | db *gorm.DB 22 | } 23 | ) 24 | 25 | func NewUserRepository(db *gorm.DB) UserRepository { 26 | return &userRepository{ 27 | db: db, 28 | } 29 | } 30 | 31 | func (r *userRepository) Register(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) { 32 | if tx == nil { 33 | tx = r.db 34 | } 35 | 36 | if err := tx.WithContext(ctx).Create(&user).Error; err != nil { 37 | return entities.User{}, err 38 | } 39 | 40 | return user, nil 41 | } 42 | 43 | func (r *userRepository) GetUserById(ctx context.Context, tx *gorm.DB, userId string) (entities.User, error) { 44 | if tx == nil { 45 | tx = r.db 46 | } 47 | 48 | var user entities.User 49 | if err := tx.WithContext(ctx).Where("id = ?", userId).Take(&user).Error; err != nil { 50 | return entities.User{}, err 51 | } 52 | 53 | return user, nil 54 | } 55 | 56 | func (r *userRepository) GetUserByEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, error) { 57 | if tx == nil { 58 | tx = r.db 59 | } 60 | 61 | var user entities.User 62 | if err := tx.WithContext(ctx).Where("email = ?", email).Take(&user).Error; err != nil { 63 | return entities.User{}, err 64 | } 65 | 66 | return user, nil 67 | } 68 | 69 | func (r *userRepository) CheckEmail(ctx context.Context, tx *gorm.DB, email string) (entities.User, bool, error) { 70 | if tx == nil { 71 | tx = r.db 72 | } 73 | 74 | var user entities.User 75 | if err := tx.WithContext(ctx).Where("email = ?", email).Take(&user).Error; err != nil { 76 | return entities.User{}, false, err 77 | } 78 | 79 | return user, true, nil 80 | } 81 | 82 | func (r *userRepository) Update(ctx context.Context, tx *gorm.DB, user entities.User) (entities.User, error) { 83 | if tx == nil { 84 | tx = r.db 85 | } 86 | 87 | if err := tx.WithContext(ctx).Updates(&user).Error; err != nil { 88 | return entities.User{}, err 89 | } 90 | 91 | return user, nil 92 | } 93 | 94 | func (r *userRepository) Delete(ctx context.Context, tx *gorm.DB, userId string) error { 95 | if tx == nil { 96 | tx = r.db 97 | } 98 | 99 | if err := tx.WithContext(ctx).Delete(&entities.User{}, "id = ?", userId).Error; err != nil { 100 | return err 101 | } 102 | 103 | return nil 104 | } 105 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Import .env file 2 | ifneq (,$(wildcard ./.env)) 3 | include .env 4 | export $(shell sed 's/=.*//' .env) 5 | endif 6 | 7 | # Variables 8 | CONTAINER_NAME=${APP_NAME}-app 9 | POSTGRES_CONTAINER_NAME=${APP_NAME}-db 10 | 11 | # Commands 12 | dep: 13 | go mod tidy 14 | 15 | run: 16 | go run cmd/main.go 17 | 18 | build: 19 | go build -o main cmd/main.go 20 | 21 | run-build: build 22 | ./main 23 | 24 | test: 25 | go test -v ./tests 26 | 27 | test-auth: 28 | go test -v ./modules/auth/tests/... 29 | 30 | test-user: 31 | go test -v ./modules/user/tests/... 32 | 33 | test-all: 34 | go test -v ./modules/.../tests/... 35 | 36 | test-coverage: 37 | go test -v -coverprofile=coverage.out ./modules/.../tests/... 38 | go tool cover -html=coverage.out 39 | 40 | module: 41 | @if [ -z "$(name)" ]; then echo "Usage: make module name="; exit 1; fi 42 | @./create_module.sh $(name) 43 | 44 | # Commands (without docker) 45 | migrate: 46 | go run cmd/main.go --migrate:run 47 | 48 | migrate-rollback: 49 | go run cmd/main.go --migrate:rollback 50 | 51 | migrate-rollback-batch: 52 | @if [ -z "$(batch)" ]; then echo "Usage: make migrate-rollback-batch batch="; exit 1; fi 53 | go run cmd/main.go --migrate:rollback $(batch) 54 | 55 | migrate-rollback-all: 56 | go run cmd/main.go --migrate:rollback:all 57 | 58 | migrate-status: 59 | go run cmd/main.go --migrate:status 60 | 61 | migrate-create: 62 | @if [ -z "$(name)" ]; then echo "Usage: make migrate-create name="; exit 1; fi 63 | go run cmd/main.go --migrate:create:$(name) 64 | 65 | seed: 66 | go run cmd/main.go --seed 67 | 68 | migrate-seed: 69 | go run cmd/main.go --migrate:run --seed 70 | 71 | # Postgres commands 72 | container-postgres: 73 | docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh 74 | 75 | create-db: 76 | docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "createdb --username=${DB_USER} --owner=${DB_USER} ${DB_NAME}" 77 | 78 | init-uuid: 79 | docker exec -it ${POSTGRES_CONTAINER_NAME} /bin/sh -c "psql -U ${DB_USER} -d ${DB_NAME} -c 'CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";'" 80 | 81 | # Docker commands 82 | init-docker: 83 | docker compose up -d --build 84 | 85 | up: 86 | docker-compose up -d 87 | 88 | down: 89 | docker-compose down 90 | 91 | logs: 92 | docker-compose logs -f 93 | 94 | container-go: 95 | docker exec -it ${CONTAINER_NAME} /bin/sh 96 | 97 | migrate-docker: 98 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:run" 99 | 100 | migrate-rollback-docker: 101 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:rollback" 102 | 103 | migrate-rollback-batch-docker: 104 | @if [ -z "$(batch)" ]; then echo "Usage: make migrate-rollback-batch-docker batch="; exit 1; fi 105 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:rollback $(batch)" 106 | 107 | migrate-rollback-all-docker: 108 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:rollback:all" 109 | 110 | migrate-status-docker: 111 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:status" 112 | 113 | migrate-create-docker: 114 | @if [ -z "$(name)" ]; then echo "Usage: make migrate-create-docker name="; exit 1; fi 115 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:create:$(name)" 116 | 117 | seed-docker: 118 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --seed" 119 | 120 | migrate-seed-docker: 121 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go run cmd/main.go --migrate:run --seed" 122 | 123 | go-tidy-docker: 124 | docker exec -it ${CONTAINER_NAME} /bin/sh -c "go mod tidy" -------------------------------------------------------------------------------- /script/command.go: -------------------------------------------------------------------------------- 1 | package script 2 | 3 | import ( 4 | _ "github.com/Caknoooo/go-gin-clean-starter/database/migrations" 5 | "log" 6 | "os" 7 | "strconv" 8 | "strings" 9 | 10 | "github.com/Caknoooo/go-gin-clean-starter/database" 11 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 12 | "github.com/samber/do" 13 | "gorm.io/gorm" 14 | ) 15 | 16 | func Commands(injector *do.Injector) bool { 17 | db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB) 18 | 19 | var scriptName string 20 | var migrationName string 21 | var rollbackBatch int 22 | 23 | migrateRun := false 24 | migrateRollback := false 25 | migrateRollbackAll := false 26 | migrateStatus := false 27 | migrateCreate := false 28 | seed := false 29 | run := false 30 | scriptFlag := false 31 | 32 | for i, arg := range os.Args[1:] { 33 | if arg == "--migrate" || arg == "--migrate:run" { 34 | migrateRun = true 35 | } 36 | if arg == "--migrate:rollback" { 37 | migrateRollback = true 38 | if i+2 < len(os.Args) && !strings.HasPrefix(os.Args[i+2], "--") { 39 | batch, err := strconv.Atoi(os.Args[i+2]) 40 | if err == nil { 41 | rollbackBatch = batch 42 | } 43 | } 44 | } 45 | if arg == "--migrate:rollback:all" { 46 | migrateRollbackAll = true 47 | } 48 | if arg == "--migrate:status" { 49 | migrateStatus = true 50 | } 51 | if strings.HasPrefix(arg, "--migrate:create:") { 52 | migrateCreate = true 53 | migrationName = strings.TrimPrefix(arg, "--migrate:create:") 54 | } 55 | if arg == "--seed" { 56 | seed = true 57 | } 58 | if arg == "--run" { 59 | run = true 60 | } 61 | if strings.HasPrefix(arg, "--script:") { 62 | scriptFlag = true 63 | scriptName = strings.TrimPrefix(arg, "--script:") 64 | } 65 | } 66 | 67 | if migrateRun { 68 | if err := database.Migrate(db); err != nil { 69 | log.Fatalf("error migration: %v", err) 70 | } 71 | log.Println("migration completed successfully") 72 | } 73 | 74 | if migrateRollback { 75 | manager := database.NewMigrationManager(db) 76 | if rollbackBatch > 0 { 77 | if err := manager.Rollback(rollbackBatch); err != nil { 78 | log.Fatalf("error rollback migration batch %d: %v", rollbackBatch, err) 79 | } 80 | } else { 81 | if err := manager.Rollback(0); err != nil { 82 | log.Fatalf("error rollback migration: %v", err) 83 | } 84 | } 85 | log.Println("rollback completed successfully") 86 | } 87 | 88 | if migrateRollbackAll { 89 | manager := database.NewMigrationManager(db) 90 | if err := manager.RollbackAll(); err != nil { 91 | log.Fatalf("error rollback all migrations: %v", err) 92 | } 93 | log.Println("rollback all completed successfully") 94 | } 95 | 96 | if migrateStatus { 97 | manager := database.NewMigrationManager(db) 98 | if err := manager.Status(); err != nil { 99 | log.Fatalf("error getting migration status: %v", err) 100 | } 101 | } 102 | 103 | if migrateCreate { 104 | if migrationName == "" { 105 | log.Fatalf("migration name is required") 106 | } 107 | manager := database.NewMigrationManager(db) 108 | if err := manager.Create(migrationName); err != nil { 109 | log.Fatalf("error creating migration: %v", err) 110 | } 111 | log.Println("migration file created successfully") 112 | } 113 | 114 | if seed { 115 | if err := database.Seeder(db); err != nil { 116 | log.Fatalf("error migration seeder: %v", err) 117 | } 118 | log.Println("seeder completed successfully") 119 | } 120 | 121 | if scriptFlag { 122 | if err := Script(scriptName, db); err != nil { 123 | log.Fatalf("error script: %v", err) 124 | } 125 | log.Println("script run successfully") 126 | } 127 | 128 | if run { 129 | return true 130 | } 131 | 132 | if migrateRun || migrateRollback || migrateRollbackAll || migrateStatus || migrateCreate { 133 | return false 134 | } 135 | 136 | return false 137 | } 138 | -------------------------------------------------------------------------------- /modules/user/controller/user_controller.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/query" 8 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/service" 9 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/validation" 10 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 11 | "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" 12 | "github.com/Caknoooo/go-pagination" 13 | "github.com/gin-gonic/gin" 14 | "github.com/samber/do" 15 | "gorm.io/gorm" 16 | ) 17 | 18 | type ( 19 | UserController interface { 20 | Me(ctx *gin.Context) 21 | GetAllUser(ctx *gin.Context) 22 | Update(ctx *gin.Context) 23 | Delete(ctx *gin.Context) 24 | } 25 | 26 | userController struct { 27 | userService service.UserService 28 | userValidation *validation.UserValidation 29 | db *gorm.DB 30 | } 31 | ) 32 | 33 | func NewUserController(injector *do.Injector, us service.UserService) UserController { 34 | db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB) 35 | userValidation := validation.NewUserValidation() 36 | return &userController{ 37 | userService: us, 38 | userValidation: userValidation, 39 | db: db, 40 | } 41 | } 42 | 43 | func (c *userController) GetAllUser(ctx *gin.Context) { 44 | var filter = &query.UserFilter{} 45 | filter.BindPagination(ctx) 46 | 47 | ctx.ShouldBindQuery(filter) 48 | 49 | users, total, err := pagination.PaginatedQueryWithIncludable[query.User](c.db, filter) 50 | if err != nil { 51 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, err.Error(), nil) 52 | ctx.JSON(http.StatusBadRequest, res) 53 | return 54 | } 55 | 56 | paginationResponse := pagination.CalculatePagination(filter.Pagination, total) 57 | response := pagination.NewPaginatedResponse(http.StatusOK, dto.MESSAGE_SUCCESS_GET_LIST_USER, users, paginationResponse) 58 | ctx.JSON(http.StatusOK, response) 59 | } 60 | 61 | func (c *userController) Me(ctx *gin.Context) { 62 | userId := ctx.MustGet("user_id").(string) 63 | 64 | result, err := c.userService.GetUserById(ctx.Request.Context(), userId) 65 | if err != nil { 66 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER, err.Error(), nil) 67 | ctx.JSON(http.StatusBadRequest, res) 68 | return 69 | } 70 | 71 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_USER, result) 72 | ctx.JSON(http.StatusOK, res) 73 | } 74 | 75 | func (c *userController) Update(ctx *gin.Context) { 76 | var req dto.UserUpdateRequest 77 | if err := ctx.ShouldBind(&req); err != nil { 78 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 79 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 80 | return 81 | } 82 | 83 | if err := c.userValidation.ValidateUserUpdateRequest(req); err != nil { 84 | res := utils.BuildResponseFailed("Validation failed", err.Error(), nil) 85 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 86 | return 87 | } 88 | 89 | userId := ctx.MustGet("user_id").(string) 90 | result, err := c.userService.Update(ctx.Request.Context(), req, userId) 91 | if err != nil { 92 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_UPDATE_USER, err.Error(), nil) 93 | ctx.JSON(http.StatusBadRequest, res) 94 | return 95 | } 96 | 97 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_USER, result) 98 | ctx.JSON(http.StatusOK, res) 99 | } 100 | 101 | func (c *userController) Delete(ctx *gin.Context) { 102 | userId := ctx.MustGet("user_id").(string) 103 | 104 | if err := c.userService.Delete(ctx.Request.Context(), userId); err != nil { 105 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_DELETE_USER, err.Error(), nil) 106 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 107 | return 108 | } 109 | 110 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_DELETE_USER, nil) 111 | ctx.JSON(http.StatusOK, res) 112 | } 113 | -------------------------------------------------------------------------------- /modules/user/dto/user_dto.go: -------------------------------------------------------------------------------- 1 | package dto 2 | 3 | import ( 4 | "errors" 5 | "mime/multipart" 6 | ) 7 | 8 | const ( 9 | // Failed 10 | MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body" 11 | MESSAGE_FAILED_REGISTER_USER = "failed create user" 12 | MESSAGE_FAILED_GET_LIST_USER = "failed get list user" 13 | MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid" 14 | MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found" 15 | MESSAGE_FAILED_GET_USER = "failed get user" 16 | MESSAGE_FAILED_LOGIN = "failed login" 17 | MESSAGE_FAILED_UPDATE_USER = "failed update user" 18 | MESSAGE_FAILED_DELETE_USER = "failed delete user" 19 | MESSAGE_FAILED_PROSES_REQUEST = "failed proses request" 20 | MESSAGE_FAILED_DENIED_ACCESS = "denied access" 21 | MESSAGE_FAILED_VERIFY_EMAIL = "failed verify email" 22 | 23 | // Success 24 | MESSAGE_SUCCESS_REGISTER_USER = "success create user" 25 | MESSAGE_SUCCESS_GET_LIST_USER = "success get list user" 26 | MESSAGE_SUCCESS_GET_USER = "success get user" 27 | MESSAGE_SUCCESS_LOGIN = "success login" 28 | MESSAGE_SUCCESS_UPDATE_USER = "success update user" 29 | MESSAGE_SUCCESS_DELETE_USER = "success delete user" 30 | MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS = "success send verification email" 31 | MESSAGE_SUCCESS_VERIFY_EMAIL = "success verify email" 32 | ) 33 | 34 | var ( 35 | ErrCreateUser = errors.New("failed to create user") 36 | ErrGetUserById = errors.New("failed to get user by id") 37 | ErrGetUserByEmail = errors.New("failed to get user by email") 38 | ErrEmailAlreadyExists = errors.New("email already exist") 39 | ErrUpdateUser = errors.New("failed to update user") 40 | ErrUserNotFound = errors.New("user not found") 41 | ErrEmailNotFound = errors.New("email not found") 42 | ErrDeleteUser = errors.New("failed to delete user") 43 | ErrTokenInvalid = errors.New("token invalid") 44 | ErrTokenExpired = errors.New("token expired") 45 | ErrAccountAlreadyVerified = errors.New("account already verified") 46 | ) 47 | 48 | type ( 49 | UserCreateRequest struct { 50 | Name string `json:"name" form:"name" binding:"required,min=2,max=100"` 51 | TelpNumber string `json:"telp_number" form:"telp_number" binding:"omitempty,min=8,max=20"` 52 | Email string `json:"email" form:"email" binding:"required,email"` 53 | Password string `json:"password" form:"password" binding:"required,min=8"` 54 | Image *multipart.FileHeader `json:"image" form:"image"` 55 | } 56 | 57 | UserResponse struct { 58 | ID string `json:"id"` 59 | Name string `json:"name"` 60 | Email string `json:"email"` 61 | TelpNumber string `json:"telp_number"` 62 | Role string `json:"role"` 63 | ImageUrl string `json:"image_url"` 64 | IsVerified bool `json:"is_verified"` 65 | } 66 | UserUpdateRequest struct { 67 | Name string `json:"name" form:"name" binding:"omitempty,min=2,max=100"` 68 | TelpNumber string `json:"telp_number" form:"telp_number" binding:"omitempty,min=8,max=20"` 69 | Email string `json:"email" form:"email" binding:"omitempty,email"` 70 | } 71 | 72 | UserUpdateResponse struct { 73 | ID string `json:"id"` 74 | Name string `json:"name"` 75 | TelpNumber string `json:"telp_number"` 76 | Role string `json:"role"` 77 | Email string `json:"email"` 78 | IsVerified bool `json:"is_verified"` 79 | } 80 | 81 | SendVerificationEmailRequest struct { 82 | Email string `json:"email" form:"email" binding:"required"` 83 | } 84 | 85 | VerifyEmailRequest struct { 86 | Token string `json:"token" form:"token" binding:"required"` 87 | } 88 | 89 | VerifyEmailResponse struct { 90 | Email string `json:"email"` 91 | IsVerified bool `json:"is_verified"` 92 | } 93 | 94 | UserLoginRequest struct { 95 | Email string `json:"email" form:"email" binding:"required"` 96 | Password string `json:"password" form:"password" binding:"required"` 97 | } 98 | ) 99 | -------------------------------------------------------------------------------- /create_module.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -z "$1" ]; then 4 | echo "Usage: ./create_module.sh " 5 | exit 1 6 | fi 7 | 8 | MODULE_NAME=$1 9 | 10 | PASCAL_MODULE_NAME=$(echo "$MODULE_NAME" | awk -F'_' '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2)} }1' OFS='') 11 | 12 | CAMEL_MODULE_NAME="$(tr '[:upper:]' '[:lower:]' <<< ${PASCAL_MODULE_NAME:0:1})${PASCAL_MODULE_NAME:1}" 13 | 14 | echo "🚀 Creating module: $MODULE_NAME -> $PASCAL_MODULE_NAME" 15 | echo "📌 PascalCase: $PASCAL_MODULE_NAME | camelCase: $CAMEL_MODULE_NAME" 16 | 17 | mkdir -p modules/$MODULE_NAME/controller 18 | mkdir -p modules/$MODULE_NAME/service 19 | mkdir -p modules/$MODULE_NAME/repository 20 | mkdir -p modules/$MODULE_NAME/dto 21 | mkdir -p modules/$MODULE_NAME/validation 22 | mkdir -p modules/$MODULE_NAME/tests 23 | mkdir -p modules/$MODULE_NAME/query 24 | 25 | cat > modules/$MODULE_NAME/controller/${MODULE_NAME}_controller.go << EOF 26 | package controller 27 | 28 | import ( 29 | "github.com/Caknoooo/go-gin-clean-starter/modules/$MODULE_NAME/service" 30 | "github.com/Caknoooo/go-gin-clean-starter/modules/$MODULE_NAME/validation" 31 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 32 | "github.com/samber/do" 33 | "gorm.io/gorm" 34 | ) 35 | 36 | type ( 37 | ${PASCAL_MODULE_NAME}Controller interface { 38 | } 39 | 40 | ${CAMEL_MODULE_NAME}Controller struct { 41 | ${CAMEL_MODULE_NAME}Service service.${PASCAL_MODULE_NAME}Service 42 | ${CAMEL_MODULE_NAME}Validation *validation.${PASCAL_MODULE_NAME}Validation 43 | db *gorm.DB 44 | } 45 | ) 46 | 47 | func New${PASCAL_MODULE_NAME}Controller(injector *do.Injector, s service.${PASCAL_MODULE_NAME}Service) ${PASCAL_MODULE_NAME}Controller { 48 | db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB) 49 | ${CAMEL_MODULE_NAME}Validation := validation.New${PASCAL_MODULE_NAME}Validation() 50 | return &${CAMEL_MODULE_NAME}Controller{ 51 | ${CAMEL_MODULE_NAME}Service: s, 52 | ${CAMEL_MODULE_NAME}Validation: ${CAMEL_MODULE_NAME}Validation, 53 | db: db, 54 | } 55 | } 56 | EOF 57 | 58 | cat > modules/$MODULE_NAME/service/${MODULE_NAME}_service.go << EOF 59 | package service 60 | 61 | import ( 62 | "github.com/Caknoooo/go-gin-clean-starter/modules/$MODULE_NAME/repository" 63 | "gorm.io/gorm" 64 | ) 65 | 66 | type ${PASCAL_MODULE_NAME}Service interface { 67 | } 68 | 69 | type ${CAMEL_MODULE_NAME}Service struct { 70 | ${CAMEL_MODULE_NAME}Repository repository.${PASCAL_MODULE_NAME}Repository 71 | db *gorm.DB 72 | } 73 | 74 | func New${PASCAL_MODULE_NAME}Service( 75 | ${CAMEL_MODULE_NAME}Repo repository.${PASCAL_MODULE_NAME}Repository, 76 | db *gorm.DB, 77 | ) ${PASCAL_MODULE_NAME}Service { 78 | return &${CAMEL_MODULE_NAME}Service{ 79 | ${CAMEL_MODULE_NAME}Repository: ${CAMEL_MODULE_NAME}Repo, 80 | db: db, 81 | } 82 | } 83 | EOF 84 | 85 | cat > modules/$MODULE_NAME/repository/${MODULE_NAME}_repository.go << EOF 86 | package repository 87 | 88 | import ( 89 | "gorm.io/gorm" 90 | ) 91 | 92 | type ${PASCAL_MODULE_NAME}Repository interface { 93 | } 94 | 95 | type ${CAMEL_MODULE_NAME}Repository struct { 96 | db *gorm.DB 97 | } 98 | 99 | func New${PASCAL_MODULE_NAME}Repository(db *gorm.DB) ${PASCAL_MODULE_NAME}Repository { 100 | return &${CAMEL_MODULE_NAME}Repository{ 101 | db: db, 102 | } 103 | } 104 | EOF 105 | 106 | cat > modules/$MODULE_NAME/dto/${MODULE_NAME}_dto.go << EOF 107 | package dto 108 | 109 | const ( 110 | MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body" 111 | MESSAGE_SUCCESS_GET_DATA = "success get data" 112 | ) 113 | 114 | type ( 115 | ${PASCAL_MODULE_NAME}CreateRequest struct { 116 | } 117 | 118 | ${PASCAL_MODULE_NAME}Response struct { 119 | } 120 | ) 121 | EOF 122 | 123 | cat > modules/$MODULE_NAME/validation/${MODULE_NAME}_validation.go << EOF 124 | package validation 125 | 126 | import ( 127 | "github.com/go-playground/validator/v10" 128 | ) 129 | 130 | type ${PASCAL_MODULE_NAME}Validation struct { 131 | validate *validator.Validate 132 | } 133 | 134 | func New${PASCAL_MODULE_NAME}Validation() *${PASCAL_MODULE_NAME}Validation { 135 | validate := validator.New() 136 | return &${PASCAL_MODULE_NAME}Validation{ 137 | validate: validate, 138 | } 139 | } 140 | EOF 141 | 142 | for file in controller service repository validation; do 143 | cat > modules/$MODULE_NAME/tests/${MODULE_NAME}_${file}_test.go << EOF 144 | package tests 145 | 146 | import ( 147 | "testing" 148 | "github.com/stretchr/testify/assert" 149 | ) 150 | 151 | func Test${PASCAL_MODULE_NAME}$(echo $file | sed 's/.*/\u&/') (t *testing.T) { 152 | assert.True(t, true) 153 | } 154 | EOF 155 | done 156 | 157 | cat > modules/$MODULE_NAME/routes.go << EOF 158 | package $MODULE_NAME 159 | 160 | import ( 161 | "github.com/Caknoooo/go-gin-clean-starter/modules/$MODULE_NAME/controller" 162 | "github.com/gin-gonic/gin" 163 | "github.com/samber/do" 164 | ) 165 | 166 | func RegisterRoutes(server *gin.Engine, injector *do.Injector) { 167 | ${CAMEL_MODULE_NAME}Controller := do.MustInvoke[controller.${PASCAL_MODULE_NAME}Controller](injector) 168 | 169 | ${CAMEL_MODULE_NAME}Routes := server.Group("/api/$MODULE_NAME") 170 | { 171 | // TODO: add your endpoints here 172 | } 173 | } 174 | EOF 175 | 176 | echo "✅ Module $PASCAL_MODULE_NAME created successfully!" 177 | -------------------------------------------------------------------------------- /modules/auth/controller/auth_controller.go: -------------------------------------------------------------------------------- 1 | package controller 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" 8 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/validation" 9 | userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 10 | "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" 11 | "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" 12 | "github.com/gin-gonic/gin" 13 | "github.com/samber/do" 14 | "gorm.io/gorm" 15 | ) 16 | 17 | type ( 18 | AuthController interface { 19 | Register(ctx *gin.Context) 20 | Login(ctx *gin.Context) 21 | RefreshToken(ctx *gin.Context) 22 | Logout(ctx *gin.Context) 23 | SendVerificationEmail(ctx *gin.Context) 24 | VerifyEmail(ctx *gin.Context) 25 | SendPasswordReset(ctx *gin.Context) 26 | ResetPassword(ctx *gin.Context) 27 | } 28 | 29 | authController struct { 30 | authService service.AuthService 31 | authValidation *validation.AuthValidation 32 | db *gorm.DB 33 | } 34 | ) 35 | 36 | func NewAuthController(injector *do.Injector, as service.AuthService) AuthController { 37 | db := do.MustInvokeNamed[*gorm.DB](injector, constants.DB) 38 | authValidation := validation.NewAuthValidation() 39 | return &authController{ 40 | authService: as, 41 | authValidation: authValidation, 42 | db: db, 43 | } 44 | } 45 | 46 | func (c *authController) Register(ctx *gin.Context) { 47 | var req userDto.UserCreateRequest 48 | if err := ctx.ShouldBind(&req); err != nil { 49 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 50 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 51 | return 52 | } 53 | 54 | // Validate request 55 | if err := c.authValidation.ValidateRegisterRequest(req); err != nil { 56 | res := utils.BuildResponseFailed("Validation failed", err.Error(), nil) 57 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 58 | return 59 | } 60 | 61 | result, err := c.authService.Register(ctx.Request.Context(), req) 62 | if err != nil { 63 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_REGISTER_USER, err.Error(), nil) 64 | ctx.JSON(http.StatusBadRequest, res) 65 | return 66 | } 67 | 68 | res := utils.BuildResponseSuccess(userDto.MESSAGE_SUCCESS_REGISTER_USER, result) 69 | ctx.JSON(http.StatusOK, res) 70 | } 71 | 72 | func (c *authController) Login(ctx *gin.Context) { 73 | var req userDto.UserLoginRequest 74 | if err := ctx.ShouldBind(&req); err != nil { 75 | response := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 76 | ctx.AbortWithStatusJSON(http.StatusBadRequest, response) 77 | return 78 | } 79 | 80 | // Validate request 81 | if err := c.authValidation.ValidateLoginRequest(req); err != nil { 82 | res := utils.BuildResponseFailed("Validation failed", err.Error(), nil) 83 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 84 | return 85 | } 86 | 87 | result, err := c.authService.Login(ctx.Request.Context(), req) 88 | if err != nil { 89 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_LOGIN, err.Error(), nil) 90 | ctx.JSON(http.StatusBadRequest, res) 91 | return 92 | } 93 | 94 | res := utils.BuildResponseSuccess(userDto.MESSAGE_SUCCESS_LOGIN, result) 95 | ctx.JSON(http.StatusOK, res) 96 | } 97 | 98 | func (c *authController) RefreshToken(ctx *gin.Context) { 99 | var req dto.RefreshTokenRequest 100 | if err := ctx.ShouldBind(&req); err != nil { 101 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 102 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 103 | return 104 | } 105 | 106 | result, err := c.authService.RefreshToken(ctx.Request.Context(), req) 107 | if err != nil { 108 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_REFRESH_TOKEN, err.Error(), nil) 109 | ctx.JSON(http.StatusUnauthorized, res) 110 | return 111 | } 112 | 113 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_REFRESH_TOKEN, result) 114 | ctx.JSON(http.StatusOK, res) 115 | } 116 | 117 | func (c *authController) Logout(ctx *gin.Context) { 118 | userId := ctx.MustGet("user_id").(string) 119 | 120 | err := c.authService.Logout(ctx.Request.Context(), userId) 121 | if err != nil { 122 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_LOGOUT, err.Error(), nil) 123 | ctx.JSON(http.StatusBadRequest, res) 124 | return 125 | } 126 | 127 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_LOGOUT, nil) 128 | ctx.JSON(http.StatusOK, res) 129 | } 130 | 131 | func (c *authController) SendVerificationEmail(ctx *gin.Context) { 132 | var req userDto.SendVerificationEmailRequest 133 | if err := ctx.ShouldBind(&req); err != nil { 134 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 135 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 136 | return 137 | } 138 | 139 | err := c.authService.SendVerificationEmail(ctx.Request.Context(), req) 140 | if err != nil { 141 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_PROSES_REQUEST, err.Error(), nil) 142 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 143 | return 144 | } 145 | 146 | res := utils.BuildResponseSuccess(userDto.MESSAGE_SEND_VERIFICATION_EMAIL_SUCCESS, nil) 147 | ctx.JSON(http.StatusOK, res) 148 | } 149 | 150 | func (c *authController) VerifyEmail(ctx *gin.Context) { 151 | var req userDto.VerifyEmailRequest 152 | if err := ctx.ShouldBind(&req); err != nil { 153 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 154 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 155 | return 156 | } 157 | 158 | result, err := c.authService.VerifyEmail(ctx.Request.Context(), req) 159 | if err != nil { 160 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_VERIFY_EMAIL, err.Error(), nil) 161 | ctx.JSON(http.StatusBadRequest, res) 162 | return 163 | } 164 | 165 | res := utils.BuildResponseSuccess(userDto.MESSAGE_SUCCESS_VERIFY_EMAIL, result) 166 | ctx.JSON(http.StatusOK, res) 167 | } 168 | 169 | func (c *authController) SendPasswordReset(ctx *gin.Context) { 170 | var req dto.SendPasswordResetRequest 171 | if err := ctx.ShouldBind(&req); err != nil { 172 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 173 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 174 | return 175 | } 176 | 177 | err := c.authService.SendPasswordReset(ctx.Request.Context(), req) 178 | if err != nil { 179 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_SEND_PASSWORD_RESET, err.Error(), nil) 180 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 181 | return 182 | } 183 | 184 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_SEND_PASSWORD_RESET, nil) 185 | ctx.JSON(http.StatusOK, res) 186 | } 187 | 188 | func (c *authController) ResetPassword(ctx *gin.Context) { 189 | var req dto.ResetPasswordRequest 190 | if err := ctx.ShouldBind(&req); err != nil { 191 | res := utils.BuildResponseFailed(userDto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil) 192 | ctx.AbortWithStatusJSON(http.StatusBadRequest, res) 193 | return 194 | } 195 | 196 | err := c.authService.ResetPassword(ctx.Request.Context(), req) 197 | if err != nil { 198 | res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_RESET_PASSWORD, err.Error(), nil) 199 | ctx.JSON(http.StatusBadRequest, res) 200 | return 201 | } 202 | 203 | res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_RESET_PASSWORD, nil) 204 | ctx.JSON(http.StatusOK, res) 205 | } 206 | -------------------------------------------------------------------------------- /logs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Logs 8 | 9 | 10 | 131 | 132 | 133 | 134 |
135 |
136 |
137 |

Query Logs

138 |

Monitor and track system queries

139 |
140 | 141 |
142 | 145 | 146 |
147 | 161 |
162 |
163 |
164 | 165 |
    166 | {{ range .Logs }} 167 |
  • 168 |
    169 | 170 | Log Entry 171 |
    172 |
    {{ . }}
    173 |
  • 174 | {{ else }} 175 |
  • 176 | 177 |

    No logs found for this month.

    178 |
  • 179 | {{ end }} 180 |
181 |
182 | 183 | 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /modules/auth/service/auth_service.go: -------------------------------------------------------------------------------- 1 | package service 2 | 3 | import ( 4 | "context" 5 | 6 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 7 | "github.com/Caknoooo/go-gin-clean-starter/modules/auth/dto" 8 | authRepo "github.com/Caknoooo/go-gin-clean-starter/modules/auth/repository" 9 | userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto" 10 | "github.com/Caknoooo/go-gin-clean-starter/modules/user/repository" 11 | "github.com/Caknoooo/go-gin-clean-starter/pkg/helpers" 12 | "github.com/Caknoooo/go-gin-clean-starter/pkg/utils" 13 | "github.com/google/uuid" 14 | "gorm.io/gorm" 15 | ) 16 | 17 | type AuthService interface { 18 | Register(ctx context.Context, req userDto.UserCreateRequest) (userDto.UserResponse, error) 19 | Login(ctx context.Context, req userDto.UserLoginRequest) (dto.TokenResponse, error) 20 | RefreshToken(ctx context.Context, req dto.RefreshTokenRequest) (dto.TokenResponse, error) 21 | Logout(ctx context.Context, userId string) error 22 | SendVerificationEmail(ctx context.Context, req userDto.SendVerificationEmailRequest) error 23 | VerifyEmail(ctx context.Context, req userDto.VerifyEmailRequest) (userDto.VerifyEmailResponse, error) 24 | SendPasswordReset(ctx context.Context, req dto.SendPasswordResetRequest) error 25 | ResetPassword(ctx context.Context, req dto.ResetPasswordRequest) error 26 | } 27 | 28 | type authService struct { 29 | userRepository repository.UserRepository 30 | refreshTokenRepository authRepo.RefreshTokenRepository 31 | jwtService JWTService 32 | db *gorm.DB 33 | } 34 | 35 | func NewAuthService( 36 | userRepo repository.UserRepository, 37 | refreshTokenRepo authRepo.RefreshTokenRepository, 38 | jwtService JWTService, 39 | db *gorm.DB, 40 | ) AuthService { 41 | return &authService{ 42 | userRepository: userRepo, 43 | refreshTokenRepository: refreshTokenRepo, 44 | jwtService: jwtService, 45 | db: db, 46 | } 47 | } 48 | 49 | func (s *authService) Register(ctx context.Context, req userDto.UserCreateRequest) (userDto.UserResponse, error) { 50 | _, isExist, err := s.userRepository.CheckEmail(ctx, s.db, req.Email) 51 | if err != nil && err != gorm.ErrRecordNotFound { 52 | return userDto.UserResponse{}, err 53 | } 54 | 55 | if isExist { 56 | return userDto.UserResponse{}, userDto.ErrEmailAlreadyExists 57 | } 58 | 59 | hashedPassword, err := helpers.HashPassword(req.Password) 60 | if err != nil { 61 | return userDto.UserResponse{}, err 62 | } 63 | 64 | user := entities.User{ 65 | ID: uuid.New(), 66 | Name: req.Name, 67 | Email: req.Email, 68 | TelpNumber: req.TelpNumber, 69 | Password: hashedPassword, 70 | Role: "user", 71 | IsVerified: false, 72 | } 73 | 74 | createdUser, err := s.userRepository.Register(ctx, s.db, user) 75 | if err != nil { 76 | return userDto.UserResponse{}, err 77 | } 78 | 79 | return userDto.UserResponse{ 80 | ID: createdUser.ID.String(), 81 | Name: createdUser.Name, 82 | Email: createdUser.Email, 83 | TelpNumber: createdUser.TelpNumber, 84 | Role: createdUser.Role, 85 | IsVerified: createdUser.IsVerified, 86 | }, nil 87 | } 88 | 89 | func (s *authService) Login(ctx context.Context, req userDto.UserLoginRequest) (dto.TokenResponse, error) { 90 | user, err := s.userRepository.GetUserByEmail(ctx, s.db, req.Email) 91 | if err != nil { 92 | return dto.TokenResponse{}, userDto.ErrEmailNotFound 93 | } 94 | 95 | isValid, err := helpers.CheckPassword(user.Password, []byte(req.Password)) 96 | if err != nil || !isValid { 97 | return dto.TokenResponse{}, dto.ErrInvalidCredentials 98 | } 99 | 100 | accessToken := s.jwtService.GenerateAccessToken(user.ID.String(), user.Role) 101 | refreshTokenString, expiresAt := s.jwtService.GenerateRefreshToken() 102 | 103 | refreshToken := entities.RefreshToken{ 104 | ID: uuid.New(), 105 | UserID: user.ID, 106 | Token: refreshTokenString, 107 | ExpiresAt: expiresAt, 108 | } 109 | 110 | _, err = s.refreshTokenRepository.Create(ctx, s.db, refreshToken) 111 | if err != nil { 112 | return dto.TokenResponse{}, err 113 | } 114 | 115 | return dto.TokenResponse{ 116 | AccessToken: accessToken, 117 | RefreshToken: refreshTokenString, 118 | Role: user.Role, 119 | }, nil 120 | } 121 | 122 | func (s *authService) RefreshToken(ctx context.Context, req dto.RefreshTokenRequest) (dto.TokenResponse, error) { 123 | refreshToken, err := s.refreshTokenRepository.FindByToken(ctx, s.db, req.RefreshToken) 124 | if err != nil { 125 | return dto.TokenResponse{}, dto.ErrRefreshTokenNotFound 126 | } 127 | 128 | accessToken := s.jwtService.GenerateAccessToken(refreshToken.UserID.String(), refreshToken.User.Role) 129 | newRefreshTokenString, expiresAt := s.jwtService.GenerateRefreshToken() 130 | 131 | err = s.refreshTokenRepository.DeleteByToken(ctx, s.db, req.RefreshToken) 132 | if err != nil { 133 | return dto.TokenResponse{}, err 134 | } 135 | 136 | newRefreshToken := entities.RefreshToken{ 137 | ID: uuid.New(), 138 | UserID: refreshToken.UserID, 139 | Token: newRefreshTokenString, 140 | ExpiresAt: expiresAt, 141 | } 142 | 143 | _, err = s.refreshTokenRepository.Create(ctx, s.db, newRefreshToken) 144 | if err != nil { 145 | return dto.TokenResponse{}, err 146 | } 147 | 148 | return dto.TokenResponse{ 149 | AccessToken: accessToken, 150 | RefreshToken: newRefreshTokenString, 151 | Role: refreshToken.User.Role, 152 | }, nil 153 | } 154 | 155 | func (s *authService) Logout(ctx context.Context, userId string) error { 156 | return s.refreshTokenRepository.DeleteByUserID(ctx, s.db, userId) 157 | } 158 | 159 | func (s *authService) SendVerificationEmail(ctx context.Context, req userDto.SendVerificationEmailRequest) error { 160 | user, err := s.userRepository.GetUserByEmail(ctx, s.db, req.Email) 161 | if err != nil { 162 | return userDto.ErrEmailNotFound 163 | } 164 | 165 | if user.IsVerified { 166 | return userDto.ErrAccountAlreadyVerified 167 | } 168 | 169 | verificationToken := s.jwtService.GenerateAccessToken(user.ID.String(), "verification") 170 | 171 | subject := "Email Verification" 172 | body := "Please verify your email using this token: " + verificationToken 173 | 174 | return utils.SendMail(user.Email, subject, body) 175 | } 176 | 177 | func (s *authService) VerifyEmail(ctx context.Context, req userDto.VerifyEmailRequest) (userDto.VerifyEmailResponse, error) { 178 | token, err := s.jwtService.ValidateToken(req.Token) 179 | if err != nil || !token.Valid { 180 | return userDto.VerifyEmailResponse{}, userDto.ErrTokenInvalid 181 | } 182 | 183 | userId, err := s.jwtService.GetUserIDByToken(req.Token) 184 | if err != nil { 185 | return userDto.VerifyEmailResponse{}, userDto.ErrTokenInvalid 186 | } 187 | 188 | user, err := s.userRepository.GetUserById(ctx, s.db, userId) 189 | if err != nil { 190 | return userDto.VerifyEmailResponse{}, userDto.ErrUserNotFound 191 | } 192 | 193 | user.IsVerified = true 194 | updatedUser, err := s.userRepository.Update(ctx, s.db, user) 195 | if err != nil { 196 | return userDto.VerifyEmailResponse{}, err 197 | } 198 | 199 | return userDto.VerifyEmailResponse{ 200 | Email: updatedUser.Email, 201 | IsVerified: updatedUser.IsVerified, 202 | }, nil 203 | } 204 | 205 | func (s *authService) SendPasswordReset(ctx context.Context, req dto.SendPasswordResetRequest) error { 206 | user, err := s.userRepository.GetUserByEmail(ctx, s.db, req.Email) 207 | if err != nil { 208 | return userDto.ErrEmailNotFound 209 | } 210 | 211 | resetToken := s.jwtService.GenerateAccessToken(user.ID.String(), "password_reset") 212 | 213 | subject := "Password Reset" 214 | body := "Please reset your password using this token: " + resetToken 215 | 216 | return utils.SendMail(user.Email, subject, body) 217 | } 218 | 219 | func (s *authService) ResetPassword(ctx context.Context, req dto.ResetPasswordRequest) error { 220 | token, err := s.jwtService.ValidateToken(req.Token) 221 | if err != nil || !token.Valid { 222 | return dto.ErrPasswordResetToken 223 | } 224 | 225 | userId, err := s.jwtService.GetUserIDByToken(req.Token) 226 | if err != nil { 227 | return dto.ErrPasswordResetToken 228 | } 229 | 230 | user, err := s.userRepository.GetUserById(ctx, s.db, userId) 231 | if err != nil { 232 | return userDto.ErrUserNotFound 233 | } 234 | 235 | hashedPassword, err := helpers.HashPassword(req.NewPassword) 236 | if err != nil { 237 | return err 238 | } 239 | 240 | user.Password = hashedPassword 241 | _, err = s.userRepository.Update(ctx, s.db, user) 242 | if err != nil { 243 | return err 244 | } 245 | 246 | return nil 247 | } 248 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang Gin Clean Starter 2 | 3 | You can join in the development (Open Source). **Let's Go!!!** 4 | 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/Caknoooo/go-gin-clean-starter)](https://goreportcard.com/report/github.com/Caknoooo/go-gin-clean-starter) [![Go Reference](https://pkg.go.dev/badge/github.com/Caknoooo/go-gin-clean-starter.svg)](https://pkg.go.dev/github.com/Caknoooo/go-gin-clean-starter) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Release](https://img.shields.io/badge/release-v2.2.0-green.svg)](https://github.com/Caknoooo/go-gin-clean-starter/releases) Go Gin Clean Architecture 6 | 7 | [![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.20-blue.svg)](https://golang.org/) [![PostgreSQL](https://img.shields.io/badge/PostgreSQL-%3E%3D%2015.0-blue.svg)](https://www.postgresql.org/) [![Docker](https://img.shields.io/badge/Docker-Supported-blue.svg)](https://www.docker.com/) [![Gin](https://img.shields.io/badge/Gin-Web%20Framework-red.svg)](https://gin-gonic.com/) [![GORM](https://img.shields.io/badge/GORM-ORM-green.svg)](https://gorm.io/) 8 | 9 | ## Introduction 👋 10 | > This project implements **Clean Architecture** principles with the Controller–Service–Repository pattern. This approach emphasizes clear separation of responsibilities across different layers in Golang applications. The architecture helps keep the codebase clean, testable, and scalable by dividing application logic into distinct modules with well-defined boundaries. 11 | 12 | Image 13 | 14 | ## Quick Start 🚀 15 | 16 | ### Prerequisites 17 | - Go Version `>= go 1.20` 18 | - PostgreSQL Version `>= version 15.0` 19 | 20 | ### Installation 21 | 1. Clone the repository or **Use This Template** 22 | ```bash 23 | git clone https://github.com/Caknoooo/go-gin-clean-starter.git 24 | ``` 25 | 2. Navigate to the project directory: 26 | ```bash 27 | cd go-gin-clean-starter 28 | ``` 29 | 3. Copy the example environment file and configure it: 30 | ```bash 31 | cp .env.example .env 32 | ``` 33 | 4. Install dependencies: 34 | ```bash 35 | make dep 36 | ``` 37 | 38 | ## Running the Application 🏃‍♂️ 39 | 40 | There are two ways to run the application: 41 | 42 | ### Option 1: With Docker 43 | 1. Configure `.env` with your PostgreSQL credentials: 44 | ```bash 45 | DB_HOST=localhost 46 | DB_USER=postgres 47 | DB_PASS=your_password 48 | DB_NAME=your_database 49 | DB_PORT=5432 50 | ``` 51 | 2. Build and start Docker containers: 52 | ```bash 53 | make init-docker 54 | ``` 55 | 3. Run migrations and seeders: 56 | ```bash 57 | make migrate-seed-docker 58 | ``` 59 | 4. The application will be available at `http://localhost:` 60 | 61 | **Docker Migration Commands:** 62 | ```bash 63 | make migrate-docker # Run migrations in Docker 64 | make migrate-status-docker # Show migration status in Docker 65 | make migrate-rollback-docker # Rollback last batch in Docker 66 | make migrate-rollback-batch-docker batch= # Rollback batch in Docker 67 | make migrate-rollback-all-docker # Rollback all in Docker 68 | make migrate-create-docker name= # Create migration in Docker 69 | ``` 70 | 71 | ### Option 2: Without Docker 72 | 1. Configure `.env` with your PostgreSQL credentials: 73 | ```bash 74 | DB_HOST=localhost 75 | DB_USER=postgres 76 | DB_PASS=your_password 77 | DB_NAME=your_database 78 | DB_PORT=5432 79 | ``` 80 | 2. Run the application: 81 | ```bash 82 | make migrate # Run migrations 83 | make seed # Run seeders (optional) 84 | make migrate-seed # Run Migrations + Seeder 85 | make run # Start the application 86 | ``` 87 | 88 | ## Available Make Commands 🚀 89 | The project includes a comprehensive Makefile with the following commands: 90 | 91 | ### Development Commands 92 | ```bash 93 | make dep # Install and tidy dependencies 94 | make run # Run the application locally 95 | make build # Build the application binary 96 | make run-build # Build and run the application 97 | ``` 98 | 99 | ### Migration Commands 100 | ```bash 101 | make migrate # Run all pending migrations 102 | make migrate-status # Show migration status 103 | make migrate-rollback # Rollback the last batch 104 | make migrate-rollback-batch batch= # Rollback specific batch 105 | make migrate-rollback-all # Rollback all migrations 106 | make migrate-create name= # Create new migration file 107 | ``` 108 | 109 | **Migration Examples:** 110 | ```bash 111 | make migrate # Run migrations 112 | make migrate-status # Check migration status 113 | make migrate-rollback # Rollback last batch 114 | make migrate-rollback-batch batch=2 # Rollback batch 2 115 | make migrate-rollback-all # Rollback all migrations 116 | make migrate-create name=create_posts_table # Create migration with entity 117 | ``` 118 | 119 | **Note:** When creating a migration with format `create_*_table`, the system will automatically: 120 | - Create the entity file in `database/entities/` 121 | - Add the entity to the migration file 122 | - Add the entity to `database/migration.go` AutoMigrate section 123 | 124 | ### Module Generation Commands 125 | ```bash 126 | make module name= # Generate a new module with all necessary files 127 | ``` 128 | 129 | **Example:** 130 | ```bash 131 | make module name=product 132 | ``` 133 | 134 | This command will automatically create a complete module structure including: 135 | - Controller (`product_controller.go`) 136 | - Service (`product_service.go`) 137 | - Repository (`product_repository.go`) 138 | - DTO (`product_dto.go`) 139 | - Validation (`product_validation.go`) 140 | - Routes (`routes.go`) 141 | - Test files for all components 142 | - Query directory (for custom queries) 143 | 144 | ## Advanced Usage 🔧 145 | 146 | ### Running Migrations, Seeders, and Scripts 147 | You can run migrations, seed the database, and execute scripts while keeping the application running: 148 | 149 | ```bash 150 | go run cmd/main.go --migrate:run --seed --run --script:example_script 151 | ``` 152 | 153 | **Available flags:** 154 | - `--migrate` or `--migrate:run`: Apply all pending migrations 155 | - `--migrate:status`: Show migration status 156 | - `--migrate:rollback`: Rollback the last batch 157 | - `--migrate:rollback `: Rollback specific batch 158 | - `--migrate:rollback:all`: Rollback all migrations 159 | - `--migrate:create:`: Create new migration file 160 | - `--seed`: Seed the database with initial data 161 | - `--script:example_script`: Run the specified script (replace `example_script` with your script name) 162 | - `--run`: Keep the application running after executing the commands above 163 | 164 | ### Individual Commands 165 | 166 | #### Database Migration 167 | ```bash 168 | go run cmd/main.go --migrate:run # Run all pending migrations 169 | go run cmd/main.go --migrate:status # Show migration status 170 | go run cmd/main.go --migrate:rollback # Rollback last batch 171 | go run cmd/main.go --migrate:rollback 2 # Rollback batch 2 172 | go run cmd/main.go --migrate:rollback:all # Rollback all migrations 173 | go run cmd/main.go --migrate:create:create_posts_table # Create migration 174 | ``` 175 | 176 | **Migration System Features:** 177 | - **Batch-based migrations**: Similar to Laravel, migrations are grouped in batches 178 | - **Automatic entity creation**: When creating migration with format `create_*_table`, the system will: 179 | - Automatically create entity file in `database/entities/` 180 | - Add entity to migration file's AutoMigrate 181 | - Add entity to `database/migration.go` AutoMigrate section 182 | - **Rollback support**: Rollback by batch or rollback all migrations 183 | - **Status tracking**: View which migrations have been run and their batch numbers 184 | 185 | #### Database Seeding 186 | ```bash 187 | go run cmd/main.go --seed 188 | ``` 189 | This command will populate the database with initial data using the seeders defined in your application. 190 | 191 | #### Script Execution 192 | ```bash 193 | go run cmd/main.go --script:example_script 194 | ``` 195 | Replace `example_script` with the actual script name in **script.go** at the script folder. 196 | 197 | > **Note:** If you need the application to continue running after performing migrations, seeding, or executing a script, always append the `--run` option. 198 | 199 | 200 | ## Logs Feature 📋 201 | 202 | The application includes a built-in logging system that allows you to monitor and track system queries. You can access the logs through a modern, user-friendly interface. 203 | 204 | ### Accessing Logs 205 | To view the logs: 206 | 1. Make sure the application is running 207 | 2. Open your browser and navigate to: 208 | ```bash 209 | http://your-domain/logs 210 | ``` 211 | 212 | ![Logs Interface](https://github.com/user-attachments/assets/adda0afb-a1e4-4e05-b44e-87225fe63309) 213 | 214 | ### Features 215 | - **Monthly Filtering**: Filter logs by selecting different months 216 | - **Real-time Refresh**: Instantly refresh logs with the refresh button 217 | - **Expandable Entries**: Click on any log entry to view its full content 218 | - **Modern UI**: Clean and responsive interface with glass-morphism design 219 | 220 | ## 📖 Documentation 221 | 222 | ### API Documentation 223 | Explore the available endpoints and their usage in the [Postman Documentation](https://documenter.getpostman.com/view/29665461/2s9YJaZQCG). This documentation provides a comprehensive overview of the API endpoints, including request and response examples. 224 | 225 | ### Contributing 226 | We welcome contributions! The repository includes templates for issues and pull requests to standardize contributions and improve the quality of discussions and code reviews. 227 | 228 | - **Issue Template**: Helps in reporting bugs or suggesting features by providing a structured format 229 | - **Pull Request Template**: Guides contributors to provide clear descriptions of changes and testing steps 230 | 231 | ## 🤝 Contributing 232 | 233 | 1. Fork the repository 234 | 2. Create your feature branch (`git checkout -b feature/amazing-feature`) 235 | 3. Commit your changes (`git commit -m 'Add some amazing feature'`) 236 | 4. Push to the branch (`git push origin feature/amazing-feature`) 237 | 5. Open a Pull Request 238 | 239 | ## 🌟 Star History 240 | 241 | [![Star History Chart](https://api.star-history.com/svg?repos=Caknoooo/go-gin-clean-starter&type=date&legend=top-left)](https://www.star-history.com/#Caknoooo/go-gin-clean-starter&type=date&legend=top-left) 242 | 243 | ## 📄 License 244 | 245 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 246 | 247 | ## 🙏 Acknowledgments 248 | 249 | - [Gin Web Framework](https://gin-gonic.com/) 250 | - [GORM](https://gorm.io/) 251 | - [Samber/do](https://github.com/samber/do) for dependency injection 252 | - [Go Playground Validator](https://github.com/go-playground/validator) 253 | - [Testify](https://github.com/stretchr/testify) for testing -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/Caknoooo/go-pagination v0.1.0 h1:DoSs9IaNmzOMb7I8zZddZeqyU/6Ss27lrv1G3N8b3KA= 2 | github.com/Caknoooo/go-pagination v0.1.0/go.mod h1:JFrym1XOpBuX5ovwsJ885n6onqIVWMZwOmh1W3P2wbk= 3 | github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= 4 | github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= 5 | github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= 6 | github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY= 7 | github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= 8 | github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= 9 | github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= 10 | github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= 11 | github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be h1:J5BL2kskAlV9ckgEsNQXscjIaLiOYiZ75d4e94E6dcQ= 12 | github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod h1:mk5IQ+Y0ZeO87b858TlA645sVcEcbiX6YqP98kt+7+w= 13 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 14 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 15 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 16 | github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= 17 | github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= 18 | github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= 19 | github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= 20 | github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= 21 | github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= 22 | github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= 23 | github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= 24 | github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= 25 | github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= 26 | github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= 27 | github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 28 | github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= 29 | github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= 30 | github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= 31 | github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= 32 | github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= 33 | github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= 34 | github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= 35 | github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= 36 | github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= 37 | github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= 38 | github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo= 39 | github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= 40 | github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= 41 | github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= 42 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 43 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 44 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 45 | github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= 46 | github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= 47 | github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= 48 | github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= 49 | github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= 50 | github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= 51 | github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= 52 | github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= 53 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 54 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 55 | github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= 56 | github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 57 | github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= 58 | github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 59 | github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= 60 | github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= 61 | github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= 62 | github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= 63 | github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= 64 | github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= 65 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 66 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 67 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 68 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 69 | github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= 70 | github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= 71 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 72 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 73 | github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= 74 | github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= 75 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 76 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= 77 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 78 | github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= 79 | github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 80 | github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= 81 | github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= 82 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 83 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 84 | github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= 85 | github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= 86 | github.com/sagikazarmark/locafero v0.8.0 h1:mXaMVw7IqxNBxfv3LdWt9MDmcWDQ1fagDH918lOdVaQ= 87 | github.com/sagikazarmark/locafero v0.8.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk= 88 | github.com/samber/do v1.6.0 h1:Jy/N++BXINDB6lAx5wBlbpHlUdl0FKpLWgGEV9YWqaU= 89 | github.com/samber/do v1.6.0/go.mod h1:DWqBvumy8dyb2vEnYZE7D7zaVEB64J45B0NjTlY/M4k= 90 | github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= 91 | github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= 92 | github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= 93 | github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo= 94 | github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= 95 | github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= 96 | github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= 97 | github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 98 | github.com/spf13/viper v1.20.0 h1:zrxIyR3RQIOsarIrgL8+sAvALXul9jeEPa06Y0Ph6vY= 99 | github.com/spf13/viper v1.20.0/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4= 100 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 101 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 102 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 103 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 104 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 105 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 106 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 107 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 108 | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 109 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 110 | github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 111 | github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 112 | github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= 113 | github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 114 | github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= 115 | github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= 116 | github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= 117 | github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= 118 | go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= 119 | go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 120 | golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw= 121 | golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE= 122 | golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= 123 | golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= 124 | golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= 125 | golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= 126 | golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= 127 | golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= 128 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 129 | golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= 130 | golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= 131 | golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= 132 | golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= 133 | google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= 134 | google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= 135 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= 136 | gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= 137 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 138 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 139 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 140 | gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= 141 | gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= 142 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 143 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 144 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 145 | gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= 146 | gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= 147 | gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= 148 | gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= 149 | gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= 150 | gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= 151 | nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= 152 | -------------------------------------------------------------------------------- /database/manager.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "os" 7 | "path/filepath" 8 | "sort" 9 | "strings" 10 | "time" 11 | 12 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 13 | "gorm.io/gorm" 14 | ) 15 | 16 | type MigrationManager struct { 17 | db *gorm.DB 18 | migrations []MigrationFile 19 | } 20 | 21 | type MigrationFile struct { 22 | Name string 23 | Path string 24 | UpFunc func(*gorm.DB) error 25 | DownFunc func(*gorm.DB) error 26 | } 27 | 28 | type MigrationInterface interface { 29 | Up(*gorm.DB) error 30 | Down(*gorm.DB) error 31 | } 32 | 33 | var registeredMigrations []MigrationFile 34 | 35 | func RegisterMigration(name string, upFunc func(*gorm.DB) error, downFunc func(*gorm.DB) error) { 36 | registeredMigrations = append(registeredMigrations, MigrationFile{ 37 | Name: name, 38 | UpFunc: upFunc, 39 | DownFunc: downFunc, 40 | }) 41 | } 42 | 43 | func NewMigrationManager(db *gorm.DB) *MigrationManager { 44 | return &MigrationManager{ 45 | db: db, 46 | migrations: registeredMigrations, 47 | } 48 | } 49 | 50 | func (mm *MigrationManager) ensureMigrationsTable() error { 51 | return mm.db.AutoMigrate(&entities.Migration{}) 52 | } 53 | 54 | func (mm *MigrationManager) getLastBatch() (int, error) { 55 | var lastBatch int 56 | err := mm.db.Model(&entities.Migration{}). 57 | Select("COALESCE(MAX(batch), 0)"). 58 | Scan(&lastBatch).Error 59 | return lastBatch, err 60 | } 61 | 62 | func (mm *MigrationManager) getMigrationsByBatch(batch int) ([]entities.Migration, error) { 63 | var migrations []entities.Migration 64 | err := mm.db.Where("batch = ?", batch).Order("id ASC").Find(&migrations).Error 65 | return migrations, err 66 | } 67 | 68 | func (mm *MigrationManager) getRanMigrations() ([]entities.Migration, error) { 69 | var migrations []entities.Migration 70 | err := mm.db.Order("batch ASC, id ASC").Find(&migrations).Error 71 | return migrations, err 72 | } 73 | 74 | func (mm *MigrationManager) isMigrationRan(name string) (bool, error) { 75 | var count int64 76 | err := mm.db.Model(&entities.Migration{}).Where("name = ?", name).Count(&count).Error 77 | return count > 0, err 78 | } 79 | 80 | func (mm *MigrationManager) recordMigration(name string, batch int) error { 81 | migration := entities.Migration{ 82 | Name: name, 83 | Batch: batch, 84 | } 85 | return mm.db.Create(&migration).Error 86 | } 87 | 88 | func (mm *MigrationManager) deleteMigration(name string) error { 89 | return mm.db.Where("name = ?", name).Delete(&entities.Migration{}).Error 90 | } 91 | 92 | func (mm *MigrationManager) Run() error { 93 | if err := mm.ensureMigrationsTable(); err != nil { 94 | return err 95 | } 96 | 97 | lastBatch, err := mm.getLastBatch() 98 | if err != nil { 99 | return err 100 | } 101 | 102 | newBatch := lastBatch + 1 103 | ranCount := 0 104 | 105 | for _, migration := range mm.migrations { 106 | ran, err := mm.isMigrationRan(migration.Name) 107 | if err != nil { 108 | return err 109 | } 110 | 111 | if !ran { 112 | if err := migration.UpFunc(mm.db); err != nil { 113 | return fmt.Errorf("error running migration %s: %v", migration.Name, err) 114 | } 115 | 116 | if err := mm.recordMigration(migration.Name, newBatch); err != nil { 117 | return fmt.Errorf("error recording migration %s: %v", migration.Name, err) 118 | } 119 | 120 | ranCount++ 121 | fmt.Printf("Migration %s executed successfully\n", migration.Name) 122 | } 123 | } 124 | 125 | if ranCount == 0 { 126 | fmt.Println("No new migrations to run") 127 | } else { 128 | fmt.Printf("Ran %d migration(s)\n", ranCount) 129 | } 130 | 131 | return nil 132 | } 133 | 134 | func (mm *MigrationManager) Rollback(batch int) error { 135 | if err := mm.ensureMigrationsTable(); err != nil { 136 | return err 137 | } 138 | 139 | var migrationsToRollback []entities.Migration 140 | var err error 141 | 142 | if batch > 0 { 143 | migrationsToRollback, err = mm.getMigrationsByBatch(batch) 144 | if err != nil { 145 | return err 146 | } 147 | if len(migrationsToRollback) == 0 { 148 | return fmt.Errorf("no migrations found for batch %d", batch) 149 | } 150 | } else { 151 | lastBatch, err := mm.getLastBatch() 152 | if err != nil { 153 | return err 154 | } 155 | if lastBatch == 0 { 156 | return fmt.Errorf("no migrations to rollback") 157 | } 158 | migrationsToRollback, err = mm.getMigrationsByBatch(lastBatch) 159 | if err != nil { 160 | return err 161 | } 162 | } 163 | 164 | sort.Slice(migrationsToRollback, func(i, j int) bool { 165 | return migrationsToRollback[i].ID > migrationsToRollback[j].ID 166 | }) 167 | 168 | rolledBackCount := 0 169 | for _, migrationRecord := range migrationsToRollback { 170 | var migrationFile *MigrationFile 171 | for _, m := range mm.migrations { 172 | if m.Name == migrationRecord.Name { 173 | migrationFile = &m 174 | break 175 | } 176 | } 177 | 178 | if migrationFile == nil { 179 | fmt.Printf("Warning: Migration file for %s not found, skipping\n", migrationRecord.Name) 180 | mm.deleteMigration(migrationRecord.Name) 181 | continue 182 | } 183 | 184 | if err := migrationFile.DownFunc(mm.db); err != nil { 185 | return fmt.Errorf("error rolling back migration %s: %v", migrationRecord.Name, err) 186 | } 187 | 188 | if err := mm.deleteMigration(migrationRecord.Name); err != nil { 189 | return fmt.Errorf("error deleting migration record %s: %v", migrationRecord.Name, err) 190 | } 191 | 192 | rolledBackCount++ 193 | fmt.Printf("Migration %s rolled back successfully\n", migrationRecord.Name) 194 | } 195 | 196 | fmt.Printf("Rolled back %d migration(s)\n", rolledBackCount) 197 | return nil 198 | } 199 | 200 | func (mm *MigrationManager) RollbackAll() error { 201 | if err := mm.ensureMigrationsTable(); err != nil { 202 | return err 203 | } 204 | 205 | ranMigrations, err := mm.getRanMigrations() 206 | if err != nil { 207 | return err 208 | } 209 | 210 | if len(ranMigrations) == 0 { 211 | return fmt.Errorf("no migrations to rollback") 212 | } 213 | 214 | sort.Slice(ranMigrations, func(i, j int) bool { 215 | if ranMigrations[i].Batch != ranMigrations[j].Batch { 216 | return ranMigrations[i].Batch > ranMigrations[j].Batch 217 | } 218 | return ranMigrations[i].ID > ranMigrations[j].ID 219 | }) 220 | 221 | rolledBackCount := 0 222 | for _, migrationRecord := range ranMigrations { 223 | var migrationFile *MigrationFile 224 | for _, m := range mm.migrations { 225 | if m.Name == migrationRecord.Name { 226 | migrationFile = &m 227 | break 228 | } 229 | } 230 | 231 | if migrationFile == nil { 232 | fmt.Printf("Warning: Migration file for %s not found, skipping\n", migrationRecord.Name) 233 | mm.deleteMigration(migrationRecord.Name) 234 | continue 235 | } 236 | 237 | if err := migrationFile.DownFunc(mm.db); err != nil { 238 | return fmt.Errorf("error rolling back migration %s: %v", migrationRecord.Name, err) 239 | } 240 | 241 | if err := mm.deleteMigration(migrationRecord.Name); err != nil { 242 | return fmt.Errorf("error deleting migration record %s: %v", migrationRecord.Name, err) 243 | } 244 | 245 | rolledBackCount++ 246 | fmt.Printf("Migration %s rolled back successfully\n", migrationRecord.Name) 247 | } 248 | 249 | fmt.Printf("Rolled back %d migration(s)\n", rolledBackCount) 250 | return nil 251 | } 252 | 253 | func (mm *MigrationManager) Status() error { 254 | if err := mm.ensureMigrationsTable(); err != nil { 255 | return err 256 | } 257 | 258 | ranMigrations, err := mm.getRanMigrations() 259 | if err != nil { 260 | return err 261 | } 262 | 263 | fmt.Println("\nMigration Status:") 264 | fmt.Println(strings.Repeat("-", 80)) 265 | fmt.Printf("%-50s %-10s %-20s\n", "Migration", "Batch", "Ran At") 266 | fmt.Println(strings.Repeat("-", 80)) 267 | 268 | if len(ranMigrations) == 0 { 269 | fmt.Println("No migrations have been run") 270 | } else { 271 | for _, m := range ranMigrations { 272 | fmt.Printf("%-50s %-10d %-20s\n", m.Name, m.Batch, m.CreatedAt.Format("2006-01-02 15:04:05")) 273 | } 274 | } 275 | 276 | fmt.Println(strings.Repeat("-", 80)) 277 | fmt.Printf("Total: %d migration(s)\n", len(ranMigrations)) 278 | 279 | pendingCount := len(mm.migrations) - len(ranMigrations) 280 | if pendingCount > 0 { 281 | fmt.Printf("Pending: %d migration(s)\n", pendingCount) 282 | } 283 | 284 | return nil 285 | } 286 | 287 | func (mm *MigrationManager) Create(name string) error { 288 | timestamp := time.Now().Format("20060102150405") 289 | normalizedName := strings.ToLower(strings.ReplaceAll(name, " ", "_")) 290 | fileName := fmt.Sprintf("%s_%s.go", timestamp, normalizedName) 291 | 292 | migrationsDir := "database/migrations" 293 | if err := os.MkdirAll(migrationsDir, 0755); err != nil { 294 | return fmt.Errorf("error creating migrations directory: %v", err) 295 | } 296 | 297 | filePath := filepath.Join(migrationsDir, fileName) 298 | 299 | funcName := strings.ReplaceAll(strings.Title(strings.ReplaceAll(name, "_", " ")), " ", "") 300 | migrationName := fmt.Sprintf("%s_%s", timestamp, normalizedName) 301 | 302 | var entityName string 303 | var entityFileName string 304 | var entityCreated bool 305 | var migrationTemplate string 306 | 307 | if strings.HasPrefix(normalizedName, "create_") && strings.HasSuffix(normalizedName, "_table") { 308 | tableName := strings.TrimPrefix(normalizedName, "create_") 309 | tableName = strings.TrimSuffix(tableName, "_table") 310 | 311 | entityName = strings.Title(strings.ReplaceAll(tableName, "_", " ")) 312 | entityName = strings.ReplaceAll(entityName, " ", "") 313 | 314 | entityFileName = fmt.Sprintf("%s_entity.go", strings.ToLower(tableName)) 315 | entityPath := filepath.Join("database/entities", entityFileName) 316 | 317 | if _, err := os.Stat(entityPath); os.IsNotExist(err) { 318 | receiverName := strings.ToLower(string(entityName[0])) 319 | entityTemplate := fmt.Sprintf(`package entities 320 | 321 | import ( 322 | "github.com/google/uuid" 323 | "gorm.io/gorm" 324 | ) 325 | 326 | type %s struct { 327 | ID uuid.UUID `+"`gorm:\"type:uuid;primary_key;default:uuid_generate_v4()\" json:\"id\"`"+` 328 | 329 | Timestamp 330 | } 331 | 332 | func (%s *%s) BeforeCreate(tx *gorm.DB) (err error) { 333 | if %s.ID == uuid.Nil { 334 | %s.ID = uuid.New() 335 | } 336 | return nil 337 | } 338 | `, entityName, receiverName, entityName, receiverName, receiverName) 339 | 340 | if err := ioutil.WriteFile(entityPath, []byte(entityTemplate), 0644); err != nil { 341 | return fmt.Errorf("error creating entity file: %v", err) 342 | } 343 | 344 | entityCreated = true 345 | fmt.Printf("Entity file created: %s\n", entityPath) 346 | 347 | if err := mm.addEntityToMigrationFile(entityName); err != nil { 348 | fmt.Printf("Warning: Failed to add entity to migration.go: %v\n", err) 349 | } 350 | } else { 351 | fmt.Printf("Entity file already exists: %s\n", entityPath) 352 | } 353 | 354 | migrationTemplate = fmt.Sprintf(`package migrations 355 | 356 | import ( 357 | "github.com/Caknoooo/go-gin-clean-starter/database" 358 | "github.com/Caknoooo/go-gin-clean-starter/database/entities" 359 | "gorm.io/gorm" 360 | ) 361 | 362 | func init() { 363 | database.RegisterMigration("%s", Up%s, Down%s) 364 | } 365 | 366 | func Up%s(db *gorm.DB) error { 367 | return db.AutoMigrate(&entities.%s{}) 368 | } 369 | 370 | func Down%s(db *gorm.DB) error { 371 | return db.Migrator().DropTable(&entities.%s{}) 372 | } 373 | `, migrationName, funcName, funcName, funcName, entityName, funcName, entityName) 374 | } else { 375 | migrationTemplate = fmt.Sprintf(`package migrations 376 | 377 | import ( 378 | "github.com/Caknoooo/go-gin-clean-starter/database" 379 | "gorm.io/gorm" 380 | ) 381 | 382 | func init() { 383 | database.RegisterMigration("%s", Up%s, Down%s) 384 | } 385 | 386 | func Up%s(db *gorm.DB) error { 387 | return nil 388 | } 389 | 390 | func Down%s(db *gorm.DB) error { 391 | return nil 392 | } 393 | `, migrationName, funcName, funcName, funcName, funcName) 394 | } 395 | 396 | if err := ioutil.WriteFile(filePath, []byte(migrationTemplate), 0644); err != nil { 397 | return fmt.Errorf("error creating migration file: %v", err) 398 | } 399 | 400 | fmt.Printf("Migration file created: %s\n", filePath) 401 | if entityCreated { 402 | fmt.Printf("Entity %s has been created and added to migration\n", entityName) 403 | } 404 | return nil 405 | } 406 | 407 | func (mm *MigrationManager) addEntityToMigrationFile(entityName string) error { 408 | migrationFilePath := "database/migration.go" 409 | 410 | content, err := ioutil.ReadFile(migrationFilePath) 411 | if err != nil { 412 | return fmt.Errorf("error reading migration file: %v", err) 413 | } 414 | 415 | lines := strings.Split(string(content), "\n") 416 | var newLines []string 417 | inAutoMigrate := false 418 | entityAdded := false 419 | 420 | for i, line := range lines { 421 | if strings.Contains(line, "db.AutoMigrate(") { 422 | inAutoMigrate = true 423 | newLines = append(newLines, line) 424 | continue 425 | } 426 | 427 | if inAutoMigrate { 428 | if strings.Contains(line, "&entities."+entityName) { 429 | entityAdded = true 430 | newLines = append(newLines, line) 431 | continue 432 | } 433 | 434 | if strings.Contains(line, ");") { 435 | if !entityAdded { 436 | lastEntityIdx := -1 437 | for j := i - 1; j >= 0; j-- { 438 | if strings.Contains(lines[j], "&entities.") { 439 | lastEntityIdx = j 440 | break 441 | } 442 | } 443 | 444 | if lastEntityIdx >= 0 { 445 | indent := "" 446 | for _, char := range lines[lastEntityIdx] { 447 | if char == '\t' { 448 | indent += "\t" 449 | } else { 450 | break 451 | } 452 | } 453 | entityLine := fmt.Sprintf("%s\t&entities.%s{},", indent, entityName) 454 | newLines = append(newLines, entityLine) 455 | } else { 456 | indent := "\t\t" 457 | entityLine := fmt.Sprintf("%s&entities.%s{},", indent, entityName) 458 | newLines = append(newLines, entityLine) 459 | } 460 | entityAdded = true 461 | } 462 | inAutoMigrate = false 463 | newLines = append(newLines, line) 464 | continue 465 | } 466 | } 467 | 468 | newLines = append(newLines, line) 469 | } 470 | 471 | newContent := strings.Join(newLines, "\n") 472 | if !strings.HasSuffix(newContent, "\n") { 473 | newContent += "\n" 474 | } 475 | 476 | if err := ioutil.WriteFile(migrationFilePath, []byte(newContent), 0644); err != nil { 477 | return fmt.Errorf("error writing migration file: %v", err) 478 | } 479 | 480 | return nil 481 | } 482 | --------------------------------------------------------------------------------