From 66b14ed85513590f7b5ef11c4130eb082b2102a8 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Wed, 6 Nov 2024 16:19:47 +0100 Subject: [PATCH] get stuff ready for deployment! --- crates/admin-fe/src/main.rs | 79 ++++++++++++++++++++----------------- crates/backend/src/admin.rs | 47 +++++++++++++++++++++- crates/backend/src/main.rs | 2 - crates/backend/src/stat.rs | 14 ------- 4 files changed, 87 insertions(+), 55 deletions(-) diff --git a/crates/admin-fe/src/main.rs b/crates/admin-fe/src/main.rs index 5c6adc0..9ee38e3 100644 --- a/crates/admin-fe/src/main.rs +++ b/crates/admin-fe/src/main.rs @@ -1,44 +1,49 @@ -use std::io; +// use std::io; -use app::App; -use crossterm::event::{self, KeyCode, KeyEventKind}; -use ratatui::{style::Stylize, widgets::Paragraph, DefaultTerminal}; +// use app::App; +// use crossterm::event::{self, KeyCode, KeyEventKind}; +// use ratatui::{style::Stylize, widgets::Paragraph, DefaultTerminal}; fn main() -> io::Result<()> { - let mut term = ratatui::init(); - term.clear()?; + // let mut term = ratatui::init(); + // term.clear()?; - let r = App::default().run(&mut term); - ratatui::restore(); - r + // let r = App::default().run(&mut term); + // ratatui::restore(); + // r + let mut teststr = String::from("Hello! รครค"); } -mod app { - - use std::io; - - use ratatui::{DefaultTerminal, Frame}; - - #[derive(Default)] - pub struct App { - exit: bool, - } - - impl App { - pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> { - while !self.exit { - terminal.draw(|frame| self.draw(frame))?; - self.handle_events()?; - } - Ok(()) - } - - fn draw(&self, frame: &mut Frame) { - todo!() - } - - fn handle_events(&mut self) -> io::Result<()> { - todo!() - } - } +fn rev_in_place(s: &mut String) { + s.ch } + +// mod app { + +// use std::io; + +// use ratatui::{DefaultTerminal, Frame}; + +// #[derive(Default)] +// pub struct App { +// exit: bool, +// } + +// impl App { +// pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> { +// while !self.exit { +// terminal.draw(|frame| self.draw(frame))?; +// self.handle_events()?; +// } +// Ok(()) +// } + +// fn draw(&self, frame: &mut Frame) { +// todo!() +// } + +// fn handle_events(&mut self) -> io::Result<()> { +// todo!() +// } +// } +// } diff --git a/crates/backend/src/admin.rs b/crates/backend/src/admin.rs index 7c6d4b0..944d8dd 100644 --- a/crates/backend/src/admin.rs +++ b/crates/backend/src/admin.rs @@ -1,19 +1,62 @@ use axum::{ - extract::{Path, State}, + extract::{Path, Request, State}, + middleware::{self, Next}, + response::{IntoResponse, Response}, routing::get, Json, Router, }; +use http::{HeaderMap, HeaderValue, StatusCode}; use rand::distributions::{Alphanumeric, DistString}; use sqlx::{Pool, Postgres, QueryBuilder}; -use crate::{model::Chat, state::AppState}; +use crate::{markup_response::simple_error_page, model::Chat, state::AppState, ADMIN_TOK}; pub fn router(state: AppState) -> Router { Router::new() .route("/new/:amount", get(create)) + .nest("/stat", stat::router(state.clone())) + .route_layer(middleware::from_fn_with_state( + state.clone(), + auth_middleware, + )) .with_state(state) } +async fn auth_middleware( + State(state): State, + headers: HeaderMap, + req: Request, + next: Next, +) -> Response { + let admin_tok = headers.get("x-admin-tok"); + if headers.get("x-admin-tok") == Some(&HeaderValue::from_static(ADMIN_TOK)) { + let res = next.run(req).await; + return res; + } + + simple_error_page(StatusCode::UNAUTHORIZED).into_response() +} + +mod stat { + use axum::{extract::State, routing::get, Json, Router}; + + use crate::{model::Chat, state::AppState}; + + // TODO: /stat/* should require authentication + pub fn router(state: AppState) -> Router { + Router::new().route("/chats", get(chats)).with_state(state) + } + + async fn chats(State(state): State) -> Json> { + let r = sqlx::query_as!(Chat, "select * from chats;") + .fetch_all(state.pool()) + .await + .unwrap(); + + Json(r) + } +} + async fn create(Path(amount): Path, State(state): State) -> Json> { let paths: Vec = (0..amount) .map(|_| Alphanumeric.sample_string(&mut rand::thread_rng(), 6)) diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index 9da292e..b374c3a 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -10,7 +10,6 @@ mod admin; mod chat; mod markup_response; mod model; -mod stat; mod state; mod ws; @@ -28,7 +27,6 @@ async fn main() -> anyhow::Result<()> { .route("/:path", get(chat::get).post(chat::post)) .route("/poll/:msg", get(chat::poll)) .with_state(state.clone()) - .nest("/stat", stat::router(state.clone())) .nest("/admin", admin::router(state.clone())) .nest("/static", axum_static::static_router("static")); let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap(); diff --git a/crates/backend/src/stat.rs b/crates/backend/src/stat.rs index 57480aa..ee39710 100644 --- a/crates/backend/src/stat.rs +++ b/crates/backend/src/stat.rs @@ -4,17 +4,3 @@ use axum::{extract::State, routing::get, Json, Router}; use sqlx::{types::Uuid, Pool, Postgres}; use crate::{model::Chat, state::AppState}; - -// TODO: /stat/* should require authentication -pub fn router(state: AppState) -> Router { - Router::new().route("/chats", get(chats)).with_state(state) -} - -async fn chats(State(state): State) -> Json> { - let r = sqlx::query_as!(Chat, "select * from chats;") - .fetch_all(state.pool()) - .await - .unwrap(); - - Json(r) -}