A RESTful API template built with Go programming language.
- Go 1.24 or later
- PostgreSQL 17 or later
- Docker (optional)
- Unit Testing: Implement the unit testing in order to code maintenance.
- Middleware: Implement the JWT Auth for the middleware.
- Service Layer: Implement the service layer for separating business logic from application logic.
- Repository Pattern: Managing interactions with databases using the repository pattern.
- CRUD Implementation: Implement CRUD logic in the model entity.
- Configuration: Supports environment variables configuration via .env files.
- CORS Support: Supports Cross-Origin Resource Sharing (CORS) configuration.
- Deployment: Supports build and deploy using Docker.
The project provides a RESTful API for posts. The API follows standard REST conventions:
GET /api/v1/users- Get all users.GET /api/v1/user/:id- Get an user by id.POST /api/v1/user/register- Register an user.POST /api/v1/user/login- Login for the registered user.
GET /api/v1/news- Get all news.GET /api/v1/news/:id- Get a news by id.POST /api/v1/news- Create a news.PUT /api/v1/news/:id- Edit a news by id.DELETE /api/v1/news/:id- Delete a news by id.
- Clone the repository:
git clone https://github.com/ahmadammarm/go-rest-api-template.git- Navigate to the project directory:
cd go-rest-api-template- Install the project dependencies:
go mod download- Configure Environment Variable: Copy the file
.env.exampleto.envand adjust it to your configuration:
cp .env.example .envPOSTGRES_HOST=localhost
POSTGRES_PORT=
POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
JWT_SECRET_KEY=your-jwt-secret-key
CORS_ALLOW_ORIGINS=your-header-http-domain
-
POSTGRES: Configuration for PostgreSQL database connection.
-
JWT_SECRET_KEY: The secret key used for JWT token signing and verification.
-
CORS_ALLOW_ORIGINS: The allowed origins for Cross-Origin Resource Sharing (CORS). This is the domain that will be able to access resources from this API. For example, if you are running the frontend on http://localhost:5173, you should set this environment variable to http://localhost:5173.
- Run the project:
go run cmd/main.go- Clone the repository:
git clone https://github.com/ahmadammarm/go-rest-api-template.git- Navigate to the project directory:
cd go-rest-api-template- Run the Docker Compose:
docker-compose upor run in detach mode:
docker-compose up -d- To stop the Docker Compose:
docker-compose downThe project will be available at:
http://localhost:8080