rust
Elnu 2 years ago
parent 94bd2da27b
commit a282d06bc5

@ -7,4 +7,4 @@ pub mod user {
pub const USER_NAME_COOKIE: &str = "user_name";
pub const USER_DISCRIMINATOR_COOKIE: &str = "user_discriminator";
pub const USER_AVATAR_COOKIE: &str = "user_avatar";
}
}

@ -25,9 +25,11 @@ async fn rocket() -> _ {
let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
let http = Http::new(&token);
let config = rocket::Config::figment()
.merge(("port", 1313))
.merge(("secret_key", env::var("SECRET").expect("Must have SECRET defined in .env for private cookie persistence")));
let config = rocket::Config::figment().merge(("port", 1313)).merge((
"secret_key",
env::var("SECRET")
.expect("Must have SECRET defined in .env for private cookie persistence"),
));
rocket::custom(config)
.manage(http)
.mount(

@ -31,7 +31,8 @@ impl Challenge {
let arena = Arena::new();
let root = parse_document(
&arena,
&fs::read_to_string(format!("content/challenges/{number}.md")).expect("Couldn't find challenge file"),
&fs::read_to_string(format!("content/challenges/{number}.md"))
.expect("Couldn't find challenge file"),
&options,
);
if let Some(node) = root.children().next() {
@ -48,7 +49,7 @@ impl Challenge {
}
} else {
panic!("Empty document!")
}
}
}
}

@ -2,4 +2,4 @@ mod user;
pub use user::User;
mod challenge;
pub use challenge::Challenge;
pub use challenge::Challenge;

@ -6,7 +6,7 @@ mod tests;
use chrono::Utc;
use derive_more::From;
use reqwest::StatusCode;
use rocket::http::{CookieJar, Cookie};
use rocket::http::{Cookie, CookieJar};
use serial::*;
use serde::Deserialize;
@ -113,4 +113,4 @@ pub enum GetUserError {
fn parse_cookie_value<T: std::str::FromStr>(cookies: &CookieJar<'_>, name: &str) -> Option<T> {
cookies.get_private(name)?.value().parse().ok()
}
}

@ -1,4 +1,4 @@
use serde::{Serialize, Serializer, ser::SerializeStruct};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use super::User;
@ -31,4 +31,4 @@ impl Serialize for User {
state.serialize_field("username", &self.username())?;
state.end()
}
}
}

@ -18,4 +18,4 @@ fn test_legacy_username() {
fn test_new_username() {
let user = test_user("test", 0);
assert_eq!(user.username(), "test");
}
}

@ -1 +1 @@
pub use crate::utils::Kyujitai;
pub use crate::utils::Kyujitai;

@ -1,7 +1,7 @@
use rocket::http::CookieJar;
use rocket_dyn_templates::{Template, context};
use rocket_dyn_templates::{context, Template};
use crate::models::{User, Challenge};
use crate::models::{Challenge, User};
#[get("/<challenge>")]
pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template {
@ -19,4 +19,4 @@ pub async fn get_challenge(challenge: u32, cookies: &CookieJar<'_>) -> Template
content: Challenge::get(challenge),
},
)
}
}

@ -10,4 +10,4 @@ pub fn login() -> Redirect {
client_id = env::var("CLIENT_ID").unwrap(),
redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()),
))
}
}

@ -2,7 +2,7 @@ use std::{collections::HashMap, env};
use rocket::{http::CookieJar, response::Redirect};
use crate::{utils::Referer, cookies::token::TOKEN_COOKIE, models::User};
use crate::{cookies::token::TOKEN_COOKIE, models::User, utils::Referer};
#[get("/logout")]
pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect {
@ -33,4 +33,4 @@ pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect {
User::purge(cookies);
let redirect_url = referer.0.unwrap_or("/".to_owned());
Redirect::to(redirect_url)
}
}

@ -20,4 +20,4 @@ pub use _success::success;
#[path = "testing.rs"]
mod _testing;
pub use _testing::testing;
pub use _testing::testing;

@ -1,5 +1,9 @@
use chrono::{Utc, Duration};
use rocket::{form::Form, http::{CookieJar, Cookie}, response::Redirect};
use chrono::{Duration, Utc};
use rocket::{
form::Form,
http::{Cookie, CookieJar},
response::Redirect,
};
use crate::{cookies::token::TOKEN_EXPIRE_COOKIE, models::User};
@ -24,4 +28,4 @@ pub async fn post_login(login: Form<Login<'_>>, cookies: &CookieJar<'_>) -> Redi
));
}
Redirect::to("/")
}
}

@ -21,4 +21,4 @@ pub fn success() -> RawHtml<&'static str> {
}
</script>",
)
}
}

@ -8,13 +8,21 @@ use crate::models::User;
#[get("/testing")]
pub async fn testing(cookies: &CookieJar<'_>, http: &State<Http>) -> String {
// Get logged in user's join date in 字ちゃん server
format!("{:?}", http.get_guild(814700630958276649).await
.expect("Failed to get testing guild")
.member(http.deref(), User::get(cookies).await
.expect("Failed to get logged in user data")
.expect("No logged in user")
.id
).await
.expect("Failed to fetch user in server")
.joined_at)
}
format!(
"{:?}",
http.get_guild(814700630958276649)
.await
.expect("Failed to get testing guild")
.member(
http.deref(),
User::get(cookies)
.await
.expect("Failed to get logged in user data")
.expect("No logged in user")
.id
)
.await
.expect("Failed to fetch user in server")
.joined_at
)
}

@ -1,6 +1,9 @@
use std::convert::Infallible;
use rocket::{request::{FromRequest, self}, Request};
use rocket::{
request::{self, FromRequest},
Request,
};
pub struct Referer(pub Option<String>);
@ -12,4 +15,4 @@ impl<'r> FromRequest<'r> for Referer {
let referer = req.headers().get_one("Referer");
request::Outcome::Success(Referer(referer.map(|referer| referer.to_owned())))
}
}
}

Loading…
Cancel
Save