From c7674f26819896ffd4b89bf7383f4152627292ae Mon Sep 17 00:00:00 2001 From: cbax Date: Tue, 8 Oct 2024 18:43:32 +0000 Subject: [PATCH] adding state, sessions, and cookies --- src/main.rs | 79 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index f9ddd96..5a705f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ #[macro_use] extern crate rocket; +use rocket::fs::FileServer; +use rocket::http::CookieJar; + pub mod channel; pub mod client; pub mod message; +#[derive(Deserialize, Serialize)] pub struct Config { admin_password: String, ratelimit: usize, @@ -15,18 +19,35 @@ pub struct Config { banned_ips: Vec, } -impl Config { - pub fn save(&self) -> bool { - todo!(); - } - pub fn load(path: &str) -> Self { - todo!(); - } +pub struct Session { + uid: Icechip, + is_admin: bool, } -pub struct State { - config: Config, - db: sled::Db, +impl Config { + pub fn save(&self, path: &str) -> bool { + let file = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .append(false) + .open(path); + let tomlstr = toml::to_string(self); + match std::fs::write(file, tomlstr) { + Ok(_) => true, + Err(_) => false + } + + } + pub fn load(path: &str) -> Self { + let file = OpenOptions::new() + .read(true) + .write(false) + .create(false) + .open(path); + let mut tomlstr = file.read_to_string(); + return toml::from_str(tomlstr.expect("Failed to parse provided config.")); + } } #[derive(Display, Serialize)] @@ -41,56 +62,68 @@ pub struct ChatResponse { message: String, } +pub fn is_admin_session(db: &sled::db, session_id: String) -> bool { + let sess: Option = match db.get(session_id) { + Ok(o) => o, + Err(_) => return false, + }; + if let Some(sess) = sess { + return sess.is_admin; + } + return false; +} #[get("/init")] -fn init() -> Json { +fn init(cookies: &CookieJar<'_>) -> Json { + cookies.get("message").map(|crumb| format!("Message: {}", crumb.value())) todo!(); } #[post("/login")] -fn login() -> Json { +fn login(cookies: &CookieJar<'_>) -> Json { todo!(); } #[post("/create_channel")] -fn create_channel() -> Json { +fn create_channel(cookies: &CookieJar<'_>) -> Json { todo!(); } #[get("/channels")] -fn channels() -> Json { +fn channels(cookies: &CookieJar<'_>) -> Json { todo!() } #[post("/join")] -fn join() -> Json { +fn join(cookies: &CookieJar<'_>) -> Json { todo!(); } #[post("/sync")] -fn sync() -> Json { +fn sync(cookies: &CookieJar<'_>) -> Json { todo!(); } #[post("/send")] -fn send() -> Json { +fn send(cookies: &CookieJar<'_>) -> Json { todo!(); } #[delete("/delete")] -fn delete() -> Json { +fn delete(cookies: &CookieJar<'_>) -> Json { todo!(); } #[post("/ban")] -fn ban() -> Json { +fn ban(cookies: &CookieJar<'_>) -> Json { todo!(); } #[launch] fn rocket() -> _ { - // load config - // create shared config state - // init database, add to shared state - rocket::build().mount("/", routes![init, login, create_channel, channels, join, sync, send, delete, ban]) + rocket::build() + .manage(Config::load(std::env::args().next().expect("Please provide a path to the application config."))) + .manage(sled::open("simpleshout").unwrap()) + .mount("/", FileServer::from("./static")) + .mount("/api", routes![init, login, create_channel, channels, join, sync, send, delete, ban]); }