Skip to content

Commit 78aeda4

Browse files
authored
Merge pull request #91 from Caknoooo/feat/improvement-the-module-structure
feat: create mvp for new validation and test
2 parents a483511 + db48ca5 commit 78aeda4

File tree

20 files changed

+1217
-804
lines changed

20 files changed

+1217
-804
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ run-build: build
2424
test:
2525
go test -v ./tests
2626

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=<module_name>"; exit 1; fi
42+
@./create_module.sh $(name)
43+
2744
# Local commands (without docker)
2845
migrate-local:
2946
go run cmd/main.go --migrate

README.md

Lines changed: 165 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,29 @@ http://your-domain/logs
2727
![Logs Interface](https://github.com/user-attachments/assets/adda0afb-a1e4-4e05-b44e-87225fe63309)
2828

2929

30-
## Prerequisite 🏆
30+
## Quick Start 🚀
31+
32+
### Prerequisites
3133
- Go Version `>= go 1.20`
3234
- PostgreSQL Version `>= version 15.0`
3335

34-
## How To Use
36+
### Installation
3537
1. Clone the repository or **Use This Template**
36-
```bash
37-
git clone https://github.com/Caknoooo/go-gin-clean-starter.git
38-
```
38+
```bash
39+
git clone https://github.com/Caknoooo/go-gin-clean-starter.git
40+
```
3941
2. Navigate to the project directory:
40-
```bash
41-
cd go-gin-clean-starter
42-
```
42+
```bash
43+
cd go-gin-clean-starter
44+
```
4345
3. Copy the example environment file and configure it:
44-
```bash
45-
cp .env.example .env
46-
```
46+
```bash
47+
cp .env.example .env
48+
```
49+
4. Install dependencies:
50+
```bash
51+
make dep
52+
```
4753

4854
## Available Make Commands 🚀
4955
The project includes a comprehensive Makefile with the following commands:
@@ -53,7 +59,37 @@ The project includes a comprehensive Makefile with the following commands:
5359
make dep # Install and tidy dependencies
5460
make run # Run the application locally
5561
make build # Build the application binary
56-
make test # Run tests
62+
make run-build # Build and run the application
63+
```
64+
65+
### Module Generation Commands
66+
```bash
67+
make module name=<module_name> # Generate a new module with all necessary files
68+
```
69+
70+
**Example:**
71+
```bash
72+
make module name=product
73+
```
74+
75+
This command will automatically create a complete module structure including:
76+
- Controller (`product_controller.go`)
77+
- Service (`product_service.go`)
78+
- Repository (`product_repository.go`)
79+
- DTO (`product_dto.go`)
80+
- Validation (`product_validation.go`)
81+
- Routes (`routes.go`)
82+
- Test files for all components
83+
- Query directory (for custom queries)
84+
85+
The generated module follows Clean Architecture principles and is ready to use with proper dependency injection setup.
86+
87+
### Testing Commands
88+
```bash
89+
make test-auth # Run auth module tests only
90+
make test-user # Run user module tests only
91+
make test-all # Run tests for all modules
92+
make test-coverage # Run tests with coverage report
5793
```
5894

5995
### Local Database Commands (without Docker)
@@ -80,95 +116,147 @@ make container-go # Access Go container shell
80116
make container-postgres # Access PostgreSQL container
81117
```
82118

83-
There are 2 ways to run the application:
84-
### With Docker
119+
## Running the Application 🏃‍♂️
120+
121+
There are two ways to run the application:
122+
123+
### Option 1: With Docker
85124
1. Build and start Docker containers:
86-
```bash
87-
make init-docker
88-
```
89-
2. Run Initial UUID V4 for Auto Generate UUID:
90-
```bash
91-
make init-uuid
92-
```
93-
3. Run Migration and Seeder:
94-
```bash
95-
make migrate-seed
96-
```
97-
98-
### Without Docker
125+
```bash
126+
make init-docker
127+
```
128+
2. Initialize UUID V4 extension for auto-generated UUIDs:
129+
```bash
130+
make init-uuid
131+
```
132+
3. Run migrations and seeders:
133+
```bash
134+
make migrate-seed
135+
```
136+
4. The application will be available at `http://localhost:8080`
137+
138+
### Option 2: Without Docker
99139
1. Configure `.env` with your PostgreSQL credentials:
100-
```bash
101-
DB_HOST=localhost
102-
DB_USER=postgres
103-
DB_PASS=
104-
DB_NAME=
105-
DB_PORT=5432
106-
```
107-
2. Open the terminal and set up PostgreSQL:
108-
- If you haven't downloaded PostgreSQL, download it first.
109-
- Run:
110-
```bash
111-
psql -U postgres
112-
```
113-
- Create the database according to what you put in `.env`:
114-
```bash
115-
CREATE DATABASE your_database;
116-
\c your_database
117-
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
118-
\q
119-
```
120-
3. Install dependencies and run the application:
121-
```bash
122-
make dep # Install dependencies
123-
make migrate-local # Run migrations
124-
make seed-local # Run seeders (optional)
125-
make run # Start the application
126-
```
127-
128-
## Run Migrations, Seeder, and Script
129-
To run migrations, seed the database, and execute a script while keeping the application running, use the following command:
140+
```bash
141+
DB_HOST=localhost
142+
DB_USER=postgres
143+
DB_PASS=your_password
144+
DB_NAME=your_database
145+
DB_PORT=5432
146+
```
147+
2. Set up PostgreSQL:
148+
- Download and install PostgreSQL if you haven't already
149+
- Create a database:
150+
```bash
151+
psql -U postgres
152+
CREATE DATABASE your_database;
153+
\c your_database
154+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
155+
\q
156+
```
157+
3. Run the application:
158+
```bash
159+
make migrate-local # Run migrations
160+
make seed-local # Run seeders (optional)
161+
make run # Start the application
162+
```
163+
164+
## Advanced Usage 🔧
165+
166+
### Running Migrations, Seeders, and Scripts
167+
You can run migrations, seed the database, and execute scripts while keeping the application running:
130168

131169
```bash
132170
go run cmd/main.go --migrate --seed --run --script:example_script
133171
```
134172

135-
- ``--migrate`` will apply all pending migrations.
136-
- ``--seed`` will seed the database with initial data.
137-
- ``--script:example_script`` will run the specified script (replace ``example_script`` with your script name).
138-
- ``--run`` will ensure the application continues running after executing the commands above.
173+
**Available flags:**
174+
- `--migrate`: Apply all pending migrations
175+
- `--seed`: Seed the database with initial data
176+
- `--script:example_script`: Run the specified script (replace `example_script` with your script name)
177+
- `--run`: Keep the application running after executing the commands above
139178

140-
#### Migrate Database
141-
To migrate the database schema
179+
### Individual Commands
180+
181+
#### Database Migration
142182
```bash
143183
go run cmd/main.go --migrate
144184
```
145185
This command will apply all pending migrations to your PostgreSQL database specified in `.env`
146186

147-
#### Seeder Database
148-
To seed the database with initial data:
187+
#### Database Seeding
149188
```bash
150189
go run cmd/main.go --seed
151190
```
152191
This command will populate the database with initial data using the seeders defined in your application.
153192

154-
#### Script Run
155-
To run a specific script:
193+
#### Script Execution
156194
```bash
157195
go run cmd/main.go --script:example_script
158196
```
159-
Replace ``example_script`` with the actual script name in **script.go** at script folder
197+
Replace `example_script` with the actual script name in **script.go** at the script folder.
198+
199+
> **Note:** If you need the application to continue running after performing migrations, seeding, or executing a script, always append the `--run` option.
200+
201+
## What You Get 🎁
202+
203+
By using this template, you get a production-ready architecture with:
204+
205+
### 🏗️ Clean Architecture Implementation
206+
- **Controller-Service-Repository pattern** with clear separation of concerns
207+
- **Dependency injection** using samber/do
208+
- **Modular structure** for easy maintenance and testing
209+
- **Consistent code organization** across all modules
210+
211+
### 🚀 Pre-configured Features
212+
- **Authentication system** with JWT tokens
213+
- **User management** with email verification
214+
- **Password reset** functionality
215+
- **Database migrations** and seeders
216+
- **Comprehensive logging** system with web interface
217+
- **CORS middleware** for cross-origin requests
218+
- **Input validation** with go-playground/validator
219+
220+
### 📚 Documentation & Testing
221+
- **Postman collection** for API testing
222+
- **Comprehensive test suite** for all modules
223+
- **Code coverage** reporting
224+
- **Issue and PR templates** for better collaboration
225+
226+
### 🔧 Developer Experience
227+
- **Hot reload** with Air for development
228+
- **Docker support** for easy deployment
229+
- **Make commands** for common tasks
230+
- **Module generator** for rapid development
231+
- **Structured logging** with query tracking
232+
233+
## 📖 Documentation
234+
235+
### API Documentation
236+
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.
237+
238+
### Contributing
239+
We welcome contributions! The repository includes templates for issues and pull requests to standardize contributions and improve the quality of discussions and code reviews.
240+
241+
- **Issue Template**: Helps in reporting bugs or suggesting features by providing a structured format
242+
- **Pull Request Template**: Guides contributors to provide clear descriptions of changes and testing steps
160243

161-
If you need the application to continue running after performing migrations, seeding, or executing a script, always append the ``--run`` option.
244+
## 🤝 Contributing
162245

163-
## What did you get?
164-
By using this template, you get a ready-to-go architecture with pre-configured endpoints. The template provides a structured foundation for building your application using Golang with Clean Architecture principles.
246+
1. Fork the repository
247+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
248+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
249+
4. Push to the branch (`git push origin feature/amazing-feature`)
250+
5. Open a Pull Request
165251

166-
### Postman Documentation
167-
You can 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, making it easier to understand how to interact with the API.
252+
## 📄 License
168253

169-
### Issue / Pull Request Template
254+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
170255

171-
The repository includes templates for issues and pull requests to standardize contributions and improve the quality of discussions and code reviews.
256+
## 🙏 Acknowledgments
172257

173-
- **Issue Template**: Helps in reporting bugs or suggesting features by providing a structured format to capture all necessary information.
174-
- **Pull Request Template**: Guides contributors to provide a clear description of changes, related issues, and testing steps, ensuring smooth and efficient code reviews.
258+
- [Gin Web Framework](https://gin-gonic.com/)
259+
- [GORM](https://gorm.io/)
260+
- [Samber/do](https://github.com/samber/do) for dependency injection
261+
- [Go Playground Validator](https://github.com/go-playground/validator)
262+
- [Testify](https://github.com/stretchr/testify) for testing

config/database.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"fmt"
55
"os"
66

7-
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
87
"github.com/joho/godotenv"
98
"gorm.io/driver/postgres"
9+
"gorm.io/driver/sqlite"
1010
"gorm.io/gorm"
1111
)
1212

@@ -15,11 +15,9 @@ func RunExtension(db *gorm.DB) {
1515
}
1616

1717
func SetUpDatabaseConnection() *gorm.DB {
18-
if os.Getenv("APP_ENV") != constants.ENUM_RUN_PRODUCTION {
19-
err := godotenv.Load(".env")
20-
if err != nil {
21-
panic(err)
22-
}
18+
err := godotenv.Load(".env")
19+
if err != nil {
20+
panic(err)
2321
}
2422

2523
dbUser := os.Getenv("DB_USER")
@@ -45,6 +43,51 @@ func SetUpDatabaseConnection() *gorm.DB {
4543
return db
4644
}
4745

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+
4891
func CloseDatabaseConnection(db *gorm.DB) {
4992
dbSQL, err := db.DB()
5093
if err != nil {

0 commit comments

Comments
 (0)