read only
This commit is contained in:
parent
7cc91285a0
commit
d4db9155a6
7 changed files with 168 additions and 4 deletions
46
src/main.rs
46
src/main.rs
|
|
@ -1,14 +1,52 @@
|
|||
use axum::{response::Html, routing::get, Router};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use axum::{extract::State, response::Html, routing::get, Router};
|
||||
use rusqlite::Connection;
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
tasks: Vec<Task>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct Task {
|
||||
id: i32,
|
||||
name: String,
|
||||
worth: f32,
|
||||
times_completed: i32,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/", get(handler));
|
||||
let db_connecion_str = "./tasks.db".to_string();
|
||||
let conn = Connection::open(db_connecion_str).unwrap();
|
||||
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
|
||||
let conn = Arc::new(Mutex::new(conn));
|
||||
|
||||
let app = Router::new().route("/", get(root)).with_state(conn);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
|
||||
println!("listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn handler() -> Html<&'static str> {
|
||||
Html("<h1>Hello, World!</h1>")
|
||||
async fn root(
|
||||
State(conn): State<Arc<Mutex<Connection>>>
|
||||
) -> Html<String> {
|
||||
let conn = conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT id, name, worth, times_completed FROM task").unwrap();
|
||||
let tasks = stmt.query_map((), |row| {
|
||||
Ok(Task {
|
||||
id: row.get(0).unwrap(),
|
||||
name: row.get(1).unwrap(),
|
||||
worth: row.get(2).unwrap(),
|
||||
times_completed: row.get(3).unwrap(),
|
||||
})
|
||||
}).unwrap().collect::<Result<_, _>>().unwrap();
|
||||
let index = IndexTemplate {tasks};
|
||||
Html(
|
||||
index.render().unwrap()
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue