Skip to content

nhdewitt/chirpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chirpy

Chirpy is a lightweight microblogging REST API written in Go. It provides basic functionality for posting short messages ("chirps"), user authentication, and token-based session management. It uses PostgreSQL for data storage, sqlc for type-safe SQL integration, and standard Go libraries for routing and middleware.

Features

  • User registration and login (email & password)
  • JWT-based authentication with refresh and revoke endpoints
  • CRUD operations for chirps
  • Admin metrics (hits, reset)
  • Webhook endpoint for Polka service
  • Static file server under /app for serving frontend assets
  • Database migrations with Goose
  • Environment-based configuration with godotenv

Prerequisites

  • Go 1.23 or later
  • PostgreSQL database
  • sqlc for generating Go code from SQL queries
  • goose for managing migrations
  • godotenv for loading environment variables

Getting Started

Clone the repository

git clone https://github.com/nhdewitt/chirpy.git
cd chirpy

Environment Variables

Copy .env.example (or create a new .env) with the following variables:

DB_URL=postgres://<user>:<password>@<host>:<port>/<dbname>?sslmode=disable
CHIRPY_AUTH_SECRET=<your-jwt-secret>
PLATFORM=<optional-platform-identifier>

Database Migrations

Ensure you have goose installed. Then run:

goose -dir sql/schema postgres "$DB_URL" up

This will create the necessary tables (users, chirps, refresh_tokens, etc.)

Generate SQL Code

Use sqlc to generate Go bindings for your SQL queries:

sqlc generate

Build & Run

go build -o bin/chirpy main.go
./bin/chirpy

The server will start on port 8080 by default.

API Reference

All endpoints are prefixed with /api (unless noted).

Healthcheck

  • GET /api/healthz
    • Returns OK if the server is running.

Authentication

  • POST /api/users

    • Create a new user.
    • Request body:
      {
        "email": "user@example.com",
        "password": "your-password"
      }
  • POST /api/login

    • Authenticate and receive access and refresh tokens.
    • Request body:
      {
        "email": "user@example.com",
        "password": "your-password"
      }
  • POST /api/refresh

    • Refresh the access token using a valid refresh token.
    • Request body:
      {
        "refresh_token": "<refresh-token>"
      }
  • POST /api/revoke

    • Revoke an existing refresh token.
    • Request body:
      {
        "refresh_token": "<refresh-token>"
      }

Users

  • PUT /api/users
    • Update authenticated user's profile (e.g., email or password).
    • Requires Authorization: Bearer <access-token> header.
    • Request body may include fields to update.

Chirps

  • GET /api/chirps

    • List all chirps. Supports pagination via query params (TBD).
  • POST /api/chirps

    • Create a new chirp.
    • Requires authentication.
    • Request body:
      {
        "body": "Your chirp message"
      }
  • GET /api/chirps/{chirpID}

    • Retrieve a single chirp by ID.
  • DELETE /api/chirps/{chirpID}

    • Delete a chirp by ID. Requires authentication and ownership.

Admin

  • GET /admin/metrics

    • Returns the number of static file hits and other metrics.
  • POST /admin/reset

    • Reset collected metrics to zero.

Webhooks

  • POST /api/polka/webhooks
    • Polka service webhook for upgrading a user account.
    • Request body and trigger handled internally.

Static File Server

  • Files under the project root can be served at /app.

Example:

GET /app/index.html HTTP/1.1
Host: localhost:8080

See index.html as a sample landing page.

Testing

Unit Tests

Run unit tests for internal packages:

go test ./internal/auth

HTTP Request Samples

Use the provided get.http as a template for API calls (requires a VS Code REST client or similar):

POST http://localhost:8080/api/login
Content-Type: application/json

{
    "email": "saul@bettercall.com",
    "password": "000011112222"
}

Configuration Files

  • sqlc.yaml - Configuration for sqlc code generation.
  • .gitignore - Standard Go .gitignore including build and vendor dirs.
  • go.mod & go.sum - Go module manifest.

Database Schema

Migrations in sql/schema define tables:

  1. 001_users.sql - Users table
  2. 002_chirps.sql - Chirps table
  3. 004_refresh_tokens.sql - Refresh tokens

(See files for full definitions.)

License

This project is open source under the MIT License. Feel free to use and contribute.


Generated by ChatGPT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages