Skip to content

Commit

Permalink
fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
harshdoesdev committed Feb 16, 2024
2 parents 76296d4 + 6c1fb51 commit 814621a
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 33 deletions.
4 changes: 1 addition & 3 deletions fastn-core/src/auth/discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ pub async fn login(req: actix_web::HttpRequest) -> fastn_core::Result<fastn_core
DiscordScopes::GuildsMembersRead.as_str()
);
// send redirect to /auth/discord/callback/
Ok(actix_web::HttpResponse::Found()
.append_header((actix_web::http::header::LOCATION, discord_auth_url))
.finish())
Ok(fastn_core::http::temporary_redirect(discord_auth_url))
}

// route: /auth/discord/callback/
Expand Down
13 changes: 5 additions & 8 deletions fastn-core/src/auth/email_password/confirm_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ pub(crate) async fn confirm_email(
if key_expired(&req_config.config.ds, sent_at).await {
// TODO: this redirect route should be configurable
tracing::info!("provided code has expired.");
return Ok(fastn_core::http::redirect_with_code(
format!(
"{}://{}/-/auth/resend-confirmation-email/",
req_config.request.connection_info.scheme(),
req_config.request.connection_info.host(),
),
302,
));
return Ok(fastn_core::http::temporary_redirect(format!(
"{}://{}/-/auth/resend-confirmation-email/",
req_config.request.connection_info.scheme(),
req_config.request.connection_info.host(),
)));
}

let email: fastn_core::utils::CiString =
Expand Down
3 changes: 1 addition & 2 deletions fastn-core/src/auth/email_password/onboarding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ pub(crate) async fn onboarding(
.cookie(fastn_core::auth::FIRST_TIME_SESSION_COOKIE_NAME)
.is_none()
{
return Ok(fastn_core::http::redirect_with_code(
return Ok(fastn_core::http::temporary_redirect(
redirect_url_from_next(req, next),
307,
));
}

Expand Down
3 changes: 1 addition & 2 deletions fastn-core/src/auth/email_password/resend_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub(crate) async fn resend_email(
// TODO: there's no GET /-/auth/login/ yet
// the client will have to create one for now
// this path should be configuratble too
Ok(fastn_core::http::redirect_with_code(
Ok(fastn_core::http::temporary_redirect(
redirect_url_from_next(req, next),
302,
))
}
8 changes: 4 additions & 4 deletions fastn-core/src/auth/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ pub async fn login(
.add_scope(oauth2::Scope::new("read:org".to_string()))
.url();

Ok(fastn_core::http::redirect(authorize_url.to_string()))
Ok(fastn_core::http::temporary_redirect(
authorize_url.to_string(),
))
}

// route: /-/auth/github/done/
Expand All @@ -56,9 +58,7 @@ pub async fn callback(
// ask to update details by giving a form
// redirect to next for now
if fastn_core::auth::utils::is_authenticated(req) {
return Ok(actix_web::HttpResponse::Found()
.append_header((actix_web::http::header::LOCATION, next))
.finish());
return Ok(fastn_core::http::temporary_redirect(next));
}

let access_token = match fastn_core::auth::github::utils::github_client(ds)
Expand Down
4 changes: 1 addition & 3 deletions fastn-core/src/auth/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ pub async fn login(req: actix_web::HttpRequest) -> fastn_core::Result<fastn_core
TwitterScopes::ReadList.as_str(),
);
// send redirect to /auth/twitter/callback/
Ok(actix_web::HttpResponse::Found()
.append_header((actix_web::http::header::LOCATION, twitter_auth_url))
.finish())
Ok(fastn_core::http::temporary_redirect(twitter_auth_url))
}
// route: /auth/twitter/callback/
// In this API we are accessing
Expand Down
2 changes: 1 addition & 1 deletion fastn-core/src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn handle_redirect(
.redirects
.as_ref()
.and_then(|v| fastn_core::package::redirects::find_redirect(v, path.as_str()))
.map(|r| fastn_core::http::redirect(r.to_string()))
.map(|r| fastn_core::http::permanent_redirect(r.to_string()))
}

/// path: /-/<package-name>/<file-name>/
Expand Down
9 changes: 9 additions & 0 deletions fastn-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,15 @@ impl Config {
apps
};

config.package.endpoints = {
for endpoint in &mut config.package.endpoints {
endpoint.endpoint =
fastn_core::utils::interpolate_env_vars(&config.ds, &endpoint.endpoint).await?;
}

config.package.endpoints
};

config
.all_packages
.insert(package.name.to_string(), package.to_owned());
Expand Down
10 changes: 6 additions & 4 deletions fastn-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ pub fn ok(data: Vec<u8>) -> fastn_core::http::Response {
actix_web::HttpResponse::Ok().body(data)
}

pub fn redirect(url: String) -> fastn_core::http::Response {
actix_web::HttpResponse::PermanentRedirect()
.insert_header(("LOCATION", url))
.finish()
pub fn permanent_redirect(url: String) -> fastn_core::http::Response {
redirect_with_code(url, 308)
}

pub fn temporary_redirect(url: String) -> fastn_core::http::Response {
redirect_with_code(url, 307)
}

pub fn redirect_with_code(url: String, code: i32) -> fastn_core::http::Response {
Expand Down
45 changes: 45 additions & 0 deletions fastn-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,3 +1072,48 @@ pub(crate) fn is_static_path(path: &str) -> bool {
None => false,
}
}

static VARIABLE_INTERPOLATION_RGX: once_cell::sync::Lazy<regex::Regex> =
once_cell::sync::Lazy::new(|| regex::Regex::new(r"\$\{([^}]+)\}").unwrap());

pub(crate) async fn interpolate_env_vars(
ds: &fastn_ds::DocumentStore,
endpoint: &str,
) -> fastn_core::Result<String> {
let mut result = String::new();
let mut last_end = 0;

for captures in VARIABLE_INTERPOLATION_RGX.captures_iter(endpoint) {
let capture = captures.get(0).unwrap();
let start = capture.start();
let end = capture.end();
result.push_str(&endpoint[last_end..start]);

let key = captures.get(1).unwrap().as_str();

let value = match key {
key if key.starts_with("env.") => {
let env_key = key.trim_start_matches("env.");
ds.env(env_key).await.map_err(|e| {
fastn_core::error::Error::generic(format!(
"Failed to interpolate environment variable '{}' in endpoint.: {e}",
env_key
))
})?
}
_ => {
return Err(fastn_core::error::Error::generic(format!(
"Failed to interpolate unknown variable '{}' in endpoint.",
key
)))
}
};

result.push_str(&value);

last_end = end;
}

result.push_str(&endpoint[last_end..]);
Ok(result)
}
2 changes: 1 addition & 1 deletion fastn-core/tests/21-http-endpoint/cmd.p1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- fbt:
cmd: $FBT_CWD/../target/debug/fastn --test build
cmd: REQRES="reqres.in" $FBT_CWD/../target/debug/fastn --test build
output: .build

-- stdout:
Expand Down
2 changes: 1 addition & 1 deletion fastn-core/tests/21-http-endpoint/input/FASTN.ftd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

-- fastn.url-mappings:

/api/* -> http+proxy://reqres.in/api/*
/api/* -> http+proxy://${env.REQRES}/api/*
2 changes: 1 addition & 1 deletion fastn-core/tests/21-http-endpoint/output/FASTN.ftd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

-- fastn.url-mappings:

/api/* -> http+proxy://reqres.in/api/*
/api/* -> http+proxy://${env.REQRES}/api/*
6 changes: 3 additions & 3 deletions fastn-core/tests/21-http-endpoint/output/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"files": {
"FASTN.ftd": {
"name": "FASTN.ftd",
"checksum": "C2CE66A098529553F69A4B28405B0777037EDFA90E02D4B054C0239D38FAF9FA",
"size": 141
"checksum": "1A051A65F8DEDF8464FC2B0BC057E78D565655893F0435EE5E145EB23A667C7C",
"size": 145
},
"index.ftd": {
"name": "index.ftd",
Expand All @@ -12,5 +12,5 @@
}
},
"zip_url": "https://codeload.github.com/fastn-stack/http-endpoint-test/zip/refs/heads/main",
"checksum": "8FE0D88CC4B265A00618574359DC902976A2B4494B950D547F7D939E7F2A25E2"
"checksum": "6B712EBA21408F2619EC7EBB3B90BAE0879A4AA451F08BD9FA54F2D34159BBE0"
}

0 comments on commit 814621a

Please sign in to comment.