Fix the Axum example (#4793)
This commit is contained in:
parent
3531435d15
commit
c173cd9325
1 changed files with 17 additions and 5 deletions
|
@ -2,24 +2,36 @@ use axum_example::create_router;
|
||||||
use std::env;
|
use std::env;
|
||||||
use surrealdb::engine::any;
|
use surrealdb::engine::any;
|
||||||
use surrealdb::opt::auth::Root;
|
use surrealdb::opt::auth::Root;
|
||||||
|
use surrealdb::opt::Config;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
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 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",
|
username: "root",
|
||||||
password: "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?;
|
db.use_ns("namespace").use_db("database").await?;
|
||||||
|
|
||||||
|
// Configure and start the Axum server.
|
||||||
let listener = TcpListener::bind("localhost:8080").await?;
|
let listener = TcpListener::bind("localhost:8080").await?;
|
||||||
let router = create_router(db);
|
let router = create_router(db);
|
||||||
|
|
||||||
axum::serve(listener, router).await?;
|
axum::serve(listener, router).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue