Skip to content

katanacap/klickhouse-example

Repository files navigation

Klickhouse Example + Actix Web 🚀

License: MIT Rust Version Actix Web ClickHouse CI Status

A sample Actix Web application demonstrating integration with ClickHouse using the klickhouse library. This example covers essential aspects of building a web service in Rust, including routing, middleware, and database connectivity.


Features ✨

  1. Actix Web

    • Route handling (e.g., GET, POST)
    • Custom middleware for logging and error handling
  2. ClickHouse Integration

    • Reading from and writing to ClickHouse
    • Example table (web_server_logs) to store and retrieve logs
  3. Logging Middleware

    • Captures HTTP request/response details and saves them into ClickHouse
  4. Error & Panic Handling

    • Demonstrates capturing errors and panics within the Actix Web middleware stack

Getting Started

Prerequisites 🛠️

Installation

  1. Clone the repository:

    git clone https://github.com/your-org/klickhouse-example.git
    cd klickhouse-example
  2. Run the application:

    direnv allow
    devenv up // for running clickhouse as a service
    cargo run migrate // to run clickhouse migrations
    cargo run serve // to run web the server
  3. Access the application:

    • Open your browser and navigate to http://localhost:1337/health to check if the application is running.
    • Open your browser and navigate to http://localhost:1337/logs?limit=10&offset=0 to view the logs.
  4. Run tests + all rust checks:

    // cargo test/fmt/clippy/audit/outdated
    just
    // only tests
    just test

API Endpoints 📡

Endpoint Method Description Example
/ GET Demo endpoint with request ID curl http://localhost:1337
/health GET Healthcheck curl http://localhost:1337/health
/logs GET Get paginated logs curl "http://localhost:1337/logs?limit=5&offset=0"
/fail GET Trigger handle panic example curl http://localhost:1337/fail

CLI Commands 🛠️

  • cargo run migrate: Run ClickHouse migrations.
  • cargo run serve: Run the web server.

Configuration 🛠️

  • confik.toml: Configuration file for the application.
  • .env: Environment variables for the application (optional).

Docker build 🐳

# x86_64
docker build -t klickhouse-example .

# ARM64
docker build -t klickhouse-example -f aarch64.Dockerfile .

Contributing 🤝

Feel free to open issues or PRs to improve this example — whether adding features or refining best practices. All suggestions are welcome!

License 📄

This project is open-sourced under the MIT License - see the LICENSE file for details.

About

Klickhouse integration example with actix-web app

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published