5.9 KiB
PureNotify Backend
This is the backend service for the PureNotify application, written in Rust. It's built to be a high-performance, reliable, and scalable foundation for sending notifications.
🚀 Features
- Asynchronous: Built with
tokioandaxumfor non-blocking I/O and high concurrency. - Configurable: Easily configure the application using environment variables.
- Logging: Integrated structured logging with
tracingfor better observability. - Graceful Shutdown: Ensures the server shuts down cleanly without dropping active connections.
- Health Check: A dedicated endpoint to monitor the service's health.
🛠️ Technologies Used
- Rust: The core programming language.
- Axum: A web application framework that focuses on ergonomics and modularity.
- Tokio: An asynchronous runtime for the Rust programming language.
- Serde: A framework for serializing and deserializing Rust data structures efficiently.
- Dotenvy: For loading environment variables from a
.envfile. - Tracing: A framework for instrumenting Rust programs to collect structured, event-based diagnostic information.
⚙️ Getting Started
Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.
Prerequisites
You need to have the Rust toolchain installed on your system. If you don't have it, you can install it from rustup.rs.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Installation & Running
-
Clone the repository:
git clone https://github.com/your-username/purenotify_backend.git cd purenotify_backend -
Create a
.envfile: Copy the example environment file to create your own local configuration.cp .env.example .envYou can modify the
.envfile to change the server's configuration. -
Build the project:
cargo build -
Run the application database with Docker (Recommended): If you have Docker installed
-
Run the application (Manual with Docker): First, start the MongoDB container:
docker run \ --name mongodb \ -p 27017:27017 \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password123 \ -e MONGO_INITDB_DATABASE=purenotify \ -v mongodb_data:/data/db \ mongo:latest(Note: The
purenotifydatabase name here should matchDATABASE_NAMEin your.envorconfig.rsfor the backend to connect correctly. TheMONGODB_URIfor the backend would bemongodb://127.0.0.1:27017ormongodb://localhost:27017.)Then, run the Rust application: For development, you can run the project directly with
cargo run:cargo runFor a release build, run:
cargo run --release
The server will start, and you should see log output in your terminal indicating that it's running.
🔧 Configuration
The application is configured using environment variables. These can be set in a .env file in the project root or directly in your shell.
BIND_ADDRESS: The IP address and port the server should listen on.- Default:
127.0.0.1:3000
- Default:
RUST_LOG: Controls the log level for the application.- Example:
RUST_LOG=info,purenotify_backend=debugwill set the default log level toinfoand the log level for this crate todebug. - Default: Reads from the environment; if not set, logging may be minimal.
- Example:
API Endpoints
Here are the available API endpoints for the service.
Health Check
- Endpoint:
/health - Method:
GET - Description: Used to verify that the service is running and healthy.
- Success Response:
- Code:
200 OK - Content:
{ "message": "health check successful", "data": {}, "success": true, "error": false }
- Code:
Example Usage
You can use curl to check the health of the service:
curl http://127.0.0.1:3000/health
Expected Output:
{
"message": "health check successful",
"data": {},
"success": true,
"error": false
}
📂 Project Structure
The project follows a standard Rust project layout. The main application logic is located in the src/ directory.
src/
├── main.rs # Application entry point, server setup
├── config.rs # Configuration management
├── handlers/ # Business logic for handling requests
│ └── health/
│ └── health.rs
├── routes/ # API route definitions
│ └── health/
│ └── health.rs
└── utils/ # Utility functions and shared modules
main.rs: Initializes the server, logging, configuration, and wires up the routes.config.rs: Defines theConfigstruct and handles loading configuration from the environment.handlers/: Contains the core logic for each API endpoint. Each handler is responsible for processing a request and returning a response.routes/: Defines the AxumRouterfor different parts of the application. These modules map URL paths to their corresponding handlers.utils/: A place for helper functions or modules that are used across different parts of the application.
🤝 Contributing
Contributions are welcome! If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/fooBar). - Commit your changes (
git commit -am 'Add some fooBar'). - Push to the branch (
git push origin feature/fooBar). - Create a new Pull Request.
📄 License
This project is licensed under the MIT License - see the LICENSE.md file for details.