feat: Implement basic (dummy) admin auth
This commit is contained in:
parent
b37c0665d3
commit
8c29f1d31f
13 changed files with 706 additions and 5 deletions
|
|
@ -1,11 +1,22 @@
|
|||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use axum::extract::State;
|
||||
use axum::response::Html;
|
||||
use axum::routing::{get, post};
|
||||
use axum::serve::Listener as AxumListener;
|
||||
use axum_extra::extract::cookie::CookieJar;
|
||||
use http::StatusCode;
|
||||
use minijinja::{Environment, context, path_loader};
|
||||
use static_serve::embed_assets;
|
||||
|
||||
use crate::db::{Database, DatabaseInterface};
|
||||
use crate::listener::{Listener, ListenerKind};
|
||||
use crate::stream::{AbstractSocketAddr, AbstractStreamKind};
|
||||
|
||||
mod login;
|
||||
mod logout;
|
||||
mod session;
|
||||
use session::HttpSessionManager;
|
||||
|
||||
impl AxumListener for Listener {
|
||||
type Io = AbstractStreamKind;
|
||||
type Addr = AbstractSocketAddr;
|
||||
|
|
@ -46,12 +57,51 @@ impl AxumListener for Listener {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HttpState<D: DatabaseInterface> {
|
||||
pub db: Database<D>,
|
||||
pub sessions: HttpSessionManager,
|
||||
pub templates: Environment<'static>,
|
||||
}
|
||||
|
||||
impl<D: DatabaseInterface> HttpState<D> {
|
||||
pub fn new(db: Database<D>) -> Self {
|
||||
let mut templates = Environment::new();
|
||||
templates.set_loader(path_loader("templates"));
|
||||
Self {
|
||||
db,
|
||||
sessions: HttpSessionManager::new(),
|
||||
templates,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn http_listen<D: DatabaseInterface>(listener: Listener, db: Database<D>) {
|
||||
let app = Router::new().route("/", get(handler)).with_state(db);
|
||||
embed_assets!("assets");
|
||||
let app = Router::new()
|
||||
.nest("/assets", static_router())
|
||||
.route("/", get(home))
|
||||
.route("/login", get(home))
|
||||
.route("/login", post(login::post_login))
|
||||
.route("/logout", get(logout::logout))
|
||||
.with_state(HttpState::new(db));
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn handler() -> &'static str {
|
||||
"hello world"
|
||||
pub async fn home<D: DatabaseInterface>(
|
||||
State(state): State<HttpState<D>>,
|
||||
cookies: CookieJar,
|
||||
) -> (StatusCode, Html<String>) {
|
||||
if let Some(session) = state.sessions.get_session(&cookies) {
|
||||
let page = state
|
||||
.templates
|
||||
.get_template("home.html")
|
||||
.unwrap()
|
||||
.render(context! {username => session.username})
|
||||
.unwrap();
|
||||
(StatusCode::OK, Html(page))
|
||||
} else {
|
||||
login::login_page(State(state), None).await
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue