adding state, sessions, and cookies
This commit is contained in:
parent
f1f01c2055
commit
c7674f2681
1 changed files with 56 additions and 23 deletions
79
src/main.rs
79
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<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn save(&self) -> bool {
|
||||
todo!();
|
||||
}
|
||||
pub fn load(path: &str) -> Self {
|
||||
todo!();
|
||||
}
|
||||
pub struct Session {
|
||||
uid: Icechip<u64>,
|
||||
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<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")]
|
||||
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]);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue