Skip to content

frozendolphin/Chirpy

Repository files navigation

Chirpy - A Twitter-like Microblogging API

Go Version License

πŸ“– Overview

Chirpy is a robust, production-ready microblogging API built with Go that provides Twitter-like functionality. It features user authentication, content management, real-time metrics, and premium user upgrades through webhook integrations.

🎯 What Chirpy Does

Chirpy is a RESTful API that enables users to:

  • Create and manage user accounts with secure authentication
  • Post short messages (chirps) with content filtering and validation
  • Authenticate users using JWT tokens with refresh token support
  • Manage user profiles with email and password updates
  • Upgrade to premium features through webhook integrations
  • Track application metrics with admin dashboard
  • Serve static content with hit tracking

πŸš€ Why You Should Care

  • Production Ready: Built with enterprise-grade security and scalability in mind
  • Modern Architecture: Uses Go 1.24.4 with PostgreSQL and JWT authentication
  • Content Safety: Built-in profanity filtering and content validation
  • Developer Friendly: Comprehensive API documentation and easy setup
  • Extensible: Modular design with clear separation of concerns
  • Monitoring: Built-in metrics and health checks for production deployment

πŸ› οΈ Technology Stack

  • Backend: Go 1.24.4
  • Database: PostgreSQL
  • Authentication: JWT with refresh tokens
  • Password Hashing: bcrypt
  • Database ORM: sqlc (type-safe SQL)
  • Environment: godotenv
  • UUID Generation: Google UUID

πŸ“‹ Prerequisites

Before running Chirpy, ensure you have:

  • Go 1.24.4 or later installed
  • PostgreSQL database server running
  • Git for cloning the repository

πŸš€ Installation & Setup

1. Clone the Repository

git clone https://github.com/frozendolphin/Chirpy.git
cd Chirpy

2. Install Dependencies

go mod download

3. Database Setup

Create Postgres Database

CREATE DATABASE chirpy_db;

Run Database Migrations

The project uses Goose for database migrations. Install Goose first:

go install github.com/pressly/goose/v3/cmd/goose@latest

Then run the migrations:

goose -dir sql/schema postgres "your_connection_string" up

4. Environment Configuration

Create a .env file in the project root:

DB_URL=postgres://username:password@localhost:5432/chirpy_db?sslmode=disable
PLATFORM=dev
SECRET=your_jwt_secret_key_here
POLKA_KEY=your_polka_webhook_key_here //polka is just like stripe

5. Generate Database Code

The project uses sqlc for type-safe database operations:

sqlc generate

6. Run the Application

go run .

The server will start on http://localhost:8080

πŸ“š API Documentation

Authentication Endpoints

POST /api/users

Create a new user account.

Request Body:

{
  "email": "[email protected]",
  "password": "securepassword"
}

Response:

{
  "id": "uuid",
  "created_at": "2024-01-01T00:00:00Z",
  "updated_at": "2024-01-01T00:00:00Z",
  "email": "[email protected]",
  "is_chirpy_red": false
}

POST /api/login

Authenticate a user and receive access tokens.

Request Body:

{
  "email": "[email protected]",
  "password": "securepassword"
}

Response:

{
  "id": "uuid",
  "created_at": "2024-01-01T00:00:00Z",
  "updated_at": "2024-01-01T00:00:00Z",
  "email": "[email protected]",
  "token": "jwt_access_token",
  "refresh_token": "jwt_refresh_token",
  "is_chirpy_red": false
}

POST /api/refresh

Refresh an access token using a refresh token.

Headers:

Authorization: Bearer <refresh_token>

Response:

{
  "token": "new_jwt_access_token"
}

POST /api/revoke

Revoke a refresh token.

Headers:

Authorization: Bearer <refresh_token>

Chirps (Posts) Endpoints

POST /api/chirps

Create a new chirp (post).

Headers:

Authorization: Bearer <access_token>

Request Body:

{
  "body": "This is my chirp content!"
}

Response:

{
  "id": "uuid",
  "created_at": "2024-01-01T00:00:00Z",
  "updated_at": "2024-01-01T00:00:00Z",
  "body": "This is my chirp content!",
  "user_id": "uuid"
}

GET /api/chirps

Retrieve all chirps.

Response:

[
  {
    "id": "uuid",
    "created_at": "2024-01-01T00:00:00Z",
    "updated_at": "2024-01-01T00:00:00Z",
    "body": "This is my chirp content!",
    "user_id": "uuid"
  }
]

GET /api/chirps/{chirpID}

Retrieve a specific chirp by ID.

DELETE /api/chirps/{chirpID}

Delete a specific chirp (requires authentication).

Headers:

Authorization: Bearer <access_token>

User Management Endpoints

PUT /api/users

Update user email and password.

Headers:

Authorization: Bearer <access_token>

Request Body:

{
  "email": "[email protected]",
  "password": "newpassword"
}

Admin Endpoints

GET /admin/metrics

View application metrics (hit counter).

Response:

<html>
  <body>
    <h1>Welcome, Chirpy Admin</h1>
    <p>Chirpy has been visited 42 times!</p>
  </body>
</html>

POST /admin/reset

Reset all users (development only).

Webhook Endpoints

POST /api/polka/webhooks

Handle Polka webhook events for user upgrades.

Headers:

Authorization: ApiKey <polka_key>

Request Body:

{
  "event": "user.upgraded",
  "data": {
    "user_id": "uuid"
  }
}

Health & Monitoring

GET /api/healthz

Health check endpoint.

GET /app/

Serve static files with hit tracking.

πŸ”’ Security Features

  • JWT Authentication: Secure token-based authentication
  • Password Hashing: bcrypt for secure password storage
  • Content Filtering: Automatic profanity filtering
  • Input Validation: Comprehensive request validation
  • CORS Support: Cross-origin resource sharing configuration
  • Rate Limiting: Built-in request limiting (configurable)

πŸ“Š Content Filtering

Chirpy automatically filters inappropriate content by replacing profane words with asterisks. The current filter list includes:

  • kerfuffle
  • sharbert
  • fornax

πŸ—οΈ Project Structure

Chirpy/
β”œβ”€β”€ main.go                 # Application entry point
β”œβ”€β”€ handlechirps.go        # Chirp-related handlers
β”œβ”€β”€ handleusers.go         # User management handlers
β”œβ”€β”€ handlelogin.go         # Authentication handlers
β”œβ”€β”€ handlerefresh.go       # Token refresh handlers
β”œβ”€β”€ handlewebhook.go       # Webhook handlers
β”œβ”€β”€ metrics.go             # Metrics and monitoring
β”œβ”€β”€ readiness.go           # Health checks
β”œβ”€β”€ json.go                # JSON response utilities
β”œβ”€β”€ go.mod                 # Go module dependencies
β”œβ”€β”€ sqlc.yaml             # SQL code generation config
β”œβ”€β”€ test.http             # API testing examples
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ auth/             # Authentication utilities
β”‚   └── database/         # Generated database code
β”œβ”€β”€ sql/
β”‚   β”œβ”€β”€ schema/           # Database migrations
β”‚   └── queries/          # SQL queries
└── assets/               # Static assets

πŸ§ͺ Testing

Test the API using the provided test.http file or with curl:

# Test health endpoint
curl http://localhost:8080/api/healthz

# Create a user
curl -X POST http://localhost:8080/api/users \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"password123"}'

πŸš€ Deployment

Production Considerations

  1. Environment Variables: Ensure all required environment variables are set
  2. Database: Use a production PostgreSQL instance
  3. SSL/TLS: Configure HTTPS for production
  4. Monitoring: Set up proper logging and monitoring
  5. Backup: Implement database backup strategies

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Uses sqlc for type-safe database operations
  • JWT implementation for secure authentication
  • PostgreSQL for reliable data storage

πŸ“ž Support

For support, please open an issue on GitHub.


About

A Twitter-like Microblogging API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published