Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ parking_lot = "0.12.4"

[dev-dependencies]
tonic-build = { version = "0.12.3", features = ["prost"] }
actix-web = "4.12.1"
axum = "0.8.7"
rocket = { version = "0.5.1", features = ["json"] }
serde_json = "1.0.128"
Comment on lines +34 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also feature gate these examples so that we don't have to pull in these dependencies by default.

Alternatively we can add a new crate dedicated for this example, so that it's isolated from the main project. That may actually be better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an excellent idea. The examples will also be more concrete, complete, and will make greater use of features.


[features]
default = ["download_snapshots", "serde", "generate-snippets"]
Expand All @@ -47,6 +51,18 @@ required-features = ["serde"]
name = "deprecated_search"
required-features = ["serde"]

[[example]]
name = "actix_web"
required-features = ["serde"]

[[example]]
name = "axum"
required-features = ["serde"]

[[example]]
name = "rocket"
required-features = ["serde"]

[package.metadata.docs.rs]
features = ["download_snapshots", "serde"]
no-default-features = true
53 changes: 53 additions & 0 deletions examples/actix_web.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().body(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().body(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
}
39 changes: 39 additions & 0 deletions examples/axum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::sync::Arc;

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>>,
) -> Json<Value> {
match client.list_collections().await {
Ok(collections) => {
let names: Vec<String> = collections
.collections
.into_iter()
.map(|c| c.name)
.collect();
Json(json!({ "collections": names }))
}
Err(e) => Json(json!({"error": e.to_string()})),
}
}

#[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();
}
32 changes: 32 additions & 0 deletions examples/rocket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[macro_use]
extern crate rocket;
use qdrant_client::Qdrant;
use rocket::serde::json::Json;
use rocket::State;
use serde_json::{json, Value};

#[get("/collections")]
async fn list_collections(client: &State<Qdrant>) -> Json<Value> {
match client.list_collections().await {
Ok(collections) => {
let names: Vec<String> = collections
.collections
.into_iter()
.map(|c| c.name)
.collect();
Json(json!({ "collections": names }))
}
Err(e) => 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])
}