set up sqlite with sqlx
This commit is contained in:
parent
9397de1d33
commit
dbcac84794
4 changed files with 1438 additions and 17 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
data.js
|
data.js
|
||||||
fish_data.js
|
fish_data.js
|
||||||
|
beacon.db*
|
||||||
|
|
1436
Cargo.lock
generated
1436
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -12,5 +12,10 @@ maud = { version = "0.27.0", features = ["axum"] }
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
serde_json = "1.0.138"
|
serde_json = "1.0.138"
|
||||||
serde_path_to_error = "0.1.16"
|
serde_path_to_error = "0.1.16"
|
||||||
|
sqlx = { version = "0.8", features = [
|
||||||
|
"runtime-tokio",
|
||||||
|
"tls-rustls-ring-webpki",
|
||||||
|
"sqlite",
|
||||||
|
] }
|
||||||
tokio = { version = "1.43.0", features = ["full"] }
|
tokio = { version = "1.43.0", features = ["full"] }
|
||||||
tower-http = { version = "0.6.2", features = ["fs"] }
|
tower-http = { version = "0.6.2", features = ["fs"] }
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
|
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
|
||||||
use data::Data;
|
use data::Data;
|
||||||
use maud::Markup;
|
use maud::Markup;
|
||||||
|
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
pub mod clock;
|
pub mod clock;
|
||||||
|
@ -12,6 +13,7 @@ pub mod templates;
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub data: Data,
|
pub data: Data,
|
||||||
|
pub pool: Pool<Sqlite>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AppError(anyhow::Error);
|
pub struct AppError(anyhow::Error);
|
||||||
|
@ -42,10 +44,19 @@ async fn main_handler(state: State<Arc<AppState>>) -> Result<Markup, AppError> {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
let pool = SqlitePoolOptions::new()
|
||||||
|
.max_connections(10)
|
||||||
|
.connect("sqlite://beacon.db")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(main_handler))
|
.route("/", get(main_handler))
|
||||||
.nest_service("/static", ServeDir::new("static"))
|
.nest_service("/static", ServeDir::new("static"))
|
||||||
.with_state(Arc::new(AppState { data: Data::new() }));
|
.with_state(Arc::new(AppState {
|
||||||
|
data: Data::new(),
|
||||||
|
pool,
|
||||||
|
}));
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||||
println!("Listening on http://localhost:3000!");
|
println!("Listening on http://localhost:3000!");
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
|
|
Loading…
Add table
Reference in a new issue