,-,
( O)>
`-'
/(_) // ||\
// || \
// || \
// || \
// || \
// || \
// || \
// || \
// || \
// || \
//___________||__________\
// || \
(( || ))
\___________||___________//
||
||
||
||
||
/|| /_||_ / O||O / ==== /________ \ (@)(@) /
\ /\ /
\ VV /
\__/
A simple, Twitter-like API backend built in Go.
To get Chirpy running locally, you'll need to set up your environment and database.
Chirpy uses environment variables for configuration. These are typically loaded from a .env file in the project root during development. Create a .env file based on the .env.example file provided in the repository.
The following variables are required:
DB_URL: The connection string for your PostgreSQL database.- Example:
postgres://user:password@localhost:5432/chirpy?sslmode=disable(for local development, SSL is often disabled).
- Example:
PLATFORM: A string identifying the platform (e.g., "local", "staging", "production").JWT_SECRET: A secure secret key used to sign and verify JSON Web Tokens. You can generate a suitable secret using OpenSSL:openssl rand -base64 64
POLKA_KEY: An API key for the Polka webhook, used for upgrading users to "Chirpy Red".
Chirpy uses Goose for managing database schema migrations and SQLC for generating type-safe Go code from SQL queries.
- Migration Files: Schema migration files are located in the
sql/schema/directory. These define the database structure and changes. - Applying Migrations: To set up or update your database schema, you will need to run Goose commands (e.g.,
goose up). Refer to the Goose documentation for installation and usage. - SQL Queries: SQL queries are defined in
sql/queries/. - Generating Go Code: SQLC uses these queries and the schema to generate Go code in the
internal/database/directory. If you modify queries or the schema, runsqlc generateto update the Go code. Refer to the SQLC documentation for setup.
- Users: Create and manage user accounts.
- Chirps: Post short messages (up to 140 characters), view, and delete them.
- Authentication: Uses JWT for secure API access.
- Profanity Filter: Automatically censors certain words in chirps.
- Database: Uses PostgreSQL to store data.
- Admin: Includes endpoints for server health, metrics, and data reset.
- Access Tokens (JWTs) are short-lived and expire after 1 hour.
- Refresh Tokens are long-lived and expire after 60 days if not used. They can be revoked via the
/api/revokeendpoint.
The main API endpoints include:
POST /api/login: User loginPOST /api/refresh: Refresh JWT tokenPOST /api/revoke: Revoke JWT tokenGET /api/chirps: Retrieve chirps (can be sorted and filtered by author)POST /api/chirps: Create a new chirpGET /api/chirps/{chirpID}: Get a specific chirpDELETE /api/chirps/{chirpID}: Delete a chirpPOST /api/users: Create a new userPUT /api/users: Update user informationPOST /api/polka/webhooks: Webhook for external service integration (user upgrades)GET /api/healthz: Server readiness checkGET /admin/metrics: View application metrics (Shows how many times the Chirpy file server at /app/ has been visited since the server started).POST /admin/reset: Reset application data (metrics)
Chirpy uses a PostgreSQL database with the following main tables:
Stores user account information.
| Column | Type | Constraints | Description |
|---|---|---|---|
id |
UUID | PRIMARY KEY | Unique identifier for the user |
created_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of user creation |
updated_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of last user update |
email |
TEXT | NOT NULL, UNIQUE | User's email address |
hashed_password |
TEXT | NOT NULL | Hashed password for the user |
is_chirpy_red |
BOOL | NOT NULL, DEFAULT false | Indicates if the user has "Chirpy Red" status |
Stores the chirps (posts) made by users.
| Column | Type | Constraints | Description |
|---|---|---|---|
id |
UUID | PRIMARY KEY | Unique identifier for the chirp |
created_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of chirp creation |
updated_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of last chirp update |
body |
TEXT | NOT NULL | Content of the chirp (max 140 chars enforced by API) |
user_id |
UUID | NOT NULL, FOREIGN KEY (users.id) ON DELETE CASCADE | ID of the user who posted the chirp |
Stores refresh tokens used to obtain new access tokens.
| Column | Type | Constraints | Description |
|---|---|---|---|
token |
TEXT | PRIMARY KEY | The refresh token string |
created_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of token creation |
updated_at |
TIMESTAMP | NOT NULL, DEFAULT CURRENT_TIMESTAMP | Timestamp of last token update |
user_id |
UUID | NOT NULL, FOREIGN KEY (users.id) ON DELETE CASCADE | ID of the user this token belongs to |
expires_at |
TIMESTAMP | NOT NULL, DEFAULT (creation + 60 days) | Timestamp when the token expires |
revoked_at |
TIMESTAMP | NULL | Timestamp if the token has been revoked |
Powered by Go!