Initial commit

This commit is contained in:
danielvici123
2026-06-08 21:23:53 +02:00
commit 56d3b0f9e0

111
README.md Normal file
View File

@@ -0,0 +1,111 @@
# DHL - Simple HTTP/HTTPS Mock Server
DHL is a lightweight, configurable HTTP/HTTPS mock server built with Rust and Axum. It allows you to define multiple routes that return static JSON responses from files.
## Features
- **Live Configuration Reloading**: Changes to `config.json` (routes, status codes, etc.) and response files are applied immediately without restarting the server.
- **Multi-Route Support**: Define as many paths as you need.
- **JSON Response Files**: Responses are loaded from external JSON files.
- **Request Logging**: Detailed logging of incoming requests, including methods, paths, and headers.
- **Header Masking**: Sensitive headers like `Authorization` can be masked in logs.
- **HTTPS/TLS Support**: Optional TLS support for secure connections.
- **Automatic Default Generation**: Creates default configuration and response files if they are missing.
- **Custom 404 Handler**: Returns a standard `{"error": "Not Found"}` JSON for unknown paths.
## Configuration (`config.json`)
The server is configured via a `config.json` file in the root directory.
### Example Configuration
```json
{
"host": "127.0.0.1",
"port": 3000,
"routes": [
{
"path": "/",
"response_file": "response.json",
"status_code": 200
},
{
"path": "/api/status",
"response_file": "status.json",
"status_code": 201
}
],
"masking": {
"enabled": true,
"headers": ["authorization", "x-api-key", "cookie"]
},
"tls": {
"enabled": false,
"cert_path": "cert.pem",
"key_path": "key.pem"
}
}
```
### Fields
| Field | Type | Description |
| :--- | :--- | :--- |
| `host` | String | The IP address to bind to (e.g., `127.0.0.1` or `0.0.0.0`). |
| `port` | Number | The port to listen on. |
| `routes` | Array | A list of route configuration objects (see below). |
| `masking` | Object | Configuration for masking sensitive headers in logs. |
| `tls` | Object | Configuration for HTTPS/TLS support. |
#### Route Object
| Field | Type | Description |
| :--- | :--- | :--- |
| `path` | String | The URL path (e.g., `/` or `/api/data`). |
| `response_file` | String | Path to the JSON file containing the response body. |
| `status_code` | Number | The HTTP status code to return (e.g., `200`, `201`, `400`). |
#### Masking Object
| Field | Type | Description |
| :--- | :--- | :--- |
| `enabled` | Boolean | Whether to enable header masking. |
| `headers` | Array | List of header names (case-insensitive) to mask in the logs. |
#### TLS Object
| Field | Type | Description |
| :--- | :--- | :--- |
| `enabled` | Boolean | Whether to enable HTTPS. |
| `cert_path` | String | Path to the PEM-encoded TLS certificate file. |
| `key_path` | String | Path to the PEM-encoded TLS private key file. |
## Usage
### Running Locally
Ensure you have [Rust](https://www.rust-lang.org/) installed.
```bash
cargo run
```
### Building for Release
```bash
cargo build --release
```
### CI/CD
This project includes a Gitea Actions workflow that automatically builds the binary when a version tag (e.g., `v1.0.0`) is pushed to the repository.
## Project Structure
The code is modularized for better maintainability:
- `src/main.rs`: Entry point and server initialization.
- `src/config.rs`: Configuration loading and structures.
- `src/handlers.rs`: Route handling logic and fallback.
- `src/middleware.rs`: Request logging middleware.
- `src/state.rs`: Shared application state.