surrealpatch/lib/examples/axum/src/main.rs
Flint6872 d4cc3272da
Upgrade Axum example to Axum 0.7.2 (#3172)
Co-authored-by: Rushmore Mushambi <rushmore@surrealdb.com>
2023-12-18 12:53:25 +00:00

36 lines
848 B
Rust

mod error;
mod person;
use axum::routing::{delete, get, post, put};
use axum::Router;
use surrealdb::engine::remote::ws::Ws;
use surrealdb::opt::auth::Root;
use surrealdb::Surreal;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db = Surreal::new::<Ws>("localhost:8000").await?;
db.signin(Root {
username: "root",
password: "root",
})
.await?;
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?;
axum::serve(listener, router).await?;
Ok(())
}