42 lines
1.0 KiB
Rust
42 lines
1.0 KiB
Rust
mod global;
|
|
mod models;
|
|
mod routes;
|
|
|
|
use axum::http::{StatusCode, Uri};
|
|
use axum::response::IntoResponse;
|
|
use axum::routing::get;
|
|
use dotenvy::dotenv;
|
|
use sqlx::SqlitePool;
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
if let Err(e) = dotenv() {
|
|
println!("WARN: did not read .env file: {}", e);
|
|
}
|
|
|
|
let database_url = std::env::var("DATABASE_URL").unwrap();
|
|
let db_pool = SqlitePool::connect(&database_url).await.unwrap();
|
|
let state = global::AppState {
|
|
db_pool: db_pool.into(),
|
|
};
|
|
|
|
let app = axum::Router::new()
|
|
.fallback(handle404)
|
|
.route(
|
|
"/tasks",
|
|
get(routes::tasks::list).post(routes::tasks::create),
|
|
)
|
|
.with_state(state);
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
|
.await
|
|
.unwrap();
|
|
let server = axum::serve(listener, app);
|
|
println!("Listening on http://127.0.0.1:3000");
|
|
server.await.unwrap();
|
|
}
|
|
|
|
async fn handle404(uri: Uri) -> impl IntoResponse {
|
|
(StatusCode::NOT_FOUND, format!("URI: {} Not Found", uri))
|
|
}
|