A minimal, high-performance Go backend for a blog system. This project features JWT authentication, role-based access control (Author/Reader), idempotent post creation, and image management using MinIO.
- Authentication: Register, Login, and Refresh Token using JWT.
- Role-Based Access:
- Author: Create, edit, publish, and manage images for their posts.
- Reader: Browse published blog posts.
- Media Management: Upload and delete post images via MinIO (S3 compatible).
- Reliability: Uses idempotency keys for post creation to prevent duplicate entries.
- Performance: Utilizes
easyjsonfor optimized JSON (un)marshaling. - Documentation: Fully documented with Swagger (OpenAPI 2.0).
- Language: Go
- Database: PostgreSQL
- Object Storage: MinIO
- Documentation: Swagger (swag)
- JSON Optimization: EasyJSON
- Containerization: Docker & Docker Compose
Ensure you have the following installed:
- Go (1.21+)
- Docker & Docker Compose
- swag (for API docs)
- easyjson (for DTO generation)
- Clone the repository.
git clone https://github.com/xKARASb/blog && cd blog
- Create a
.envanddocker.envfile based on the provided example:cp example.env .env cp example.env docker.env
- Update the
.envanddocker.envvalues (especially thePOSTGRES_HOSTandMINIO_ENDPOINTif running locally vs. in Docker).
This is the fastest way to get the database, storage, and app running.
# Install dependencies
make utils
# Build and start all services
make docker-dev
# Build and start all services in background
make docker-upIf you want to run the Go server locally:
- Make sure that the
databaseandstorageare up - Set up
.envlike inexample.env - Install dependencies:
make utils go mod download
- Run the app:
make run
Once the server is running, you can access the interactive Swagger UI:
http://localhost/swagger/ (Base path: /api)
Or with defined port if run localy
http://localhost:8080/swagger/ (Base path: /api)
To manually regenerate documentation after changing code:
make swagger| Command | Description |
|---|---|
make swagger |
Update Swagger/OpenAPI documentation |
make json |
Generate optimized DTO models using easyjson |
make build |
Compile the binary to ./bin/app |
make run |
Generate dependencies and run the server |
make test |
Run unit tests for HTTP handlers |
make docker-dev |
Build and start everything in development mode |
make docker-up |
Start infrastructure (DB, MinIO) in background |
make docker-down |
Stop and remove all containers |
make docker-build |
Build the production-ready Docker image |
cmd/server/main.go: Application entry point.internal/core/servers/: Server initialization and middleware.internal/core/dto/: Data Transfer Objects (optimized with EasyJSON).internal/transport/http/handlers/: API route handlers and logic.docs/: Generated Swagger documentation.
- JWT: Headers should include
Authorization: Bearer <your_token>. - Validation: Strict input validation on registration and post creation.
- Storage: MinIO access keys managed via environment variables.
- Adding a new DTO: Add the struct to
internal/core/dto/and runmake json. - Adding a new Route: Add the logic in handlers, update Swagger comments, and run
make swagger. - Deployment: Run
make docker-buildto ensure tests pass and images are ready for registry.