Skip to content

Latest commit

 

History

History
127 lines (101 loc) · 3.04 KB

File metadata and controls

127 lines (101 loc) · 3.04 KB

Rust Chat Backend

License: MIT Rocket SQLx

A lightweight, session‑based authentication backend for web chat built with Rocket, SQLx, Argon2, and WebSockets.

Features

  • User registration with Argon2 password hashing
  • Login logout endpoints
  • Session‑based authentication using secure cookies
  • Database integration via SQLx
  • WebSocket chat support
  • /me endpoint to verify sessions
  • Clean service‑layer architecture

Tech Stack

  • Rust
  • Rocket
  • SQLx
  • Argon2
  • Serde
  • MySql
  • WebSockets

Structure

SRC
│   db.rs
│   main.rs
│
├───models
│       auth.rs
│       chat_message.rs
│       datetime.rs
│       mod.rs
│
└───routes
    │   auth.rs
    │   chat.rs
    │   history.rs
    │   mod.rs
    │
    └───services
            auth.rs
            chat.rs
            mod.rs

Configuration

Rocket Database Connecting

To connect a database to rocket, you have to create Rocket.toml
and specify address, port, URL and database name.

Example:

[default]
address = "127.0.0.1"
port = 8000

[default.databases.mysql_logs]
url = "mysql://USER:PASSWORD@localhost:3306/DB_NAME"
Note:

For full configuration, see the official guide: https://rocket.rs/guide/v0.5/state/#databases

Cargo.toml configuration

[dependencies]
rocket = {version = "0.5.1", features = ["json"] }
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.7.4", features = ["mysql", "runtime-tokio", "macros", "time", "chrono"] }
rocket_ws = "0.1.1"
chrono = "0.4.45"
argon2 = "0.5"
password-hash = "0.5"
rand_core = { version = "0.6", features = ["std"] }

[dependencies.rocket_db_pools]
version = "0.2.0"
features = ["sqlx_mysql"]
Note:

It is important to use SQLx with version 0.7.4 and "chrono" feature enabled
so the system can properly encode 'DATETIME' from database

Endpoints

POST /register

  • Handles user registration. Creates a session and user and saves it to a database

POST /login

  • Handles login requests. Creates a session and saves it to a database

POST /logout

  • Deletes session and removes cookie

GET /me

  • Confirms the user, returns username and color

GET /history

  • Returns the message history

GET /echo?channel

  • Upgrades to WebSocket connection

A word about

First I watched an interesting guide from 'Let’s Get Rusty' channel and decided
to re-implement it from scratch. After I did that, I understood that
this project is missing database persistence, a proper WebSocket connection
and other fancy things like cookies and users, so I implemented them.
Also I have to say that there is an issue with .js file, but according to me
not having knowlege of .js I let it stay that messy way I rate it 8/10 rusty tubes.

License

This project is licensed under the MIT License.

Copyright (c) 2026 LeakyDutchmann

See the LICENSE file for full details.