-
How to share SQLite connection ? mod utils;
mod jwt;
mod stipe;
use axum::{
routing::{get, post},
response::IntoResponse,
Router,
extract::{Extension}, Json,
};
use std::net::SocketAddr;
use rusqlite::{params, Connection};
struct Person {
name: String,
place: String,
}
#[tokio::main]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();
// Initialise Database
let conn = Connection::open("kanna.db").unwrap();
init_db(&conn).unwrap();
let app = Router::new()
.route("/", get(root))
.route("/api/create", post(create))
// How to share connection?
.layer(Extension(&conn));
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
println!("running at http://0.0.0.0:8080");
tracing::info!("listening on {}", addr);
axum::Server::bind(&addr).serve(app.into_make_service()).await.unwrap();
}
fn init_db(conn: &Connection) -> Result<(), rusqlite::Error> {
dbg!("Initialising sqlite database");
let sql_schema = include_str!("schema.sql");
conn.execute(sql_schema, [])?;
Ok(())
}
// This is not working
async fn create(Extension(conn): Extension<&Connection>, Json(p): Json<Person>) -> impl IntoResponse {
conn.execute(
"INSERT INTO profile (name, place) VALUES (?1, ?2)",
params![&p.name, &p.place],
);
}
// basic handler that responds with a static string
async fn root() -> impl IntoResponse {
"Hello, World!"
} Thanks |
Beta Was this translation helpful? Give feedback.
Answered by
ekanna
Apr 25, 2022
Replies: 2 comments
-
https://discord.com/channels/500028886025895936/870760546109116496/968125519679868939 tldr: rusqlite doesn't support parallel requests. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ekanna
-
Async rusqlite built on top of rusqlite. This is working really nice. Big thanks to @programatik29 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://discord.com/channels/500028886025895936/870760546109116496/968125519679868939
tldr: rusqlite doesn't support parallel requests.