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.
- 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
/appfor serving frontend assets - Database migrations with Goose
- Environment-based configuration with
godotenv
- Go 1.23 or later
- PostgreSQL database
sqlcfor generating Go code from SQL queriesgoosefor managing migrationsgodotenvfor loading environment variables
git clone https://github.com/nhdewitt/chirpy.git
cd chirpyCopy .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>Ensure you have goose installed. Then run:
goose -dir sql/schema postgres "$DB_URL" upThis will create the necessary tables (users, chirps, refresh_tokens, etc.)
Use sqlc to generate Go bindings for your SQL queries:
sqlc generatego build -o bin/chirpy main.go
./bin/chirpyThe server will start on port 8080 by default.
All endpoints are prefixed with /api (unless noted).
- GET
/api/healthz- Returns
OKif the server is running.
- Returns
-
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>" }
- 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.
-
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.
-
GET
/admin/metrics- Returns the number of static file hits and other metrics.
-
POST
/admin/reset- Reset collected metrics to zero.
- POST
/api/polka/webhooks- Polka service webhook for upgrading a user account.
- Request body and trigger handled internally.
- Files under the project root can be served at
/app.
Example:
GET /app/index.html HTTP/1.1
Host: localhost:8080See index.html as a sample landing page.
Run unit tests for internal packages:
go test ./internal/authUse 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"
}
sqlc.yaml- Configuration forsqlccode generation..gitignore- Standard Go.gitignoreincluding build and vendor dirs.go.mod&go.sum- Go module manifest.
Migrations in sql/schema define tables:
001_users.sql- Users table002_chirps.sql- Chirps table004_refresh_tokens.sql- Refresh tokens
(See files for full definitions.)
This project is open source under the MIT License. Feel free to use and contribute.
Generated by ChatGPT