X/Twitter clone in Axum 0.7.1

Overview

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
  • tracing v0.1.40

  • tracing-subscriber v0.3.18

  • tower-http v0.5.0

    • with features
      • trace
  • serde v1.0.193

    • with features
      • derive
  • sqlx v0.7.3

    • with features
      • postgres
      • runtime-tokio-rustls
  • tower-http v0.5.0

    • with features
      • cors
  • cli (use cargo install)

    • sqlx-cli v0.7.3

Setup

  1. 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
  2. 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"
You might also like...
Provides json/csv/protobuf streaming support for axum

axum streams for Rust Library provides HTTP response streaming support for axum web framework: JSON array stream format JSON lines stream format CSV s

Simple example of axum, sqlx with sqlite and utoipa (swagger) - without auth

axum_crud_api Simple example to learn creating CRUD rest apis in Rust with axum, sqlx with sqlite and utoipa (swagger) - without auth Also shows how t

Heavy Metal Leptos Stack with Tailwind, Axum, Sqlite, and Cargo Leptos

Heavy Metal Stack Leptos stack with Axum, TailwindCSS, and Sqlite This example creates a basic todo app with an Axum backend that uses Leptos' server

A crate built on top of `axum-sessions`, implementing the CSRF Synchronizer Token Pattern

Axum Synchronizer Token Pattern CSRF prevention This crate provides a Cross-Site Request Forgery protection layer and middleware for use with the axum

Rate Limiting middleware for Tower/Axum/Tonic/Hyper utilizing the governor crate

A Tower service and layer that provides a rate-limiting backend by governor. Based heavily on the work done for actix-governor. Works with Axum, Hyper

An adapter to easily allow an Axum server to be run within a Cloudflare worker.

axum-cloudflare-adapter An adapter to easily allow an Axum server to be run within a Cloudflare worker. Usage use worker::*; use axum::{ response

Starter template for use with the Leptos web framework and Axum.

Leptos Axum Starter Template This is a template for use with the Leptos web framework and the cargo-leptos tool using Axum. Creating your template rep

Type safe multipart/form-data handling for axum.

axum_typed_multipart Designed to seamlessly integrate with Axum, this crate simplifies the process of handling multipart/form-data requests in your we

Axum + Connect-Web = ♥️

Axum Connect-Web ⚠️ This project isn't even Alpha state yet. Don't use it. Brings the protobuf-based Connect-Web RPC framework to Rust via idiomatic A

Owner
Brooks Builds
Welcome to Brooks Builds! Here I'm constantly learning and teaching skills around Software Development, Productivity, and Leadership.
Brooks Builds
A very basic "clone" of Reddit's r/place written in Rust using warp

r(usty)/place A very basic "clone" of r/place written in Rust using warp. Instead of rendering the image on the client side, the image is encoded as P

canta slaus 3 Dec 27, 2022
A simple clone of Waifu-pics written in Rust

really A simple clone of Waifu-pics written in Rust. Why? I've no idea. API Doc GET /api/v1/meta GET /api/v1/by/:type/:category GET /api/v1/random GET

Nik 2 Dec 18, 2022
Experiments with Rust CRDTs using Tokio web application framework Axum.

crdt-genome Synopsis Experiments with Rust CRDTs using Tokio web application framework Axum. Background Exploring some ideas of Martin Kleppmann, part

dougfort 3 Mar 18, 2022
Axum web framework tutorial for beginners.

Axum Tutorial For Beginners Hello web developers! This tutorial will cover how to write simple web applications in rust with axum framework. If you ar

Eray Karatay 46 Jan 5, 2023
Yew + Axum + blog = Yab

Yew + Axum + blog = Yab

STUDIO RSBM 13 Dec 5, 2022
Demo of Rust and axum web framework

Demo of Rust and axum web framework Demonstration of: Rust: programming language that focuses on reliability and stability. axum: web framework that f

Joel Parker Henderson 115 Dec 29, 2022
Rust Axum+SQLx Sample

rust-axum-sqlx-sample Install git clone https://github.com/web3ten0/rust-axum-sqlx-1.git cd rust-axum-sqlx-1/local docker-compose up -d sh scripts/exe

web3ten0 5 Jul 9, 2022
🪪 Session-based user authentication for Axum.

axum-login ?? Session-based user authentication for Axum. ?? Overview axum-login is a Tower middleware providing session-based user authentication for

Max Countryman 99 Jan 5, 2023
Layers, extractors and template engine wrappers for axum based Web MVC applications

axum-template Layers, extractors and template engine wrappers for axum based Web MVC applications Getting started Cargo.toml [dependencies] axum-templ

Altair Bueno 11 Dec 15, 2022
🔎 Prometheus metrics middleware for Axum

Axum-Prometheus A Prometheus middleware to collect HTTP metrics for Axum applications. axum-prometheus relies on metrics_exporter_prometheus as a back

Péter Leéh 14 Jan 4, 2023