set up sqlite with sqlx
This commit is contained in:
parent
9397de1d33
commit
dbcac84794
4 changed files with 1438 additions and 17 deletions
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 data::Data;
|
||||
use maud::Markup;
|
||||
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
pub mod clock;
|
||||
|
@ -12,6 +13,7 @@ pub mod templates;
|
|||
|
||||
pub struct AppState {
|
||||
pub data: Data,
|
||||
pub pool: Pool<Sqlite>,
|
||||
}
|
||||
|
||||
pub struct AppError(anyhow::Error);
|
||||
|
@ -42,10 +44,19 @@ async fn main_handler(state: State<Arc<AppState>>) -> Result<Markup, AppError> {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(10)
|
||||
.connect("sqlite://beacon.db")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(main_handler))
|
||||
.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();
|
||||
println!("Listening on http://localhost:3000!");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue