A project from boot.dev's go backend course.
Chirpy is a lightweight social media platform backend written in Go. It provides APIs for user management, authentication, and posting short messages called "chirps." The project uses Postgres for backend.
- User registration and authentication
- Password hashing using Argon2id
- JWT-based access tokens and refresh tokens
- CRUD operations for "chirps"
- Admin endpoints for metrics and database reset
- Input validation and basic content moderation
- API key-based webhook handling for user upgrades
.
├── assets/ # Static assets (e.g., logo)
├── internal/ # Internal packages
│ ├── auth/ # Authentication logic
│ └── database/ # Database queries and models
├── sql/ # SQL schema and queries
│ ├── schema/ # Database schema migrations
│ └── queries/ # SQL queries for sqlc
├── main.go # Entry point of the application
├── mux.go # HTTP router setup
├── handler_*.go # HTTP handlers for various endpoints
├── funcs.go # Utility functions
├── consts.go # Global constants and configuration
├── index.html # Welcome page
├── go.mod # Go module file
└── sqlc.yaml # sqlc configuration
- Go 1.25.1 or higher
- PostgreSQL database
- Environment variables:
DB_URL: PostgreSQL connection stringSECRET: for JWT signingPOLKA_KEY: API key for webhook authentication
-
Clone the repository:
git clone https://github.com/your-repo/chirpy.git cd chirpy -
Install dependencies:
go mod tidy
-
Set up the database:
- Apply the migrations in
sql/schema/using a tool likegoose.
- Apply the migrations in
-
Configure environment variables:
- Create a
.envfile with the required variables:DB_URL=your_database_url #format:postgresql://USERNAME:PASSWORD@HOST:PORT/DATABASE_NAME SECRET=your_secret_key POLKA_KEY=your_polka_key
- Create a
-
Start the server:
go run main.go
-
The server will run on
http://localhost:8080. -
Use the following endpoints:
POST /api/users: Register a new userPOST /api/login: Log in and receive tokensPOST /api/refresh: Refresh access token
POST /api/chirps: Create a new chirpGET /api/chirps: Get all chirps(Supported queries:?sort=asc(default)/descwhich sorts by created_at, or?author_id=IDwhich returns chirps for given ID)GET /api/chirps/{chirpID}: Get a specific chirpDELETE /api/chirps/{chirpID}: Delete a chirpPUT /api/users: Update user details
GET /admin/metrics: View metricsPOST /admin/reset: Reset the database (dev mode only)
POST /api/polka/webhooks: Handle user upgrade events
Run the tests using:
go test ./...