From c173cd9325a2c96d94da0fcfb6a9e2d1b8237a4c Mon Sep 17 00:00:00 2001 From: Rushmore Mushambi Date: Tue, 17 Sep 2024 13:09:26 +0100 Subject: [PATCH] Fix the Axum example (#4793) --- sdk/examples/axum/src/main.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/sdk/examples/axum/src/main.rs b/sdk/examples/axum/src/main.rs index f398e0e0..d07e4ef8 100644 --- a/sdk/examples/axum/src/main.rs +++ b/sdk/examples/axum/src/main.rs @@ -2,24 +2,36 @@ use axum_example::create_router; use std::env; use surrealdb::engine::any; use surrealdb::opt::auth::Root; +use surrealdb::opt::Config; use tokio::net::TcpListener; #[tokio::main] async fn main() -> Result<(), Box> { + // Allow the endpoint to be configured via a `SURREALDB_ENDPOINT` environment variable + // or fallback to memory. This makes it possible to configure the endpoint at runtime. let endpoint = env::var("SURREALDB_ENDPOINT").unwrap_or_else(|_| "memory".to_owned()); - let db = any::connect(endpoint).await?; - db.signin(Root { + // Define the root user. + let root = Root { username: "root", password: "root", - }) - .await?; + }; + // Activate authentication on local engines by supplying the root user to be used. + let config = Config::new().user(root); + + // Create the database connection. + let db = any::connect((endpoint, config)).await?; + + // Sign in as root. + db.signin(root).await?; + + // Configure the namespace amd database to use. db.use_ns("namespace").use_db("database").await?; + // Configure and start the Axum server. let listener = TcpListener::bind("localhost:8080").await?; let router = create_router(db); - axum::serve(listener, router).await?; Ok(())