|
| 1 | +# Actix Web + Klickhouse Example |
| 2 | + |
| 3 | +A sample **Actix Web** application demonstrating integration with **ClickHouse** using the [klickhouse](https://github.com/katanacap/klickhouse) library. This example covers essential aspects of building a web service in Rust, including routing, middleware, and database connectivity. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +1. **Actix Web** |
| 10 | + - Route handling (e.g., `GET`, `POST`) |
| 11 | + - Custom middleware for logging and error handling |
| 12 | + |
| 13 | +2. **ClickHouse Integration** |
| 14 | + - Reading from and writing to ClickHouse |
| 15 | + - Example table (`web_server_logs`) to store and retrieve logs |
| 16 | + |
| 17 | +3. **Logging Middleware** |
| 18 | + - Captures HTTP request/response details and saves them into ClickHouse |
| 19 | + |
| 20 | +4. **Error & Panic Handling** |
| 21 | + - Demonstrates capturing errors and panics within the Actix Web middleware stack |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Getting Started |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | +- **Devenv** (for running the application under development environment) |
| 29 | + |
| 30 | +### Installation |
| 31 | + |
| 32 | +1. **Clone the repository**: |
| 33 | + ```bash |
| 34 | + git clone https://github.com/your-org/klickhouse-example.git |
| 35 | + cd klickhouse-example |
| 36 | + ``` |
| 37 | + |
| 38 | +2. **Run the application**: |
| 39 | + ```bash |
| 40 | + direnv allow |
| 41 | + devenv up // for running clickhouse as a service |
| 42 | + cargo run migrate // to run clickhouse migrations |
| 43 | + cargo run serve // to run web the server |
| 44 | + ``` |
| 45 | + |
| 46 | +3. **Access the application**: |
| 47 | + - Open your browser and navigate to `http://localhost:1337/health` to check if the application is running. |
| 48 | + - Open your browser and navigate to `http://localhost:1337/logs?limit=10&offset=0` to view the logs. |
| 49 | + |
| 50 | + |
| 51 | +### Endpoints: |
| 52 | +- GET /: example endpoint to check request id logic |
| 53 | +- GET /logs: Fetch logs from ClickHouse (optional query params for pagination). |
| 54 | +- GET /health: Basic health check. |
| 55 | +- GET /fail: Example endpoint that triggers an error or panic for demonstration. |
| 56 | + |
| 57 | +### CLI Commands |
| 58 | +- `cargo run migrate`: Run ClickHouse migrations. |
| 59 | +- `cargo run serve`: Run the web server. |
| 60 | + |
| 61 | +### Configuration |
| 62 | +- `confik.toml`: Configuration file for the application. |
| 63 | +- `.env`: Environment variables for the application (optional). |
| 64 | + |
| 65 | +### Docker build |
| 66 | +```shell |
| 67 | +docker build -t klickhouse_example . |
| 68 | +``` |
| 69 | + |
| 70 | +### Docker build for aarch64 |
| 71 | +```shell |
| 72 | +docker build -t klickhouse-example-img -f aarch64.Dockerfile . |
| 73 | +``` |
| 74 | + |
| 75 | +## Contributing |
| 76 | + |
| 77 | +Feel free to open issues or PRs to improve this example — whether adding features or refining best practices. All suggestions are welcome! |
| 78 | + |
| 79 | +## License |
| 80 | + |
| 81 | +This project is open-sourced under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments