An API starter template for projects based on Controller-Service-Repository (CSR) Pattern utilizing Gin (Golang) and PostgreSQL, with GORM as the ORM.
/backend
β
βββ /api
β βββ /v1
β βββ /controller
β β βββ user.go
β β βββ etc
β βββ /router
β βββ user.go
β βββ etc
β
βββ /cmd
β βββ db.go
β βββ exec.go
β βββ etc
β
βββ /config
β βββ db.go
β βββ etc
β
βββ /core
β βββ /entity
β β βββ user.go
β β βββ etc
β βββ /helper
β β βββ /dto
β β β βββ user.go
β β β βββ etc
β β βββ /errors
β β β βββ other.go
β β β βββ user.go
β β β βββ etc
β β βββ /messages
β β βββ file.go
β β βββ user.go
β β βββ etc
β βββ /interface
β β βββ /query
β β βββ user.go
β β βββ etc
β β βββ /repository
β β βββ tx.go
β β βββ user.go
β β βββ etc
β βββ /service
β βββ user.go
β βββ etc
β
βββ /database
β βββ /seeder
β β βββ user.go
β β βββ etc
β βββ migrator.go
β
βββ /infrastructure
β βββ /query
β β βββ user.go
β β βββ etc
β βββ /repository
β βββ tx.go
β βββ user.go
β βββ etc
β
βββ /provider
β βββ user.go
β βββ etc
β
βββ /support
β βββ /base
β β βββ model.go
β β βββ request.go
β β βββ response.go
β β βββ etc
β βββ /constant
β β βββ default.go
β β βββ enums.go
β β βββ etc
β βββ /middleware
β β βββ authentication.go
β β βββ cors.go
β β βββ authorization.go
β β βββ etc
β βββ /util
β βββ bcrypt.go
β βββ file.go
β βββ etc
β
βββ /tests
β βββ /testutil
β βββ /integration
β
βββ main.go
-
/api/v1
: The directory for things related to API like all available endpoints (routes) and the handlers for each endpoint (controller). Subdirectory/v1
is used for easy version control in case of several development phases./controller
: The directory for things related to the Controller layer, which handles requests and returns responses./router
: The directory for things related to routing. Contains all supported routes/endpoints along with request methods and used middleware.
-
/cmd
: The directory to store executable commands like for database migration or seeder runs. -
/config
: The directory for things related to program configuration like database configuration. -
/core
: The directory for things related to the core backend logic. Contains business logic, entities, and database interaction.-
/entity
: The directory for entities/models that are mapped to the database via migration. -
/helper
: The directory to store items that help backend operations, such as DTOs, error variables, and message constants./dto
: Stores DTO (Data Transfer Object) used as placeholders to transfer data for requests and responses./errors
: Stores error variables for each entity or other needs./messages
: Stores message constants for each entity or feature.
-
/interface
: The directory for all core interfaces used by the service layer, including contracts for repository and query layers./repository
: Interfaces for repository layer (entity CRUD)./query
: Interfaces for query layer (read-only operations, projections).
-
/service
: The directory for the Service layer, responsible for application flow and business logic.
-
-
/database
: The directory for things related to database migrations and seeding./seeder
: The directory for database seeders for each entity.
-
/infrastructure
: The directory for implementations of interfaces defined in/core/interface
, it's the only layer capable of interacting directly with the database./repository
: Implementations of repository interfaces, handling CRUD and transactional operations./query
: Implementations of query interfaces, handling read-only or optimized queries.
-
/provider
: The directory for dependency injection setup, e.g., Samber/Do providers for wiring services, repositories, and queries. -
/support
: The directory for common supporting things that are frequently used across the architecture./base
: The directory for base structures such as variables, constants, and functions used in other directories. Includes response, request, and model base structures./middleware
: The directory for Middlewares, mechanisms that intercept HTTP requests/responses before they are handled by controllers./util
: The directory for utility/helper functions that can be used in other directories.
-
/tests
: The directory for automated API testing (unit tests and integration tests)./testutil
: Stores utility/helper functions for testing purposes./integration
: Stores integration test functions.
-
main.go
: The entry point of the application.
-
/api/v1
: Direktori yang berisi berbagai hal yang berkaitan dengan API seperti daftar endpoint yang disediakan (route) serta handler (controller) dari setiap endpoint. Subdirectory/v1
digunakan untuk version control apabila ada beberapa versi API./controller
: Direktori untuk menyimpan hal-hal terkait Controller, yang bertugas menerima request dan memberikan response./router
: Direktori untuk menyimpan hal-hal yang terkait dengan routing, berisi semua route/endpoints yang didukung beserta metode request dan middleware yang digunakan.
-
/cmd
: Direktori untuk menyimpan perintah-perintah yang dapat dijalankan seperti migrasi atau seeding pada database. -
/config
: Direktori yang berisi hal-hal terkait konfigurasi aplikasi, misalnya konfigurasi database. -
/core
: Direktori yang berisi bagian inti dari backend. Meliputi business logic, entitas, dan interaksi dengan database.-
/entity
: Direktori untuk menyimpan entitas atau model yang digunakan di migrasi dan aplikasi. -
/helper
: Direktori untuk menyimpan hal-hal yang membantu operasi backend, seperti DTO, variabel error, dan konstanta pesan./dto
: Direktori untuk menyimpan DTO (Data Transfer Object), placeholder untuk memindahkan data request dan response./errors
: Direktori untuk menyimpan variabel error untuk setiap entitas maupun kebutuhan lain./messages
: Direktori untuk menyimpan konstanta pesan untuk response API.
-
/interface
: Direktori untuk menyimpan semua interface inti yang digunakan service layer, termasuk kontrak untuk repository dan query./repository
: Interface untuk repository (CRUD entitas)./query
: Interface untuk query (read-only, projections).
-
/service
: Direktori untuk service layer, yang menangani alur aplikasi dan logika bisnis.
-
-
/database
: Direktori untuk hal-hal terkait migrasi dan seeding database./seeder
: Direktori untuk database seeding tiap entitas.
-
/infrastructure
: Direktori untuk implementasi interface yang ada di/core/interface
. Merupakan satu-satunya layer yang dapat berinteraksi secara langsung dengan basis data./repository
: Implementasi repository, menangani operasi CRUD dan transaksi./query
: Implementasi query, menangani operasi read-only atau query yang dioptimalkan.
-
/provider
: Direktori untuk setup dependency injection, misalnya provider Samber/Do untuk menghubungkan service, repository, dan query. -
/support
: Direktori yang berisi hal-hal umum pembantu untuk digunakan di seluruh project./base
: Direktori yang berisi struktur dasar seperti variabel, konstanta, dan fungsi yang digunakan di directory lain. Termasuk response, request, dan model base structure./middleware
: Direktori untuk Middleware, mekanisme yang menengahi proses HTTP request/response sebelum ditangani controller./util
: Direktori untuk fungsi utilitas/pembantu yang dapat digunakan di berbagai directory.
-
/tests
: Direktori untuk automated API testing (unit dan integration tests)./testutil
: Menyimpan fungsi utilitas/pembantu untuk testing./integration
: Menyimpan fungsi integration testing.
-
main.go
: Titik masuk (entry point) aplikasi.
- Create the database in PostgreSQL with the name equal to the value of DB_NAME in
.env
Note : GitHooks is not mandatory for this starter. Only do the steps below if you want to apply & use it.
- Install golangci-lint as the linters aggregator for pre-commit linting by executing
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
. Alternatively, you can follow the recommended method, which involves installing the binary from the official source - Install commitlint as the conventional commit message checker by executing
go install github.com/conventionalcommit/commitlint@latest
. Alternatively, you can follow the recommended method, which involves installing the binary from the official source - Configure your git's hooks path to be linked to the
.githooks
directory on this repository by executinggit config core.hooksPath .githooks
- Use the command
make tidy
(or usego mod tidy
instead, ifmake
is unable to be used) to adjust the dependencies accordingly - Use the command
make setup
(or usego run main.go setup
instead, ifmake
is unable to be used) to setup (migrate and seed) your database - Use the command
make run
(or usego run main.go
instead, ifmake
is unable to be used) to run the application. You can also use Docker with air to auto-reload by runningmake up
(or usedocker-compose up
instead ifmake
is unable to be used) - Use the command
make test
(or usego test ./...
instead, ifmake
is unable to be used) to run the automated testing
Link : https://documenter.getpostman.com/view/25087235/2s9YXfcizj