Chirpy is a sample HTTP server built with Go. It supports user authentication, chirp posting, and includes administrative endpoints for metrics and system resets.
- Features
- Requirements
- Installation
- Environment Variables
- Database Migrations
- Running the Server
- API Endpoints
- Testing
- License
- User creation, login, and update using JWT authentication.
- Post chirps (short messages) with creation, retrieval, and deletion endpoints.
- Refreshing and revoking JWT tokens.
- Admin endpoints to check the server metrics and reset the hit counter.
- Webhook support for external events.
- Go 1.23
- PostgreSQL
- Goose for handling database migrations
- sqlc for handling generate type-safe code from SQL
-
Clone the repository:
git clone https://github.com/marcuschui2022/go-http-server.git cd go-http-server
-
Install dependencies:
go mod tidy
-
Setup the PostgreSQL database and create a database named
chirpy
. -
Set up environment variables:
Copy the example below into a
.env
file at the root of the project.
Create a .env
file at the root with the following configuration (replace placeholders with your actual values):
PLATFORM=dev
DB_URL=postgres://<USER>:<PASSWORD>@localhost:5432/chirpy
JWT_SECRET=<your_jwt_secret>
POLKA_KEY=<your_api_key>
Database migrations are managed with goose. To apply or revert migrations, follow these steps:
-
Navigate to the SQL schema directory:
cd sql/schema
-
Migrate Up:
goose postgres "postgres://<USER>:<PASSWORD>@localhost:5432/chirpy" up
-
Migrate Down:
goose postgres "postgres://<USER>:<PASSWORD>@localhost:5432/chirpy" down
-
Write queries in SQL file :
cd sql/queries touch <name>.sql
5generate type-safe code form SQL:
sqlc generate
From the project root, run:
go run .
By default, the server listens on port 8080. You can change this in the source if needed.
- Create User:
POST /api/users
- Update User:
PUT /api/users
- Login:
POST /api/login
- Refresh Token:
POST /api/refresh
- Revoke Token:
POST /api/revoke
- Create Chirp:
POST /api/chirps
- Retrieve Chirps:
GET /api/chirps
- Optional query parameter
author_id
(UUID) to filter chirps by user. - Optional query parameter
sort
to specify the order in which results are returned. Useasc
for ascending order (default) ordesc
for descending order.
- Optional query parameter
- Retrieve Single Chirp:
GET /api/chirps/{chirpID}
- Delete Chirp:
DELETE /api/chirps/{chirpID}
- Reset Hits:
POST /admin/reset
(only available in dev mode) - Metrics:
GET /admin/metrics
- Polka Webhook:
POST /api/polka/webhooks
Run the test suite with the following command:
go test ./...
This project is licensed under the MIT License.