1个不稳定版本
0.1.0 | 2022年4月7日 |
---|
#267 in #axum
4KB
axum-sqlite
cargo add axum-sqlite
为axum
提供Sqlite数据库。
use axum::{Extension, response::Html, routing::get, Router};
use std::net::SocketAddr;
use axum_sqlite::*;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(index))
.layer(Database::new(":memory:").unwrap());
axum::Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
.serve(app.into_make_service())
.await
.unwrap();
}
async fn index(Extension(database): Extension<Database>) -> Html<&'static str> {
let connection = database.connection().unwrap(); // Do stuff with connection
Html("Hello, sqlite!")
}
lib.rs
:
为axum
提供Sqlite数据库。
use axum::{Extension, response::Html, routing::get, Router};
use std::net::SocketAddr;
use axum_sqlite::*;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", get(index))
.layer(Database::new(":memory:").unwrap());
axum::Server::bind(&SocketAddr::from(([127, 0, 0, 1], 3000)))
.serve(app.into_make_service())
.await
.unwrap();
}
async fn index(Extension(database): Extension<Database>) -> Html<&'static str> {
let connection = database.connection().unwrap(); // Do stuff with connection
Html("Hello, sqlite!")
}
依赖项
~29–39MB
~641K SLoC