Proyek ini adalah implementasi dari arsitektur bersih (clean architecture) menggunakan bahasa pemrograman Go. Proyek ini menggunakan beberapa library seperti Fiber untuk web framework, GORM untuk ORM, dan Viper untuk manajemen konfigurasi.
- Go Fiber: Web framework yang cepat dan ringan untuk Go
- GORM: ORM (Object Relational Mapping) untuk Go
- MySQL: Database utama untuk penyimpanan data
- Redis: In-memory data store untuk caching
- Viper: Library untuk manajemen konfigurasi
Proyek ini diorganisir dengan beberapa lapisan utama:
- cmd: Berisi entry point dari aplikasi.
- internal/config: Berisi konfigurasi aplikasi seperti database, logger, validator, dan framework web.
- internal/delivery: Berisi controller dan middleware untuk menangani HTTP request dan response.
- internal/entity: Berisi definisi dari entitas-entitas yang digunakan dalam aplikasi.
- internal/helper: Berisi helper functions yang digunakan di berbagai bagian aplikasi.
- internal/model: Berisi definisi dari model-model yang digunakan untuk request dan response.
- internal/repository: Berisi implementasi dari repository pattern untuk mengakses data.
- internal/usecase: Berisi implementasi dari use case yang merupakan logika bisnis dari aplikasi.
Pastikan Anda telah menginstal Go dan memiliki akses ke database MySQL.
-
Clone repository ini:
git clone https://github.com/abdisetiakawan/go-clean-arch.git cd go-clean-arch
-
Buat file config.json di root directory dengan konfigurasi berikut:
{ "app": { "name": "Go Clean Architecture" }, "web": { "port": 8080, "prefork": false }, "database": { "username": "your_db_username", "password": "your_db_password", "host": "localhost", "port": 3306, "name": "your_db_name", "pool": { "idle": 10, "max": 100, "lifetime": 300 } }, "log": { "level": 4 }, "redis": { "addr": "localhost:6379", "password": "", "db": 0 }, "credentials": { "accesssecret": "your_access_secret", "refreshsecret": "your_refresh_secret" } }
-
Jalankan migrasi database:
migrate -database "mysql://root:@tcp(localhost:3306)/your_db_name?charset=utf8mb4&parseTime=True&loc=Local" -path db/migrations up
-
Jalankan aplikasi:
go run cmd/web/main.go
Aplikasi akan berjalan di http://localhost:8080.
- Endpoint:
POST /api/users
- Request Body:
{ "name": "John Doe", "email": "john.doe@example.com", "password": "password123" }
- Response:
{ "status": "success", "message": "Successfully registered user", "data": { "name": "John Doe", "email": "john.doe@example.com", "access_token": "access_token" } }
- Endpoint:
POST /api/users/_login
- Request Body:
{ "email": "john.doe@example.com", "password": "password123" }
- Response:
{ "status": "success", "message": "Successfully login user", "data": { "name": "John Doe", "email": "john.doe@example.com", "access_token": "access_token" } }
- Endpoint:
GET /api/users/_current
- Response:
{ "status": "success", "message": "Successfully get current user", "data": { "name": "John Doe", "email": "john.doe@example.com" } }
- Endpoint:
PATCH /api/users/_current
- Request Body:
{ "name": "John Doe Updated", "password": "newpassword123" }
- Response:
{ "status": "success", "message": "Successfully updated user", "data": { "name": "John Doe Updated", "email": "john.doe@example.com" } }
- Endpoint:
POST /api/tasks
- Request Body:
{ "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31" }
- Response:
{ "status": "success", "message": "Successfully created task", "data": { "id": 1, "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31" } }
- Endpoint:
GET /api/tasks
- Response:
{ "status": "success", "message": "Tasks fetched successfully", "data": [ { "id": 1, "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31" } ], "paging": { "page": 1, "size": 10, "total_item": 1, "total_page": 1 } }
- Endpoint:
GET /api/tasks/:taskId
- Response:
{ "status": "success", "message": "Successfully get task", "data": { "id": 1, "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31" } }
- Endpoint:
PUT /api/tasks/:taskId
- Request Body:
{ "title": "Updated Task", "description": "Updated description", "status": "in_progress", "due_date": "2023-12-31" }
- Response:
{ "status": "success", "message": "Successfully updated task", "data": { "id": 1, "title": "Updated Task", "description": "Updated description", "status": "in_progress", "due_date": "2023-12-31" } }
- Endpoint:
DELETE /api/tasks/:taskId
- Response: No content (204)
- Endpoint:
POST /api/tags
- Request Body:
{ "name": "New Tag" }
- Response:
{ "status": "success", "message": "Successfully created tag", "data": { "id": 1, "name": "New Tag" } }
- Endpoint:
GET /api/tags
- Response:
{ "status": "success", "message": "Tags fetched successfully", "data": [ { "id": 1, "name": "New Tag" } ], "paging": { "page": 1, "size": 10, "total_item": 1, "total_page": 1 } }
- Endpoint:
GET /api/tags/:tagId
- Response:
{ "status": "success", "message": "Successfully get tag", "data": { "id": 1, "name": "New Tag" } }
- Endpoint:
PUT /api/tags/:tagId
- Request Body:
{ "name": "Updated Tag" }
- Response:
{ "status": "success", "message": "Successfully updated tag", "data": { "id": 1, "name": "Updated Tag" } }
- Endpoint:
DELETE /api/tags/:tagId
- Response: No content (204)
- Endpoint:
POST /api/tasks/:taskId/tags
- Request Body:
{ "tag_id": 1 }
- Response:
{ "status": "success", "message": "Successfully created task tag", "data": { "id": 1, "taskId": 1, "tag_id": 1 } }
- Endpoint:
GET /api/taskswithtags
- Response:
{ "status": "success", "message": "Task tags fetched successfully", "data": [ { "id": 1, "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31", "tag_id": 1 } ], "paging": { "page": 1, "size": 10, "total_item": 1, "total_page": 1 } }
- Endpoint:
GET /api/tags/:tagId/tasks
- Response:
{ "status": "success", "message": "Task tags fetched successfully", "data": [ { "id": 1, "title": "New Task", "description": "Task description", "status": "pending", "due_date": "2023-12-31", "tag_id": 1 } ], "paging": { "page": 1, "size": 10, "total_item": 1, "total_page": 1 } }
- Endpoint:
DELETE /api/tasks/:taskId/tags/:tagId
- Response: No content (204)