From c83928be1359567ad9fe948abf83e52329392d47 Mon Sep 17 00:00:00 2001 From: Pratim Date: Mon, 19 Feb 2024 20:41:58 +0530 Subject: [PATCH] Refactor the Axum example (#3542) --- lib/examples/axum/Cargo.toml | 2 +- lib/examples/axum/src/lib.rs | 24 ++++++++++++++++++++++++ lib/examples/axum/src/main.rs | 20 ++++++-------------- lib/examples/axum/src/person.rs | 4 ++-- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 lib/examples/axum/src/lib.rs diff --git a/lib/examples/axum/Cargo.toml b/lib/examples/axum/Cargo.toml index a90c965c..697ebfcc 100644 --- a/lib/examples/axum/Cargo.toml +++ b/lib/examples/axum/Cargo.toml @@ -7,6 +7,6 @@ publish = false [dependencies] axum = "0.7.2" serde = { version = "1.0.193", features = ["derive"] } -surrealdb = { path = "../.." } +surrealdb = { path = "../.." ,features = ["kv-mem"]} thiserror = "1.0.50" tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread"] } diff --git a/lib/examples/axum/src/lib.rs b/lib/examples/axum/src/lib.rs new file mode 100644 index 00000000..a84e0c13 --- /dev/null +++ b/lib/examples/axum/src/lib.rs @@ -0,0 +1,24 @@ +pub mod error; +pub mod person; + +use axum::{ + routing::{delete, get, post, put}, + Router, +}; +use surrealdb::engine::any::Any; +use surrealdb::Surreal; + +pub fn create_router(db: Surreal) -> Router { + Router::new() + //curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe"}' http://localhost:8080/person/1 + .route("/person/:id", post(person::create)) + //curl -X GET http://localhost:8080/person/1 + .route("/person/:id", get(person::read)) + //curl -X PUT -H "Content-Type: application/json" -d '{"name":"Jane Doe"}' http://localhost:8080/person/1 + .route("/person/:id", put(person::update)) + //curl -X DELETE http://localhost:8080/person/1 + .route("/person/:id", delete(person::delete)) + //curl -X GET http://localhost:8080/people + .route("/people", get(person::list)) + .with_state(db) +} diff --git a/lib/examples/axum/src/main.rs b/lib/examples/axum/src/main.rs index d22ce8b2..29e3cc04 100644 --- a/lib/examples/axum/src/main.rs +++ b/lib/examples/axum/src/main.rs @@ -1,16 +1,15 @@ mod error; mod person; - -use axum::routing::{delete, get, post, put}; -use axum::Router; -use surrealdb::engine::remote::ws::Ws; +use axum_example::create_router; +use std::env; +use surrealdb::engine::any; use surrealdb::opt::auth::Root; -use surrealdb::Surreal; use tokio::net::TcpListener; #[tokio::main] async fn main() -> Result<(), Box> { - let db = Surreal::new::("localhost:8000").await?; + let endpoint = env::var("SURREALDB_ENDPOINT").unwrap_or_else(|_| "memory".to_owned()); + let db = any::connect(endpoint).await?; db.signin(Root { username: "root", @@ -20,15 +19,8 @@ async fn main() -> Result<(), Box> { db.use_ns("namespace").use_db("database").await?; - let router = Router::new() - .route("/person/:id", post(person::create)) - .route("/person/:id", get(person::read)) - .route("/person/:id", put(person::update)) - .route("/person/:id", delete(person::delete)) - .route("/people", get(person::list)) - .with_state(db); - let listener = TcpListener::bind("localhost:8080").await?; + let router = create_router(db); axum::serve(listener, router).await?; diff --git a/lib/examples/axum/src/person.rs b/lib/examples/axum/src/person.rs index 6225e989..3d2c9a85 100644 --- a/lib/examples/axum/src/person.rs +++ b/lib/examples/axum/src/person.rs @@ -4,12 +4,12 @@ use axum::extract::State; use axum::Json; use serde::Deserialize; use serde::Serialize; -use surrealdb::engine::remote::ws::Client; +use surrealdb::engine::any::Any; use surrealdb::Surreal; const PERSON: &str = "person"; -type Db = State>; +type Db = State>; #[derive(Serialize, Deserialize)] pub struct Person {