A modern, production-ready Go REST API boilerplate built with Fiber, Ent ORM, and PostgreSQL.
- Authentication & Authorization - JWT-based sessions with OAuth support
- Email & SMS Integration - Resend and Twilio integration
- Analytics - PostHog integration for user analytics
- Database - PostgreSQL with Ent ORM for type-safe queries
- Docker Support - Multi-stage Docker builds
- Security - CORS, security headers, input validation
- OTP Authentication - One-time password support
- Production Ready - Graceful shutdown, proper error handling
gofiber-boilerplate/
βββ config/ # Configuration management
βββ ent/ # Ent ORM generated code
βββ internal/ # Private application code
β βββ database/ # Database connection & setup
β βββ handlers/ # HTTP request handlers
β βββ middleware/ # Custom middleware
β βββ router/ # Route definitions
β βββ services/ # Business logic
βββ model/ # Data models
βββ pkg/ # Public packages
β βββ analytics/ # Analytics integration
β βββ notifications/ # Email/SMS services
β βββ utils/ # Utility functions
β βββ validator/ # Input validation
βββ seeds/ # Database seeders
- Go 1.24.2 or higher
- PostgreSQL 12 or higher
- Docker (optional)
git clone https://github.com/NikSchaefer/go-fiber
cd go-fiber
go mod tidy
Create a .env
file in the root directory:
# Copy the example environment file
cp .env.example .env
Configure your environment variables:
# Database Configuration
DATABASE_URL="host=localhost port=5432 user=postgres password=password dbname=postgres sslmode=disable"
# Server Configuration
PORT=8000
STAGE=development
ALLOWED_ORIGINS="http://localhost:3000,http://localhost:3001"
# External Services (Optional for development)
POSTHOG_KEY=your_posthog_key_here
RESEND_KEY=your_resend_key_here
TWILIO_ACCOUNT_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
# OAuth Configuration (Optional for development)
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
# Application Configuration
APP_DOMAIN=localhost:8000
TWILIO_PHONE_NUMBER=+1234567890
# Start PostgreSQL container
docker run --name postgres-db \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=postgres \
-p 5432:5432 \
-d postgres:alpine
# Wait a few seconds for the database to start
Make sure PostgreSQL is running and create a database:
CREATE DATABASE postgres;
go run main.go
The server will start on http://localhost:8000
# Build the Docker image
docker build -t go-fiber-app .
# Run the container
docker run -p 8000:8000 \
--env-file .env \
--name go-fiber-container \
go-fiber-app
POST /auth/signup
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword123"
}
POST /auth/login/password
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword123"
}
POST /auth/login/otp/request
Content-Type: application/json
{
"email": "john@example.com"
}
POST /auth/login/otp/verify
Content-Type: application/json
{
"email": "john@example.com",
"otp": "123456"
}
DELETE /auth/logout
Cookie: session=<session_token>
GET /users/me
Cookie: session=<session_token>
PATCH /users/profile
Cookie: session=<session_token>
Content-Type: application/json
{
"bio": "Software Developer",
"location": "San Francisco"
}
POST /auth/password/change
Cookie: session=<session_token>
Content-Type: application/json
{
"currentPassword": "oldpassword",
"newPassword": "newpassword123"
}
POST /auth/oauth/google
Content-Type: application/json
{
"redirectUri": "http://localhost:3000/callback"
}
Variable | Description | Default | Required |
---|---|---|---|
DATABASE_URL |
PostgreSQL connection string | - | β |
PORT |
Server port | 8000 |
β |
STAGE |
Environment stage | development |
β |
ALLOWED_ORIGINS |
CORS allowed origins | localhost:3000,3001 |
β |
POSTHOG_KEY |
PostHog analytics key | - | β |
RESEND_KEY |
Resend email API key | - | β |
TWILIO_ACCOUNT_SID |
Twilio account SID | - | β |
TWILIO_AUTH_TOKEN |
Twilio auth token | - | β |
GOOGLE_CLIENT_ID |
Google OAuth client ID | - | β |
GOOGLE_CLIENT_SECRET |
Google OAuth client secret | - | β |
The application uses Ent ORM with the following entities:
- User - User accounts and profiles
- Session - User sessions and authentication
- OTP - One-time passwords for authentication
- Account - OAuth account connections
- Profile - User profile information
internal/
βββ database/ # Database connection and setup
βββ handlers/ # HTTP request handlers
β βββ auth/ # Authentication handlers
β βββ users/ # User management handlers
βββ middleware/ # Custom middleware
β βββ auth.go # Authentication middleware
β βββ security.go # Security headers
β βββ json.go # JSON parsing middleware
βββ router/ # Route definitions
βββ services/ # Business logic layer
- Create a handler in
internal/handlers/
- Add business logic in
internal/services/
- Define routes in
internal/router/router.go
- Add validation in
pkg/validator/
The application uses Ent ORM for database management:
# Generate Ent code after schema changes
go generate ./ent
# Run migrations (automatic in development)
go run main.go
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test
go test ./internal/handlers/auth
- CORS Protection - Configurable allowed origins
- Security Headers - XSS protection, content type options
- Input Validation - Request validation using validator
- Session Management - Secure session handling
- Password Hashing - bcrypt password hashing
- Rate Limiting - Built-in rate limiting (configurable)
GET /
Returns a simple health check response.
The application includes PostHog integration for user analytics:
// Track user events
analytics.Track("user_signed_up", map[string]interface{}{
"user_id": user.ID,
"email": user.Email,
})
- Set
STAGE=production
in environment - Configure
ALLOWED_ORIGINS
with your domain - Set up SSL/TLS certificates
- Configure database connection pooling
- Set up monitoring and logging
- Configure backup strategy
- Set up CI/CD pipeline
STAGE=development
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001
STAGE=production
ALLOWED_ORIGINS=https://yourdomain.com
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Fiber - Fast HTTP framework
- Ent - Type-safe ORM
- PostgreSQL - Reliable database
- Resend - Email delivery
- Twilio - SMS services
- PostHog - Product analytics
If you have any questions or need help:
- Create an issue
- Check the documentation
- Join our Discord community
Made with β€οΈ