Skip to content

Commit

Permalink
worker: allows cors for pob.cool
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed May 25, 2024
1 parent 784fb45 commit de0eebe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions worker/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ pub const OAUTH_SCOPE: &str = "account:profile";

pub const CACHE_A_BIT: Duration = Duration::from_secs(21600); // 6 Hours
pub const CACHE_FOREVER: Duration = Duration::from_secs(31536000);

pub const CORS_POB_API: &[&str] = &["https://pob.cool"];
24 changes: 22 additions & 2 deletions worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod app_metadata {
}

use request_context::RequestContext;
use utils::CacheControl;
use utils::{CacheControl, RequestExt};

pub use self::error::{Error, ErrorResponse, Result};
pub use self::response::Response;
Expand Down Expand Up @@ -96,11 +96,31 @@ async fn cached(rctx: &mut RequestContext) -> Response {
.tag("status", "miss")
.tag("transaction", rctx.transaction());

let response = handle(rctx).await;
let response = cors(rctx).await;

cache_entry.store(response).await
}

async fn cors(rctx: &mut RequestContext) -> Response {
let response = handle(rctx).await;

let matches = |allowed: &[&str]| {
let origin = rctx.header("Origin");
origin.filter(|origin| allowed.contains(&origin.as_str()))
};

use route::{Api::*, GetEndpoints::*, Route::*};
let origin = match rctx.route() {
Api(Get(PobPaste(_) | PobUserPaste(_, _))) => matches(consts::CORS_POB_API),
_ => None,
};

response.append_header(
"Access-Control-Allow-Origin",
origin.as_deref().unwrap_or(""),
)
}

#[tracing::instrument(skip_all)]
async fn handle(rctx: &mut RequestContext) -> Response {
let response = match rctx.route() {
Expand Down

0 comments on commit de0eebe

Please sign in to comment.