Melangerie is a real-time chat application built with Rust, Svelte, and Rocket, a web framework for Rust. The main purpose is an in-house alternative to apps like Microsoft Teams and Skype when just used for basic communication. It allows users to create chat rooms, invite other users to join, and send messages within those rooms. The application uses WebSockets for real-time communication and JSON Web Tokens (JWT) for user authentication.
The project is structured as follows:
office-chat-rs/
├── server/
│ ├── public/
│ │ ├── index.html
│ │ └── ...
│ ├── src/
│ │ ├── auth.rs
│ │ ├── chat.rs
│ │ ├── cors.rs
│ │ ├── log.rs
│ │ ├── main.rs
│ │ ├── test.rs
│ │ └── types.rs
│ └── Cargo.toml
└── client/
└── ...
server
directory contains the Rust backend code.auth.rs
: Handles user registration, login, and JWT generation/verification.chat.rs
: Implements the WebSocket connection handling and chat room functionality.cors.rs
: Provides CORS support for the server.log.rs
: Defines the logging functionality for server events and user reports.main.rs
: The entry point of the application, setting up the Rocket server and routes.test.rs
: Contains unit tests for the application.types.rs
: Defines the data types used throughout the application.public
directory serves as the static file server, containing the HTML, CSS, and JavaScript files for the frontend.Clone the repository:s
git clone https://github.com/exotik850/melangerie.git
Change to the client
directory and install dependencies:
cd melangerie/client && npm i
Build the client for the backend to serve:
npm run build
Change to the server
directory:
cd ../server
Set up the environment variables:
.env
file in the server
directory..env
file:JWT_SECRET=your-jwt-secret
Replace your-jwt-secret
with a secure secret key for JWT generation and verification.Run the server:
cargo run
The server will start running at http://0.0.0.0:8080
.
POST /auth/login
: User login endpoint. Expects a JSON payload with name
and password
fields.POST /auth/createuser
: User registration endpoint. Expects a JSON payload with name
and password
fields.GET /auth/checkuser/<name>
: Checks if a user with the given name
exists.GET /chat/connect
: WebSocket endpoint for establishing a chat connection.GET /chat/list
: Lists the available chat rooms for the authenticated user.POST /chat/adduser/<room>/<user_id>
: Adds a user with the given user_id
to the specified room
.POST /chat/chatroom
: Sends a message to a chat room. Expects a JSON payload with sender
, room
, content
, and timestamp
fields.POST /chat/create/<name>/<users..>
: Creates a new chat room with the given name
and initial users
.GET /<file..>
: Serves static files from the public
directory.POST /report
: Endpoint for users to report issues. Expects a JSON payload with name
and issue
fields.The application uses JSON Web Tokens (JWT) for user authentication. When a user logs in or registers, a JWT is generated and sent to the client. The client must include this token in the Authorization
header for subsequent requests that require authentication.
The WebSocket endpoint (/chat/connect
) handles real-time communication between the server and clients. Upon establishing a connection, the server authenticates the user using the provided JWT. Once authenticated, the user can join chat rooms, send messages, and receive messages from other users in real-time.
The WebSocket messages are JSON-encoded and follow a specific structure defined by the UserAction
and ServerAction
enums in types.rs
.
The application includes a logging mechanism to log server events and user reports. The Log
struct in log.rs
handles writing log messages to a file named log.txt
. The server periodically flushes the log buffer to ensure that logs are persisted.
Contributions to the project are welcome! If you find any bugs or have suggestions for improvements, please open an issue or submit a pull request.
This project is licensed under the MIT License.