Fix the Axum example (#4793)

This commit is contained in:
Rushmore Mushambi 2024-09-17 13:09:26 +01:00 committed by GitHub
parent 3531435d15
commit c173cd9325
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<dyn std::error::Error>> {
// 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(())