Skip to content

Commit

Permalink
feat: fixes (i forgor the details)
Browse files Browse the repository at this point in the history
  • Loading branch information
112batuhan committed Nov 26, 2024
1 parent 910dd73 commit e966a62
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ JWT_SECRET_KEY=something_password_like
PORT=8000

ADMIN_PASSWORD=password

# Set this to true when you want to start periodical user updates
DAILY_UPDATE=false
2 changes: 1 addition & 1 deletion src/daily_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn update_routine(
client.clone(),
database.clone(),
users_to_update,
Duration::from_secs(15),
Duration::from_secs(60),
)
.await;
}
Expand Down
1 change: 1 addition & 0 deletions src/database/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl DatabaseClient {
ORDER BY count DESC
)
WHERE $country = none or out.country_code = $country
ORDER count DESC
LIMIT $limit
START $start;
",
Expand Down
2 changes: 1 addition & 1 deletion src/database/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl DatabaseClient {
pub async fn get_users_to_update(&self) -> Result<Vec<u32>, AppError> {
let ids: Vec<DbUserId> = self
.db
.query("SELECT meta::id(id) as id FROM user WHERE updated_at + 1s < time::now()")
.query("SELECT meta::id(id) as id FROM user WHERE updated_at + 1w < time::now()")
.await?
.take(0)?;

Expand Down
17 changes: 15 additions & 2 deletions src/handlers/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub async fn osu_oauth2_redirect(
auth_response.expires_in,
)?;
let mut redirect_response = Redirect::to(POST_LOGIN_REDIRECT_URI.as_str()).into_response();
redirect_response.headers_mut().insert(
let headers = redirect_response.headers_mut();
headers.append(
SET_COOKIE,
format!(
"user_token={}; HttpOnly; Max-Age=86400; Path=/; SameSite=lax",
Expand All @@ -70,6 +71,12 @@ pub async fn osu_oauth2_redirect(
.parse()
.unwrap(),
);
headers.append(
SET_COOKIE,
"logged_in=true;Max-Age=86400; Path=/; SameSite=lax"
.parse()
.unwrap(),
);

// TODO: maybe fix authorized thing to be in the same query later?
let osu_user_id = osu_user.id;
Expand All @@ -87,12 +94,18 @@ pub fn osu_oauth2_redirect_docs(op: TransformOperation<'_>) -> TransformOperatio

pub async fn logout() -> Response {
let mut headers = HeaderMap::new();
headers.insert(
headers.append(
SET_COOKIE,
"user_token=deleted; HttpOnly; Max-Age=-1; Path=/; SameSite=lax"
.parse()
.unwrap(),
);
headers.append(
SET_COOKIE,
"logged_in=false; Max-Age=-1; Path=/; SameSite=lax"
.parse()
.unwrap(),
);
headers.into_response()
}

Expand Down
22 changes: 14 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use axum::{
use axum_swagger_ui::swagger_ui;
use mapper_influences_backend_rs::{
daily_update::update_routine,
database::{user::ActivityPreferences, DatabaseClient},
database::DatabaseClient,
osu_api::{credentials_grant::CredentialsGrantClient, request::OsuApiRequestClient},
routes, AppState,
};
Expand All @@ -30,19 +30,25 @@ async fn main() {
let db = DatabaseClient::new(&url)
.await
.expect("failed to initialize db connection");

let request = Arc::new(OsuApiRequestClient::new(10));
let credentials_grant_client = CredentialsGrantClient::new(request.clone())
.await
.expect("Failed to initialize credentials grant client");

let state = AppState::new(request, credentials_grant_client.clone(), db.clone()).await;

tokio::spawn(update_routine(
credentials_grant_client,
db.clone(),
Duration::from_secs(120),
));
let start_var = std::env::var("DAILY_UPDATE");
if start_var.is_ok_and(|value| value.to_lowercase() == "true") {
let initial_delay = 10;
info!(
"starting daily updates after initial delay of {} seconds",
initial_delay,
);
tokio::spawn(update_routine(
credentials_grant_client,
db.clone(),
Duration::from_secs(initial_delay),
));
}

aide::gen::on_error(|error| {
println!("{error}");
Expand Down

0 comments on commit e966a62

Please sign in to comment.