feat: implement initial user authentication, session management, and admin dashboard routing with
This commit is contained in:
73
src/main.rs
73
src/main.rs
@@ -1,7 +1,7 @@
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::{Html, IntoResponse},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
@@ -11,9 +11,15 @@ use std::sync::Arc;
|
||||
use tokio::net::TcpListener;
|
||||
use std::env;
|
||||
|
||||
pub mod models;
|
||||
pub mod middleware;
|
||||
pub mod utils;
|
||||
pub mod error;
|
||||
pub mod handlers;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
db: SqlitePool,
|
||||
pub struct AppState {
|
||||
pub db: SqlitePool,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
@@ -22,35 +28,25 @@ struct IndexTemplate {
|
||||
title: String,
|
||||
}
|
||||
|
||||
struct HtmlTemplate<T>(T);
|
||||
|
||||
impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self.0.render() {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(err) => (
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Failed to render template: {}", err),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn index(State(_state): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
let template = IndexTemplate {
|
||||
title: "Coming Soon".to_string(),
|
||||
};
|
||||
HtmlTemplate(template)
|
||||
crate::utils::HtmlTemplate(template)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
dotenvy::dotenv().ok();
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||
"blog_cms=debug,tower_http=debug,axum=debug".into()
|
||||
}),
|
||||
)
|
||||
.init();
|
||||
|
||||
let db_url = env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite://data.db".to_string());
|
||||
let port = env::var("PORT").unwrap_or_else(|_| "3000".to_string());
|
||||
let addr = format!("0.0.0.0:{}", port);
|
||||
@@ -65,11 +61,42 @@ async fn main() {
|
||||
.await
|
||||
.expect("Failed to connect to SQLite database");
|
||||
|
||||
// Create schema
|
||||
sqlx::query(
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
role TEXT NOT NULL DEFAULT 'readonly'
|
||||
);
|
||||
"#,
|
||||
)
|
||||
.execute(&db_pool)
|
||||
.await
|
||||
.expect("Failed to create users table");
|
||||
|
||||
sqlx::query(
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
"#,
|
||||
)
|
||||
.execute(&db_pool)
|
||||
.await
|
||||
.expect("Failed to create sessions table");
|
||||
|
||||
let app_state = Arc::new(AppState { db: db_pool });
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.with_state(app_state);
|
||||
.nest("/__dungeon", handlers::router(&app_state)) // I'll create a single `handlers::router`
|
||||
.with_state(app_state.clone())
|
||||
.layer(tower_http::trace::TraceLayer::new_for_http());
|
||||
|
||||
let listener = TcpListener::bind(&addr).await.unwrap();
|
||||
println!("Server running at http://{}", addr);
|
||||
|
||||
Reference in New Issue
Block a user