X / Twitter Clone
If we removed everything other than the basics of x/twitter, we have a micro-blogging platform with the following features which we'll be implementing.
Features
- Create a post
- limit the characters per post
- optionally can be response to another post
- optionally can be response to response
- posts are stored in postgres
- post are validated before storing in database
- get a list of all top-level posts
- text
- likes
- count of immediate children
- deleted posts are ignored
- get one post
- get immediate responses to the post
- text
- likes
- deleted posts respond with a 404
- update post
- text
- delete post
- soft delete post
Tech
-
Axum v0.7.1
-
dotenvy v0.15.7
-
eyre v0.6.9
-
tokio v1.34.0
- with features
- net
- rt-multi-thread
- macros
- with features
-
tracing v0.1.40
-
tracing-subscriber v0.3.18
-
tower-http v0.5.0
- with features
- trace
- with features
-
serde v1.0.193
- with features
- derive
- with features
-
sqlx v0.7.3
- with features
- postgres
- runtime-tokio-rustls
- with features
-
tower-http v0.5.0
- with features
- cors
- with features
-
cli (use
cargo install
)- sqlx-cli v0.7.3
Setup
- Create the dotenv file by copying the .env_example to .env. On a Unix-like system you can do this with the command
cp .env_example .env
- Update the environment variables to whatever you want
Database
A Docker compose file is included to spin up a Postgres database. If you have docker installed run the command docker compose up -d
to start the database.
Connecting to the database locally
We can connect to the database directly to check it by running the psql
command in the docker container.
docker compose exec database psql -U postgres
Models
Posts
| PK | FK | Name | Type | Nullable | Default | |----|----|-----------|--------------| | | | * | * | post_id | serial | | | | | | text | varchar(255) | | | | | | parent_id | int | * | | | | | likes | int | | 0 |
Testing
The following are CURL commands to test the API. You can run them in the terminal if you have curl
installed. Otherwise you might be able to import them into your favorite API testing tool like Postman.
## create post
curl -X "POST" "http://localhost:33498/api/v1/posts" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"text": "I am a new post",
}'
## reply to a post
curl -X "POST" "http://localhost:33498/api/v1/posts" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"text": "I am a reply post",
"parent_id": 1
}'
## get all top level
curl "http://localhost:33498/api/v1/posts"
## delete post
curl -X "DELETE" "http://localhost:33498/api/v1/posts/1"
## update
curl -X "PATCH" "http://localhost:33498/api/v1/posts/4" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"text": "I have been renamed"
}'
## get one post
curl "http://localhost:33498/api/v1/posts/15"