A lightweight, performant REST API server built with Go that demonstrates modern backend development practices. Chirpy lets users post short messages (chirps), manage their accounts, and interact through a clean RESTful interface.
This project showcases:
- Clean Architecture - Well-organized code with clear separation of concerns
- Type-Safe Database Operations - Using sqlc for compile-time SQL verification
- Secure Authentication - JWT-based auth with refresh tokens
- Modern Go Patterns - Idiomatic Go code following best practices
- Production-Ready Features - Health checks, metrics, webhook support
Perfect for learning Go backend development or as a foundation for your own social platform!
- JWT Authentication - Secure token-based authentication with refresh tokens
- User Management - Registration, login, and profile updates
- Chirps (Posts) - Create, read, update, and delete short messages
- Webhook Integration - Support for external service webhooks (Polka)
- Metrics & Monitoring - Built-in metrics and health endpoints
- PostgreSQL Database - Robust data persistence with migrations
- Type-Safe Queries - sqlc-generated code eliminates SQL injection risks
- Well-Tested - Comprehensive test coverage for critical paths
- Go 1.24.4 - Fast, reliable backend language
- PostgreSQL - Battle-tested relational database
- sqlc - Generate type-safe Go from SQL
- Goose - Database migration management
- JWT - Industry-standard authentication
- bcrypt - Secure password hashing
- Go 1.24+ installed
- PostgreSQL database running
- sqlc installed (
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest) - Goose installed (
go install github.com/pressly/goose/v3/cmd/goose@latest)
- Clone the repository
git clone https://github.com/yourusername/basic-http-server.git
cd basic-http-server- Install dependencies
go mod tidy- Set up environment variables
# Create a .env file or export these variables
export DB_URL="postgres://username:password@localhost:5432/chirpy?sslmode=disable"
export JWT_SECRET="your-super-secret-jwt-key"
export PLATFORM="dev"
export POLKA_KEY="your-webhook-api-key"- Run database migrations
goose -dir sql/schema postgres "$DB_URL" up- Generate database code (if you modify SQL queries)
sqlc generate- Build and run
go build -o chirpy
./chirpyThe server will start on http://localhost:8080
GET /api/healthz- Health check endpointGET /admin/metrics- Server metrics
POST /api/login- Login with email/passwordPOST /api/refresh- Refresh access tokenPOST /api/revoke- Revoke refresh token
POST /api/users- Create new user accountPUT /api/users- Update user information (requires auth)
POST /api/chirps- Create a new chirp (requires auth)GET /api/chirps- List all chirps (optional filters)GET /api/chirps/{id}- Get specific chirpDELETE /api/chirps/{id}- Delete your chirp (requires auth)
POST /api/polka/webhooks- Webhook endpoint for Polka integration
POST /admin/reset- Reset database (development only)
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "securepassword"}'curl -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "securepassword"}'curl -X POST http://localhost:8080/api/chirps \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{"body": "This is my first chirp!"}'curl http://localhost:8080/api/chirps# Run all tests
go test ./...
# Run with coverage
go test -cover ./...
# Run specific package tests
go test ./internal/auth -vgo fmt ./...
go vet ./...# Create a new migration
goose -dir sql/schema create migration_name sql
# Apply migrations
goose -dir sql/schema postgres "$DB_URL" up
# Rollback last migration
goose -dir sql/schema postgres "$DB_URL" down
# Check migration status
goose -dir sql/schema postgres "$DB_URL" statusAfter modifying SQL queries in sql/queries/:
sqlc generate.
├── main.go # Application entry point
├── handler_*.go # HTTP request handlers
├── json.go # JSON response helpers
├── metrics.go # Metrics middleware
├── readiness.go # Health check handler
├── reset.go # Database reset handler
├── internal/
│ ├── auth/ # Authentication logic
│ │ ├── auth.go # JWT & password functions
│ │ └── auth_test.go # Auth unit tests
│ └── database/ # Generated database code
│ ├── *.sql.go # sqlc-generated queries
│ └── models.go # Database models
├── sql/
│ ├── queries/ # SQL queries for sqlc
│ └── schema/ # Database migrations
└── sqlc.yaml # sqlc configuration
- Passwords are hashed using bcrypt before storage
- JWT tokens expire after a configurable duration
- All authenticated endpoints validate tokens before processing
- SQL injection prevention through parameterized queries (sqlc)
- Environment-based configuration keeps secrets out of code
Feel free to open issues or submit pull requests! Make sure to:
- Run tests before submitting
- Follow Go formatting standards
- Update documentation for new features
- Add tests for new functionality
MIT License - feel free to use this project as a learning resource or foundation for your own applications!
Built as a learning project to explore Go backend development, inspired by the need for a simple yet robust social platform API.