-
Notifications
You must be signed in to change notification settings - Fork 75
feat(examples): add integration examples for Actix, Axum, and Rocket #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,11 @@ parking_lot = "0.12.4" | |
| [dev-dependencies] | ||
| tonic-build = { version = "0.12.3", features = ["prost"] } | ||
|
|
||
| [workspace] | ||
| members = [ | ||
| ".", | ||
| ] | ||
|
Comment on lines
32
to
38
|
||
|
|
||
| [features] | ||
| default = ["download_snapshots", "serde", "generate-snippets"] | ||
| download_snapshots = ["reqwest", "futures-util"] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| target |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [package] | ||
| name = "web-integrations-examples" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [workspace] | ||
|
|
||
| [dependencies] | ||
| qdrant-client = { path = "../../", features = ["serde"] } | ||
| actix-web = "4.12.1" | ||
| axum = "0.8.7" | ||
| rocket = { version = "0.5.1", features = ["json"] } | ||
| serde_json = "1.0.128" | ||
| tokio = { version = "1.40.0", features = ["rt-multi-thread", "macros", "net"] } | ||
|
|
||
| [[bin]] | ||
| name = "actix_web" | ||
| path = "src/actix_web.rs" | ||
|
|
||
| [[bin]] | ||
| name = "axum" | ||
| path = "src/axum.rs" | ||
|
|
||
| [[bin]] | ||
| name = "rocket" | ||
| path = "src/rocket.rs" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use actix_web::{web, App, HttpResponse, HttpServer, Responder}; | ||
| use qdrant_client::Qdrant; | ||
| use serde_json::json; | ||
|
|
||
| struct AppState { | ||
| client: Arc<Qdrant>, | ||
| } | ||
|
|
||
| async fn list_collections(data: web::Data<AppState>) -> impl Responder { | ||
| match data.client.list_collections().await { | ||
| Ok(collections) => { | ||
| let names: Vec<String> = collections | ||
| .collections | ||
| .into_iter() | ||
| .map(|c| c.name) | ||
| .collect(); | ||
| HttpResponse::Ok().json(json!({ "collections": names })) | ||
| } | ||
| Err(e) => HttpResponse::InternalServerError().json(json!({ "error": e.to_string() })), | ||
| } | ||
| } | ||
|
|
||
| async fn health_check(data: web::Data<AppState>) -> impl Responder { | ||
| match data.client.health_check().await { | ||
| Ok(resp) => HttpResponse::Ok().json(json!({ "result": format!("{:?}", resp) })), | ||
| Err(e) => HttpResponse::InternalServerError().json(json!({ "error": e.to_string() })), | ||
| } | ||
| } | ||
|
|
||
| #[actix_web::main] | ||
| async fn main() -> std::io::Result<()> { | ||
| let client = Qdrant::from_url("http://localhost:6334") | ||
| .build() | ||
| .expect("Failed to create Qdrant client"); | ||
|
|
||
| let state = web::Data::new(AppState { | ||
| client: Arc::new(client), | ||
| }); | ||
|
|
||
| println!("Starting Actix server on http://localhost:8080"); | ||
|
|
||
| HttpServer::new(move || { | ||
| App::new() | ||
| .app_data(state.clone()) | ||
| .route("/collections", web::get().to(list_collections)) | ||
| .route("/health", web::get().to(health_check)) | ||
| }) | ||
| .bind(("127.0.0.1", 8080))? | ||
| .run() | ||
| .await | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use axum::http::StatusCode; | ||
| use axum::routing::get; | ||
| use axum::{Json, Router}; | ||
| use qdrant_client::Qdrant; | ||
| use serde_json::{json, Value}; | ||
|
|
||
| async fn list_collections( | ||
| axum::extract::State(client): axum::extract::State<Arc<Qdrant>>, | ||
| ) -> (StatusCode, Json<Value>) { | ||
| match client.list_collections().await { | ||
| Ok(collections) => { | ||
| let names: Vec<String> = collections | ||
| .collections | ||
| .into_iter() | ||
| .map(|c| c.name) | ||
| .collect(); | ||
| (StatusCode::OK, Json(json!({ "collections": names }))) | ||
| } | ||
| Err(e) => ( | ||
| StatusCode::INTERNAL_SERVER_ERROR, | ||
| Json(json!({ "error": e.to_string() })), | ||
| ), | ||
| } | ||
|
Comment on lines
12
to
25
|
||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let client = Qdrant::from_url("http://localhost:6334") | ||
| .build() | ||
| .expect("Failed to create Qdrant client"); | ||
|
|
||
| let app = Router::new() | ||
| .route("/collections", get(list_collections)) | ||
| .with_state(Arc::new(client)); | ||
|
|
||
| let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") | ||
| .await | ||
| .unwrap(); | ||
| println!("Starting Axum server on http://localhost:3000"); | ||
| axum::serve(listener, app).await.unwrap(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #[macro_use] | ||
| extern crate rocket; | ||
| use qdrant_client::Qdrant; | ||
| use rocket::http::Status; | ||
| use rocket::response::status; | ||
| use rocket::serde::json::Json; | ||
| use rocket::State; | ||
| use serde_json::{json, Value}; | ||
|
|
||
| #[get("/collections")] | ||
| async fn list_collections( | ||
| client: &State<Qdrant>, | ||
| ) -> Result<Json<Value>, status::Custom<Json<Value>>> { | ||
| match client.list_collections().await { | ||
| Ok(collections) => { | ||
| let names: Vec<String> = collections | ||
| .collections | ||
| .into_iter() | ||
| .map(|c| c.name) | ||
| .collect(); | ||
| Ok(Json(json!({ "collections": names }))) | ||
| } | ||
| Err(e) => Err(status::Custom( | ||
| Status::InternalServerError, | ||
| Json(json!({ "error": e.to_string() })), | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| #[launch] | ||
| async fn rocket() -> _ { | ||
| let client = Qdrant::from_url("http://localhost:6334") | ||
| .build() | ||
| .expect("Failed to create Qdrant client"); | ||
|
|
||
| rocket::build() | ||
| .manage(client) | ||
| .mount("/", routes![list_collections]) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a workspace with
examples/web-integrationsas a member will cause existing CI commands likecargo clippy --workspace --all-targets --all-featuresandcargo test --all(see.github/workflows/lint.ymlandtests/integration-tests.sh) to compile these new Actix/Axum/Rocket binaries. That can significantly increase CI time and can introduce new system dependency requirements. Consider keeping these examples out of the workspace (or adjusting CI commands / moving them under a non-workspace directory) so they don’t become part of the default lint/test surface.