A Twitter-like microblogging platform built with Go, featuring user authentication, chirp posting, and profanity filtering.
- User Management: User registration and authentication with secure password hashing
- Chirp System: Post short messages (chirps) up to 140 characters
- Profanity Filtering: Automatic content moderation using a built-in profanity filter
- RESTful API: Clean HTTP API endpoints for all operations
- Database Integration: PostgreSQL database with automatic schema management
- Security: bcrypt password hashing and UUID-based identifiers
- Metrics: Built-in visit counter and admin metrics
- Backend: Go (Golang) with standard library HTTP server
- Database: PostgreSQL with automatic code generation via SQLC
- Authentication: bcrypt password hashing
- API: RESTful HTTP endpoints
- Schema Management: Goose migrations for database versioning
Before running Chirpy, ensure you have the following installed on your machine:
- Go 1.24.1 or later - Download here
- PostgreSQL 12 or later - Download here
- Goose - For database migrations:
go install github.com/pressly/goose/v3/cmd/goose@latest - SQLC - For code generation:
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
git clone https://github.com/bernardodestefano/chirpy.git
cd chirpygo mod download# Connect to PostgreSQL as superuser
psql -U postgres
# Create database and user
CREATE DATABASE chirpy;
CREATE USER chirpy_user WITH PASSWORD 'your_secure_password';
GRANT ALL PRIVILEGES ON DATABASE chirpy TO chirpy_user;
\q# Set your database URL
export DB_URL="postgres://chirpy_user:your_secure_password@localhost:5432/chirpy?sslmode=disable"
# Run migrations (if you have Goose installed)
goose -dir sql/schema postgres "$DB_URL" upNote: If you don't have Goose installed, you can manually run the SQL files in sql/schema/ in order:
001_users.sql002_chirps.sql003_users_hashed_password.sql
Create a .env file in the project root:
# Database connection
DB_URL=postgres://chirpy_user:your_secure_password@localhost:5432/chirpy?sslmode=disable
# Platform environment (dev/prod)
PLATFORM=dev# Generate Go code from SQL queries
sqlc generate# Run the application
go run main.goThe server will start on http://localhost:8080
# Build the binary
go build -o chirpy .
# Run the binary
./chirpyGET /api/healthz- Health check endpoint
POST /api/users- Create a new userPOST /api/login- User authentication
POST /api/chirps- Create a new chirpGET /api/chirps- Get all chirpsGET /api/chirps/{chirpID}- Get a specific chirp
GET /admin/metrics- View application metricsPOST /admin/reset- Reset metrics and delete all users (dev only)
GET /app/*- Serve static files (HTML, CSS, JS)
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
email TEXT UNIQUE NOT NULL,
hashed_password TEXT NOT NULL
);CREATE TABLE chirps (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
body TEXT NOT NULL,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE
);| Variable | Description | Default |
|---|---|---|
DB_URL |
PostgreSQL connection string | Required |
PLATFORM |
Environment (dev/prod) | dev |
The application uses the following PostgreSQL extensions:
pgcrypto- For UUID generation and cryptographic functions
Run the test suite:
# Run all tests
go test ./...
# Run specific package tests
go test ./internal/auth/...
# Run tests with verbose output
go test -v ./...chirpy/
βββ assets/ # Static assets (logo, etc.)
βββ internal/ # Internal application code
β βββ auth/ # Authentication utilities
β βββ database/ # Database models and queries
β βββ helpers.go # Helper functions
βββ sql/ # Database-related files
β βββ queries/ # SQL queries for SQLC
β βββ schema/ # Database migrations
βββ main.go # Application entry point
βββ go.mod # Go module file
βββ go.sum # Go module checksums
βββ sqlc.yaml # SQLC configuration
βββ README.md # This file
- Password Hashing: bcrypt with default cost factor
- UUID Identifiers: Prevents enumeration attacks
- Input Validation: Comprehensive parameter validation
- Profanity Filtering: Content moderation
- CORS Protection: Built-in security headers
-
Database Connection Failed
- Verify PostgreSQL is running
- Check
DB_URLenvironment variable - Ensure database and user exist
-
Migration Errors
- Check database permissions
- Verify SQL syntax in migration files
- Ensure migrations are run in order
-
Port Already in Use
- Change the port in
main.go(line 298) - Kill existing processes using port 8080
- Change the port in
Set PLATFORM=dev in your .env file to enable:
- Reset endpoint access
- Detailed logging
- Development-only features
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is open source and available under the MIT License.
If you encounter any issues or have questions:
- Check the troubleshooting section above
- Review the API documentation
- Open an issue on GitHub
- Check the Go and PostgreSQL documentation
Happy Chirping! π¦β¨