{"id":27966650,"url":"https://github.com/alef11/chitchatrustserver","last_synced_at":"2026-04-27T20:31:41.673Z","repository":{"id":291599056,"uuid":"972904238","full_name":"Alef11/chitchatrustserver","owner":"Alef11","description":"Small scale Rust Backend for a Chatapp. Goal is to make it as easily deployable as possible. [WIP]","archived":false,"fork":false,"pushed_at":"2025-05-06T15:13:30.000Z","size":48,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T20:55:37.996Z","etag":null,"topics":["chatapplication","mariadb","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Alef11.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-25T21:40:15.000Z","updated_at":"2025-05-06T15:13:34.000Z","dependencies_parsed_at":"2025-05-05T16:04:32.516Z","dependency_job_id":null,"html_url":"https://github.com/Alef11/chitchatrustserver","commit_stats":null,"previous_names":["alef11/chitchatrustserver"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alef11%2Fchitchatrustserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alef11%2Fchitchatrustserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alef11%2Fchitchatrustserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alef11%2Fchitchatrustserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alef11","download_url":"https://codeload.github.com/Alef11/chitchatrustserver/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252954375,"owners_count":21830902,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatapplication","mariadb","rust"],"created_at":"2025-05-07T20:55:42.638Z","updated_at":"2026-04-27T20:31:41.666Z","avatar_url":"https://github.com/Alef11.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 📨 Rust ChatApp Backend\n\nThis is the backend for a self-hostable chat application, built in Rust with modular design and future extensibility in mind.\n\n## 🔧 Features\n\n- **Modular Rust Architecture** – Clean and extensible codebase built for maintainability.\n- **Direct Messages \u0026 Group Chats** – Core messaging support for one-on-one and group communication.\n- **Self-hostable** – Easily deployable via Docker Compose.\n- **Custom `Xtime` Library** – A lightweight and user-defined time system to avoid external datetime dependencies like `chrono::DateTime`.\n- **Secure Password Handling** – Passwords are stored encrypted using SHA-256 hashing.\n- **MariaDB Integration** – User and message data is persisted in a MariaDB database.\n- **Dynamic Environment Setup** – A `.bat` script is included to prompt for and generate your `.env` file (next to the `docker-compose.yml`) for database configuration.\n\n## 🗃️ Database\n\n- User data includes:\n  - `uuid`, `username`, `email`\n  - Timestamps for `created_at` and `last_online` stored using `Xtime`\n- Messages are handled via a unified message structure that distinguishes between direct and group messages.\n\n## 🛠️ Structure Overview\n\n- `utils/xtime.rs` – Custom time system replacing `chrono::DateTime`\n- `utils/encryption.rs` – Password hashing and verification\n- `db/` – Database logic for users and message handling\n- `.env` – Created via the provided batch script for secure local setup\n\n## ⏱️ Xtime Library\n\nThe `Xtime` library was created as a custom time solution to avoid the complexities and overhead of external libraries like `chrono::DateTime`. It provides a lightweight and simplified way of handling and representing time within the application.\n\n### Why `Xtime`?\n\n- **Customizability**: The standard datetime libraries often provide too much functionality for our needs. `Xtime` is designed to keep things simple while offering enough features to handle the application’s needs.\n- **Consistency**: With `Xtime`, we use a fixed structure and format that doesn’t rely on external libraries, ensuring consistent behavior across all instances.\n- **Simplicity**: It allows easy parsing and formatting without the need for complicated third-party dependencies.\n\n### What `Xtime` Can Do\n\n- **Time Representation**: `Xtime` uses a structure with separate fields for seconds, minutes, hours, days, months, and years.\n- **Format Conversion**: It provides simple methods for converting to and from different formats, such as converting to a string for database insertion (`YYYY-MM-DD HH:mm:ss` format).\n- **Current Time**: You can generate the current time using `Xtime::now()`, which fetches the current time from the system and returns it in the custom format.\n- **Parsing**: It also supports parsing times from strings in a predefined format to ensure that no external libraries are needed to parse or format the time.\n\n## 📄 Logging System\n\nThe ChatApp backend includes a simple but powerful custom logging system that handles both runtime diagnostics and error handling. This helps track program activity and pinpoint issues without relying on external crates.\n\n### Overview\n\nAt the heart of the logger is a thread-safe, lazily-initialized global file writer. It ensures that all log messages are written to a persistent `output.log` file and echoed to the console.\n\n```rust\nstatic LOG_FILE: LazyLock\u003cMutex\u003cFile\u003e\u003e = LazyLock::new(|| {\n    // Initializes and opens the log file in append mode\n});\n```\n\n* Logs are **timestamped** using `chrono::Local::now()`.\n* Messages are formatted with `[timestamp](source_file) message`.\n* Uses `Mutex` to allow **thread-safe** writes to the log file.\n* Automatically creates `output.log` on first run.\n\n### 📲 Example Output\n\n```\n[18-05-2025 16:42:13](main.rs) Failed to initialize database: Error(ConnectionRefused)\n```\n\n### 🛠️ `log_message` Function\n\nThis function handles the actual writing to the log file:\n\n```rust\npub fn log_message(message: \u0026str, source_file: \u0026str)\n```\n\n* `message`: The message to log.\n* `source_file`: The name of the file or module (use `file!()` macro).\n\nEach message is timestamped, written to the `output.log` file, and printed to `stdout`.\n\n---\n\n## 🧠 Error Handling with `LogExpect`\n\nTo simplify error handling and ensure important errors are always logged before crashing the program, the project introduces a custom trait called `LogExpect`.\n\n### ⚖️ How It Works\n\nInstead of using:\n\n```rust\ndb::init_db().expect(\"Failed to initialize database\");\n```\n\nYou can use:\n\n```rust\ndb::init_db().log_expect(\"Failed to initialize database\", file!());\n```\n\nThis has two key benefits:\n\n1. **Logs the error** using `log_message()` before the program panics.\n2. **Includes file context** via `file!()` for easier debugging.\n\n### 🗱️ Trait Definition\n\n```rust\npub trait LogExpect\u003cT\u003e {\n    fn log_expect(self, msg: \u0026str, source_file: \u0026str) -\u003e T;\n}\n```\n\nImplemented for all `Result\u003cT, E\u003e` types where `E: Debug`. The trait:\n\n* Extracts the `Ok(T)` value normally.\n* On `Err(E)`, logs the error along with a custom message.\n* Panics immediately afterward with the same message.\n\n### 💡 Example Usage\n\n```rust\nuse crate::utils::logger::LogExpect;\n\nfn start() {\n    db::init_db().log_expect(\"Failed to initialize database\", file!());\n}\n```\n\nThis approach combines **panic handling** and **diagnostic logging** into one clean API, and helps surface critical failures during development and production.\n\n\n\n## 📦 Coming Soon\n\n- Installation \u0026 deployment guide\n- Full API documentation\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falef11%2Fchitchatrustserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falef11%2Fchitchatrustserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falef11%2Fchitchatrustserver/lists"}