A modern REST API built with Go and Gin that connects to MongoDB, following Go best practices and clean architecture principles.
POST /api/v1/posts- Create a new postGET /api/v1/posts- Get all posts (with pagination)GET /api/v1/posts?published=true- Get only published postsGET /api/v1/posts/:id- Get a specific postPUT /api/v1/posts/:id- Update a postDELETE /api/v1/posts/:id- Delete a post
GET /api/v1/authors/:author/posts- Get posts by author
GET /health- Health check endpoint
gopirest/
βββ cmd/
β βββ server/
β βββ main.go # Application entry point
βββ internal/
β βββ config/ # Configuration management
β β βββ config.go # Config struct and loading logic
β β βββ default.json # Default embedded configuration
β β βββ local.json # Local development configuration
β βββ database/ # Database connection management
β β βββ mongodb.go # MongoDB connection and utilities
β β βββ mongodb_test.go # Database tests
β βββ handler/ # HTTP handlers (controllers)
β β βββ post.go # Post-related HTTP handlers
β βββ model/ # Data models and DTOs
β β βββ post.go # Post model and request/response types
β βββ repository/ # Data access layer
β β βββ post.go # Post repository interface and implementation
β βββ service/ # Business logic layer
β βββ post.go # Post service implementation
βββ examples/ # Usage examples
β βββ config_example.go # Configuration system example
βββ scripts/ # Utility scripts
β βββ test_mongodb.sh # MongoDB integration test script
βββ docker-compose.yml # Local development setup
βββ Dockerfile # Production container
βββ Makefile # Development commands
βββ README.md # This file
- Go 1.24.3 or later
- Docker and Docker Compose
- Make (optional, for using Makefile commands)
-
Clone the repository:
git clone https://github.com/senonide/gopirest.git cd gopirest -
Start MongoDB and services:
make dev-start # Or manually: docker compose up -d -
Run the application:
make run # Or manually: go run cmd/server/main.go
The API will be available at http://localhost:8080
# Start all services (API + MongoDB + Mongo Express)
docker compose up -d
# View logs
docker compose logs -f api
# Stop all services
docker compose downThe application uses an embedded configuration system that supports multiple environments:
- Default: Production-ready configuration embedded in the binary
- Local: Development configuration with debug mode enabled
- External: Override with external
config.jsonfile
GO_ENV: Set tolocalordevelopmentfor development configuration
{
"server": {
"port": 8080,
"host": "localhost",
"mode": "release"
},
"database": {
"name": "gopirest",
"user": "gopirest_user",
"password": "gopirest_password",
"host": "localhost:27017",
"timeout": 10
},
"logging": {
"level": "info",
"format": "json"
}
}When using Docker Compose:
- MongoDB:
mongodb://gopirest_user:gopirest_password@localhost:27017/gopirest - Admin Access:
mongodb://admin:password123@localhost:27017/ - Web UI (Mongo Express): http://localhost:8081
make build
# Creates: build/gopirestmake docker-build
# Creates: gopirest:latest# Build optimized binary
go build -ldflags="-s -w" -o gopirest ./cmd/server/main.go
# Run with production config
./gopirestmake test
# Or: go test ./...# Test MongoDB setup and API functionality
./scripts/test_mongodb.sh# Create a post
curl -X POST http://localhost:8080/api/v1/posts \
-H "Content-Type: application/json" \
-d '{"title":"Hello World","content":"My first post","author":"John Doe","published":true}'
# Get all posts
curl http://localhost:8080/api/v1/posts
# Get a specific post (replace ID)
curl http://localhost:8080/api/v1/posts/507f1f77bcf86cd799439011
# Health check
curl http://localhost:8080/healthThe project includes a comprehensive Makefile:
make help # Show all available commands
make dev-setup # Setup development environment
make dev-start # Start MongoDB with Docker Compose
make dev-stop # Stop development services
make run # Run the application
make build # Build production binary
make test # Run tests
make lint # Run linter
make clean # Clean build artifactscurl -X POST http://localhost:8080/api/v1/posts \
-H "Content-Type: application/json" \
-d '{
"title": "Getting Started with GopiRest",
"content": "This is a comprehensive guide to using the GopiRest API...",
"author": "Jane Developer",
"tags": ["go", "api", "mongodb"],
"published": true
}'curl "http://localhost:8080/api/v1/posts?page=1&limit=10&published=true"curl -X PUT http://localhost:8080/api/v1/posts/507f1f77bcf86cd799439011 \
-H "Content-Type: application/json" \
-d '{
"title": "Updated Title",
"published": true
}'See the LICENSE file for details.
Set environment variable for debug logging:
GO_ENV=local go run cmd/server/main.go