π Go Fiber Boilerplate
A modern, production-ready Go REST API boilerplate built with Fiber, Ent ORM, and PostgreSQL.
β¨ Features
- 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
ποΈ Architecture
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
π Quick Start
Prerequisites
- Go 1.24.2 or higher
- PostgreSQL 12 or higher
- Docker (optional)
1. Clone the Repository
git clone https://github.com/NikSchaefer/go-fiber
cd go-fiber
2. Install Dependencies
go mod tidy
3. Set Up Environment Variables
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
4. Set Up Database
Option A: Using Docker (Recommended)
# 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
Option B: Local PostgreSQL
Make sure PostgreSQL is running and create a database:
CREATE DATABASE postgres;
5. Run the Application
go run main.go
The server will start on http://localhost:8000
π³ Docker Deployment
Build and Run with Docker
# 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
π API Documentation
Authentication Endpoints
User Registration
POST /auth/signup
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "securepassword123"
}
Password Login
POST /auth/login/password
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword123"
}
OTP Login Request
POST /auth/login/otp/request
Content-Type: application/json
{
"email": "john@example.com"
}
OTP Verification
POST /auth/login/otp/verify
Content-Type: application/json
{
"email": "john@example.com",
"otp": "123456"
}
Logout
DELETE /auth/logout
Cookie: session=<session_token>
User Management
Get Current User
GET /users/me
Cookie: session=<session_token>
Update User Profile
PATCH /users/profile
Cookie: session=<session_token>
Content-Type: application/json
{
"bio": "Software Developer",
"location": "San Francisco"
}
Change Password
POST /auth/password/change
Cookie: session=<session_token>
Content-Type: application/json
{
"currentPassword": "oldpassword",
"newPassword": "newpassword123"
}
OAuth Integration
Google OAuth
POST /auth/oauth/google
Content-Type: application/json
{
"redirectUri": "http://localhost:3000/callback"
}
π§ Configuration
Environment Variables
| 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 |
- |
β |
Database Schema
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
π οΈ Development
Project Structure
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
Adding New Endpoints
- Create a handler in
internal/handlers/
- Add business logic in
internal/services/
- Define routes in
internal/router/router.go
- Add validation in
pkg/validator/
Database Migrations
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
Testing
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run specific test
go test ./internal/handlers/auth
π Security Features
- 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)
π Monitoring & Analytics
Health Check
GET /
Returns a simple health check response.
Analytics Integration
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,
})
π Deployment
Production Checklist
- 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
Environment-Specific Configurations
Development
STAGE=development
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001
Production
STAGE=production
ALLOWED_ORIGINS=https://yourdomain.com
π€ Contributing
- 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
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
π Support
If you have any questions or need help:
Made with β€οΈ