A RESTful web API that implements a backend server in Golang for a twitter-like website.
Features:
- JSON data storage safely exposed in an internal database package
- User and 'chirp' creation, storage and management
- Authorization with JWT refresh and access tokens
- Subscribed users and non-subscribed users
- Middleware for web app pages
Ensure you have Go v1.22+ installed on your system.
Create a .env
file in your project root directory with the following environment variables:
JWT_SECRET=<Your JWT Secret Key>
POLKA_APIKEY=<Your Polka API Key>
From the project's root directory, use the Go command-line tool to build the executable:
go build -o chirpy
This command generates an executable named chirpy
, which starts the web API server on the specified port.
Execute the built binary:
./chirpy
POST http://localhost:<Port>/api/users
Creates a new user database entry and returns it.
- Headers: None
- Request Body:
{
"email": "<User Name>",
"password": "<User Password>"
}
- Response Body:
{
"id": "<User ID>",
"email": "<User Name>",
"is_chirpy_red": "<Boolean Value>"
}
PUT http://localhost:<Port>/api/users
Updates a user's database entry details - their email and password.
- Headers: Requires authentication header:
Authentication: Bearer <Access Token>
- Request Body:
{
"email": "<User Name>",
"password": "<User Password>"
}
- Response Body:
{
"id": "<User ID>",
"email": "<User Name>",
"is_chirpy_red": "<Boolean Value>"
}
POST http://localhost:<Port>/api/login
Authenticates a user and returns to them new access and refresh tokens.
- Headers: None
- Request Body:
{
"email": "<User Name>",
"password": "<User Password>"
}
- Response Body:
{
"id": "<User ID>",
"email": "<User Name>",
"is_chirpy_red": "<Boolean Value>",
"token": "<JWT Access Token>",
"refresh_token": "<JWT Refresh Token>"
}
POST http://localhost:<Port>/api/chirps
Creates a new chirp database entry associated with a specific user and returns it.
- Headers: Requires authentication header:
Authentication: Bearer <Access Token>
- Request Body:
{
"body": "<Chrip Message>"
}
- Response Body:
{
"id": "<Chirp ID>",
"author_id": "<User ID>",
"body": "<Chirp Message>"
}
GET http://localhost:<Port>/api/chirps
Returns a list of all chirp database entries.
Defaults sorting by ascending id. Use optional query parameter sort
to sort chirps in descending order.
For example: http://localhost:8080/api/chirps?sort=desc
Use optional query parameter author_id
to limit chirps to a specific author.
For example: http://localhost:8080/api/chirps?author_id=1
- Headers: None
- Request Body: None
- Response Body:
[
{
"id": "<Chirp ID>",
"author_id": "<User ID>",
"body": "<Chirp Message>"
}
]
GET http://localhost:<Port>/api/chirps/{id}
Returns a specific chirp database entry with the ID that is provided in the URL.
- Headers: None
- Request Body: None
- Response Body:
{
"id": "<Chirp ID>",
"author_id": "<User ID>",
"body": "<Chirp Message>"
}
DELETE http://localhost:<Port>/api/chirps/{id}
Deletes a specific chirp database entry with the ID that is provided in the URL, for an authenticated user.
- Headers: Requires authentication header:
Authentication: Bearer <Access Token>
- Request Body: None
- Response Body: None
POST http://localhost:<Port>/api/refresh
Creates a new JWT access token and returns it in the response.
- Headers: Requires authentication header:
Authentication: Bearer <Refresh Token>
- Request Body: None
- Response Body:
{
"token": "<Access Token>"
}
POST http://localhost:<Port>/api/revoke
Revokes a JWT token.
- Headers: Requires authentication header:
Authentication: Bearer <JWT Token>
- Request Body: None
- Response Body: None
POST http://localhost:<Port>/api/polka/webhooks
Webhook endpoint for polka service that updates user's subscription status.
- Headers: Requires authentication header:
Authentication: Bearer <Polka API Key>
- Request Body:
{
"event": "user.upgraded",
"data": {
"user_id": "<UserID>"
}
}
- Response Body: None
GET http://localhost:<Port>/api/healthz
Returns status of the web server.
- Headers: None
- Request Body: None
- Response Body:
OK
GET http://localhost:<Port>/admin/metrics
Resets the middleware counter for the number of the times the app pages have been viewed.
- Headers: None
- Request Body: None
- Response Body: None
Serves the web app welcome page.
Serves the web app logo.
Displays how many times an app page is viewed.