A production-ready HTTP server built in Go that implements a "Chirps" API (similar to Twitter/X). This project demonstrates modern Go web development patterns including RESTful API design, JWT authentication, database integration, middleware, and webhook handling.
Built while following Boot.dev's Learn HTTP Servers in Go course, which covers routing, middleware, JSON APIs, auth/JWTs, webhooks, and production-minded patterns in Go (course link).
This server provides a complete backend API for a social media platform where users can:
- User Management: Create accounts, update profiles, and manage authentication
- Authentication: Secure JWT-based authentication with refresh tokens
- Chirps: Post, read, update, and delete short messages ("chirps")
- Content Filtering: Automatic profanity filtering for user-generated content
- Webhooks: Integration with external services (Polka) for premium user upgrades
- Admin Features: Metrics and administrative endpoints
- Static File Serving: Serve frontend assets and track file server hits
The API is organized into three main route groups:
/api/- RESTful API endpoints for users, chirps, and authentication/app/- Static file server for frontend assets/admin/- Administrative endpoints and metrics
This project serves as an excellent learning resource and reference implementation for:
- Production-Ready Patterns: Demonstrates real-world HTTP server architecture in Go
- Security Best Practices: Implements secure password hashing (Argon2id), JWT authentication, and token refresh mechanisms
- Database Integration: Uses PostgreSQL with SQLC for type-safe database queries and migrations
- Clean Architecture: Well-organized code structure with separation of concerns
- Modern Go Features: Leverages Go 1.25+ features and standard library patterns
- API Design: RESTful API design with proper error handling and JSON responses
Whether you're learning Go web development, building a similar API, or looking for production patterns, this codebase provides a solid foundation.
- Go 1.25.5 or later (see
go.modfor exact version) - PostgreSQL database (for data persistence)
- just command runner (optional but recommended)
- goose and sqlc (optional, for database migrations and code generation)
git clone <repository-url>
cd boot-go-http-servergo mod downloadCreate a PostgreSQL database for the project:
createdb golang # or use your preferred database nameCreate a .env file in the project root with the following required variables:
# Database connection string (required)
DB_URL=postgres://username:password@localhost:5432/golang
# JWT secret for token signing (required)
JWT_SECRET=your-secret-key-here
# Polka API key for webhook authentication (required)
POLKA_KEY=your-polka-api-key
# Optional: Server port (defaults to 8080)
PORT=8080
# Optional: Environment mode (defaults to development)
ENVIRONMENT=developmentNote: Make sure .env is in your .gitignore (it should be by default) to avoid committing secrets.
Apply the database schema migrations:
# Using just (recommended)
just migrate
# Or manually with goose
goose -dir ./sql/schema postgres "your-db-url" up# Using just (recommended)
just run
# Or directly with Go
go run .The server will start on the port specified in your PORT environment variable (default: 8080). You should see:
Listening on port 8080
Test the health endpoint:
curl http://localhost:8080/api/healthzYou should receive OK as the response.
The project uses just for common development tasks. List all available commands:
just --listjust run— Start the development server (go run .)just test— Run auth package testsjust testv— Run tests with verbose outputjust migrate— Apply database migrations via goosejust rollback— Roll back the last migrationjust sqlc— Regenerate SQLC code from SQL queries
POST /api/users- Create a new user accountPOST /api/login- Authenticate and receive JWT tokensPUT /api/users- Update user account (requires authentication)POST /api/refresh- Refresh access tokenPOST /api/revoke- Revoke refresh token
GET /api/chirps- List all chirps (supportsauthor_idandsortquery params)GET /api/chirps/{chirpID}- Get a specific chirpPOST /api/chirps- Create a new chirp (requires authentication)DELETE /api/chirps/{chirpID}- Delete a chirp (requires authentication, owner only)
GET /admin/metrics- View file server hit metrics
GET /api/healthz- Health check endpoint
.
├── api.go # Main API route handlers
├── admin.go # Admin endpoints
├── app.go # Static file server
├── main.go # Application entry point
├── middleware.go # HTTP middleware (auth, logging, CORS)
├── helper.go # Utility functions
├── internal/
│ ├── auth/ # Authentication utilities (JWT, password hashing)
│ └── database/ # Database queries and models (generated by SQLC)
├── sql/
│ ├── schema/ # Database migrations
│ └── queries/ # SQL queries for SQLC
└── assets/ # Static frontend assets
- Go 1.25.5 - Programming language
- PostgreSQL - Relational database
- SQLC - Type-safe SQL code generation
- Goose - Database migrations
- JWT - JSON Web Tokens for authentication
- Argon2id - Password hashing algorithm
- godotenv - Environment variable management