Set up sqlx
This commit is contained in:
parent
12ec8ac217
commit
79667c15b8
5 changed files with 980 additions and 21 deletions
938
Cargo.lock
generated
938
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -10,6 +10,7 @@ askama_axum = "0.3.0"
|
|||
axum = { version = "0.6.19", features = ["macros"] }
|
||||
mime_guess = "2.0.4"
|
||||
rust-embed = "6.8.1"
|
||||
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] }
|
||||
tokio = { version = "1.29.1", features = ["full"] }
|
||||
|
||||
[build-dependencies]
|
||||
|
|
|
|||
29
src/db.rs
Normal file
29
src/db.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// TODO Occasionally run PRAGMA optimize
|
||||
|
||||
use sqlx::{
|
||||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
|
||||
SqlitePool,
|
||||
};
|
||||
|
||||
// TODO Open db from path
|
||||
pub async fn pool() -> sqlx::Result<SqlitePool> {
|
||||
let options = SqliteConnectOptions::new()
|
||||
// https://www.sqlite.org/pragma.html#pragma_journal_mode
|
||||
.journal_mode(SqliteJournalMode::Wal)
|
||||
// https://www.sqlite.org/pragma.html#pragma_synchronous
|
||||
// NORMAL recommended when using WAL, can't cause corruption
|
||||
.synchronous(SqliteSynchronous::Normal)
|
||||
// https://www.sqlite.org/pragma.html#pragma_foreign_keys
|
||||
.foreign_keys(true)
|
||||
// https://www.sqlite.org/pragma.html#pragma_trusted_schema
|
||||
// The docs recommend always turning this off
|
||||
.pragma("trusted_schema", "false")
|
||||
.create_if_missing(true)
|
||||
.optimize_on_close(true, None);
|
||||
|
||||
let pool = SqlitePoolOptions::new().connect_with(options).await?;
|
||||
|
||||
sqlx::migrate!().run(&pool).await?;
|
||||
|
||||
Ok(pool)
|
||||
}
|
||||
31
src/main.rs
31
src/main.rs
|
|
@ -1,25 +1,34 @@
|
|||
mod db;
|
||||
mod r#static;
|
||||
|
||||
use askama::Template;
|
||||
use axum::{routing::get, Router};
|
||||
use askama_axum::{IntoResponse, Response};
|
||||
use axum::{http::StatusCode, routing::get, Extension, Router};
|
||||
use sqlx::{Row, SqlitePool};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
greetee: String,
|
||||
number: i32,
|
||||
}
|
||||
|
||||
async fn index(Extension(pool): Extension<SqlitePool>) -> Result<Response, Response> {
|
||||
let result = sqlx::query("SELECT * FROM (VALUES (1))")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")).into_response())?;
|
||||
|
||||
let number: i32 = result.get(0);
|
||||
Ok(IndexTemplate { number }.into_response())
|
||||
}
|
||||
|
||||
async fn run() -> anyhow::Result<()> {
|
||||
let pool = db::pool().await?;
|
||||
|
||||
let app = Router::new()
|
||||
.route(
|
||||
"/",
|
||||
get(|| async {
|
||||
IndexTemplate {
|
||||
greetee: "world".to_string(),
|
||||
}
|
||||
}),
|
||||
)
|
||||
.fallback(get(r#static::static_handler));
|
||||
.route("/", get(index))
|
||||
.fallback(get(r#static::static_handler))
|
||||
.layer(Extension(pool));
|
||||
// TODO Add text body to body-less status codes
|
||||
|
||||
axum::Server::bind(&"0.0.0.0:8000".parse().unwrap())
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<body>
|
||||
<h1>Hello</h1>
|
||||
<p>{{ greetee }}</p>
|
||||
<p>The number from the DB is {{ number }}, yay!</p>
|
||||
<button id="button">Click me!</button>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue