{"id":27173041,"url":"https://github.com/sumit03guha/restful-axum","last_synced_at":"2026-05-02T06:36:45.461Z","repository":{"id":284421602,"uuid":"946165654","full_name":"sumit03guha/restful-axum","owner":"sumit03guha","description":"This project is a personal, self-directed learning endeavor built with Rust and Axum. It’s designed as a sandbox to experiment with asynchronous programming, robust routing, and error handling—all in a type-safe and performance-oriented environment.","archived":false,"fork":false,"pushed_at":"2025-04-03T18:26:42.000Z","size":71,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-03T19:32:01.202Z","etag":null,"topics":["axum","mongodb","rest-api","restful-api","rust","serde","serde-json","tokio-rs"],"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/sumit03guha.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}},"created_at":"2025-03-10T18:00:36.000Z","updated_at":"2025-04-03T18:26:46.000Z","dependencies_parsed_at":"2025-03-26T00:01:49.239Z","dependency_job_id":null,"html_url":"https://github.com/sumit03guha/restful-axum","commit_stats":null,"previous_names":["sumit03guha/restful-axum"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sumit03guha%2Frestful-axum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sumit03guha%2Frestful-axum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sumit03guha%2Frestful-axum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sumit03guha%2Frestful-axum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sumit03guha","download_url":"https://codeload.github.com/sumit03guha/restful-axum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248027389,"owners_count":21035594,"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":["axum","mongodb","rest-api","restful-api","rust","serde","serde-json","tokio-rs"],"created_at":"2025-04-09T10:49:59.192Z","updated_at":"2026-05-02T06:36:40.434Z","avatar_url":"https://github.com/sumit03guha.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rust Axum API Playground\n\nThis project is a personal, self-directed learning endeavor built with Rust and Axum. It serves as a sandbox to experiment with asynchronous programming, robust routing, structured error handling, and secure authentication—all within a type-safe, high-performance environment. Use this project to explore key web development concepts, build practical RESTful APIs, and sharpen your Rust skills.\n\n---\n\n## Features\n\n- **Asynchronous Programming:** Built using Tokio for concurrent operations.\n- **Robust Routing:** Leverages Axum's routing system to design a RESTful API.\n- **Type Safety:** Utilizes Rust’s strong type system to catch errors at compile time.\n- **Structured Error Handling:** Consistent JSON responses via an `ApiResponse` wrapper.\n- **MongoDB Integration:** Uses MongoDB as the datastore via the official Rust driver.\n- **Authentication \u0026 Authorization:** Secure endpoints with JWT and password hashing (Argon2).\n- **Centralized Configuration:** Loads environment variables from a `.env` file using a dedicated configuration module.\n- **Middleware Support:** Implements custom middleware for JWT authentication.\n\n---\n\n## Configuration\n\nThe application uses a dedicated `config` module to load environment variables via [`dotenvy`](https://crates.io/crates/dotenvy) and [`once_cell`](https://crates.io/crates/once_cell). Global configuration values are defined in `config.rs` and can be imported throughout the app.\n\n### Environment Variables\n\nThe following environment variables must be set in your `.env` file:\n\n- `SECRET_KEY` – The secret key used for JWT encoding.\n- `HOST` – The host on which the API server will run (e.g., `0.0.0.0`).\n- `PORT` – The port on which the API server will listen (e.g., `3000`).\n- `MONGO_URI` – The connection URI for your MongoDB instance (e.g., `mongodb://localhost:27017/`).\n\nExample `.env` file:\n\n```env\nSECRET_KEY=your_secret_key_here\nHOST=0.0.0.0\nPORT=3000\nMONGO_URI=mongodb://localhost:27017/\n```\n\nThe configuration is loaded at the beginning of the `main` function by calling `load_dotenv()`, making these values available globally.\n\n---\n\n## Routes and Endpoints\n\nThe API is split into **public endpoints** (accessible without authentication) and **protected endpoints** (which require a valid JWT token). For protected endpoints, include the token in the `Authorization` header using the format:\n\n```sh\nAuthorization: Bearer \u003cJWT_TOKEN\u003e\n```\n\n### Public Endpoints\n\n#### GET `/`\n\n- **Description:**  \n  Returns a simple \"Hello World\" message as a health check.\n- **Method:** GET\n- **Response:**  \n  - **Status:** 200 OK  \n  - **Body:**\n\n    ```plain\n    Hello World\n    ```\n\n#### POST `/signup`\n\n- **Description:**  \n  Registers a new user by accepting an email and password. The password is securely hashed using Argon2.\n- **Method:** POST\n- **Request Body Example:**\n\n  ```json\n  {\n    \"email\": \"user@example.com\",\n    \"password\": \"your_password\"\n  }\n  ```\n\n- **Response:**  \n  - **Status:** 201 Created  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Auth created\",\n      \"data\": \"INSERTED_ID_HERE\"\n    }\n    ```\n\n#### POST `/login`\n\n- **Description:**  \n  Authenticates a user with email and password. On success, returns a JWT token.\n- **Method:** POST\n- **Request Body Example:**\n\n  ```json\n  {\n    \"email\": \"user@example.com\",\n    \"password\": \"your_password\"\n  }\n  ```\n\n- **Response:**  \n  - **Status:** 200 OK  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"You are logged in\",\n      \"data\": \"JWT_TOKEN_HERE\"\n    }\n    ```\n\n---\n\n### Protected Endpoints\n\n*These endpoints require a valid JWT token in the `Authorization` header.*\n\n#### GET `/protected`\n\n- **Description:**  \n  A sample endpoint that greets the authenticated user.\n- **Method:** GET\n- **Response:**  \n  - **Status:** 200 OK  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Hello. You are logged in using user@example.com\",\n      \"data\": {}\n    }\n    ```\n\n#### Identity CRUD Operations\n\n##### POST `/identity`\n\n- **Description:**  \n  Creates a new identity record. The JSON payload must include `name` and `age`.\n- **Method:** POST\n- **Request Body Example:**\n\n  ```json\n  {\n    \"name\": \"Alice\",\n    \"age\": 30\n  }\n  ```\n\n- **Response:**  \n  - **Status:** 201 Created  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Identity created\",\n      \"data\": \"INSERTED_ID_HERE\"\n    }\n    ```\n\n##### GET `/identity`\n\n- **Description:**  \n  Retrieves a list of all identities in the database.\n- **Method:** GET\n- **Response:**  \n  - **Status:** 200 OK  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Fetched all identities\",\n      \"data\": [\n        {\n          \"_id\": \"60b8d6c5f1a8d23d4c8f4e1a\",\n          \"name\": \"Alice\",\n          \"age\": 30\n        },\n        {\n          \"_id\": \"60b8d6d9f1a8d23d4c8f4e1b\",\n          \"name\": \"Bob\",\n          \"age\": 25\n        }\n      ]\n    }\n    ```\n\n##### GET `/identity/{id}`\n\n- **Description:**  \n  Retrieves a specific identity by its MongoDB ObjectId.\n- **Method:** GET\n- **URL Parameter:**  \n  - `id`: The MongoDB ObjectId of the identity.\n- **Response:**  \n  - **Status:**  \n    - **200 OK** if found  \n    - **404 Not Found** if the identity does not exist  \n  - **Body Example (Found):**\n\n    ```json\n    {\n      \"message\": \"Fetched\",\n      \"data\": {\n        \"_id\": \"60b8d6c5f1a8d23d4c8f4e1a\",\n        \"name\": \"Alice\",\n        \"age\": 30\n      }\n    }\n    ```\n\n##### PATCH `/identity/{id}`\n\n- **Description:**  \n  Partially updates an existing identity. At least one field (`name` or `age`) must be provided.\n- **Method:** PATCH\n- **URL Parameter:**  \n  - `id`: The MongoDB ObjectId of the identity.\n- **Request Body Example:**\n\n  ```json\n  {\n    \"name\": \"Alice Smith\"\n  }\n  ```\n\n- **Response:**  \n  - **Status:**  \n    - **200 OK** if updated (or no changes were made)  \n    - **404 Not Found** if the identity does not exist  \n    - **400 Bad Request** if neither field is provided  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Updated\", // or \"No changes made\" or \"Document not found\"\n      \"data\": null\n    }\n    ```\n\n##### DELETE `/identity/{id}`\n\n- **Description:**  \n  Deletes an identity record based on its MongoDB ObjectId.\n- **Method:** DELETE\n- **URL Parameter:**  \n  - `id`: The MongoDB ObjectId of the identity.\n- **Response:**  \n  - **Status:**  \n    - **200 OK** if deletion was successful  \n    - **404 Not Found** if the identity does not exist  \n  - **Body:**\n\n    ```json\n    {\n      \"message\": \"Deleted\", // or \"Document not found\"\n      \"data\": null\n    }\n    ```\n\n---\n\n## Running the Project\n\n### Prerequisites\n\n- **Rust Toolchain:** Install from [rustup.rs](https://rustup.rs/)\n- **MongoDB:** Ensure you have a running instance (default URI: `mongodb://localhost:27017/`)\n- **Cargo:** Rust’s package manager\n\n### Installation\n\n1. **Clone the Repository:**\n\n   ```bash\n   git clone https://github.com/yourusername/rust-axum-api-playground.git\n   cd rust-axum-api-playground\n   ```\n\n2. **Set Up Environment Variables:**\n\n   Create a `.env` file in the root directory with the following contents:\n\n   ```env\n   SECRET_KEY=your_secret_key_here\n   HOST=0.0.0.0\n   PORT=3000\n   MONGO_URI=mongodb://localhost:27017/\n   ```\n\n3. **Build and Run:**\n\n   ```bash\n   cargo run\n   ```\n\n   The server will start on the host and port specified in your `.env` file (e.g., `0.0.0.0:3000`). Test the endpoints using tools like `curl`, Postman, or your preferred REST client.\n\n---\n\n## Project Structure\n\n- **Main File:** Contains the Axum server setup, router composition, and main function.\n- **Configuration:**  \n  - `config.rs` loads the `.env` file and exposes global configuration values (`SECRET_KEY`, `HOST`, `PORT`, `MONGO_URI`) via lazy statics.\n- **Route Handlers:** Functions for Identity CRUD operations and authentication (signup/login).\n- **Middleware:** Custom `login_required` middleware to enforce JWT authentication on protected endpoints.\n- **Data Models:** Structs (`Identity`, `Auth`, etc.) using Serde for serialization/deserialization.\n- **Database Integration:** Uses the official MongoDB Rust driver for database operations.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsumit03guha%2Frestful-axum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsumit03guha%2Frestful-axum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsumit03guha%2Frestful-axum/lists"}