adding state, sessions, and cookies

This commit is contained in:
cbax 2024-10-08 18:43:32 +00:00
parent f1f01c2055
commit c7674f2681

View file

@ -1,9 +1,13 @@
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
use rocket::fs::FileServer;
use rocket::http::CookieJar;
pub mod channel; pub mod channel;
pub mod client; pub mod client;
pub mod message; pub mod message;
#[derive(Deserialize, Serialize)]
pub struct Config { pub struct Config {
admin_password: String, admin_password: String,
ratelimit: usize, ratelimit: usize,
@ -15,18 +19,35 @@ pub struct Config {
banned_ips: Vec<String>, banned_ips: Vec<String>,
} }
impl Config { pub struct Session {
pub fn save(&self) -> bool { uid: Icechip<u64>,
todo!(); is_admin: bool,
}
pub fn load(path: &str) -> Self {
todo!();
}
} }
pub struct State { impl Config {
config: Config, pub fn save(&self, path: &str) -> bool {
db: sled::Db, 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)] #[derive(Display, Serialize)]
@ -41,56 +62,68 @@ pub struct ChatResponse {
message: String, message: String,
} }
pub fn is_admin_session(db: &sled::db, session_id: String) -> bool {
let sess: Option<Session> = match db.get(session_id) {
Ok(o) => o,
Err(_) => return false,
};
if let Some(sess) = sess {
return sess.is_admin;
}
return false;
}
#[get("/init")] #[get("/init")]
fn init() -> Json { fn init(cookies: &CookieJar<'_>) -> Json {
cookies.get("message").map(|crumb| format!("Message: {}", crumb.value()))
todo!(); todo!();
} }
#[post("/login")] #[post("/login")]
fn login() -> Json { fn login(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[post("/create_channel")] #[post("/create_channel")]
fn create_channel() -> Json { fn create_channel(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[get("/channels")] #[get("/channels")]
fn channels() -> Json { fn channels(cookies: &CookieJar<'_>) -> Json {
todo!() todo!()
} }
#[post("/join")] #[post("/join")]
fn join() -> Json { fn join(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[post("/sync")] #[post("/sync")]
fn sync() -> Json { fn sync(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[post("/send")] #[post("/send")]
fn send() -> Json { fn send(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[delete("/delete")] #[delete("/delete")]
fn delete() -> Json { fn delete(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[post("/ban")] #[post("/ban")]
fn ban() -> Json { fn ban(cookies: &CookieJar<'_>) -> Json {
todo!(); todo!();
} }
#[launch] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
// load config rocket::build()
// create shared config state .manage(Config::load(std::env::args().next().expect("Please provide a path to the application config.")))
// init database, add to shared state .manage(sled::open("simpleshout").unwrap())
rocket::build().mount("/", routes![init, login, create_channel, channels, join, sync, send, delete, ban]) .mount("/", FileServer::from("./static"))
.mount("/api", routes![init, login, create_channel, channels, join, sync, send, delete, ban]);
} }