Skip to content

Commit

Permalink
host resolution refactor
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 67669bd8b46688a1f5172a74ff07480d7b332b1a
  • Loading branch information
gautamg795 authored and Convex, Inc. committed Aug 30, 2024
1 parent c018273 commit 03fa608
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 71 deletions.
62 changes: 31 additions & 31 deletions crates/application/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use common::{
ExportPath,
PublicFunctionPath,
},
http::ResolvedHost,
http::ResolvedHostname,
pause::PauseClient,
runtime::Runtime,
types::{
Expand Down Expand Up @@ -93,7 +93,7 @@ pub enum ExecuteQueryTimestamp {
pub trait ApplicationApi: Send + Sync {
async fn authenticate(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
auth_token: AuthenticationToken,
) -> anyhow::Result<Identity>;
Expand All @@ -103,7 +103,7 @@ pub trait ApplicationApi: Send + Sync {
/// for queries.
async fn execute_public_query(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand All @@ -118,7 +118,7 @@ pub trait ApplicationApi: Send + Sync {
/// for admin or system identity.
async fn execute_admin_query(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -131,7 +131,7 @@ pub trait ApplicationApi: Send + Sync {
/// Execute a public mutation on the root app.
async fn execute_public_mutation(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand All @@ -144,7 +144,7 @@ pub trait ApplicationApi: Send + Sync {
/// Execute an admin mutation for a particular component for the dashboard.
async fn execute_admin_mutation(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -156,7 +156,7 @@ pub trait ApplicationApi: Send + Sync {
/// Execute a public action on the root app.
async fn execute_public_action(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand All @@ -167,7 +167,7 @@ pub trait ApplicationApi: Send + Sync {
/// Execute an admin action for a particular component for the dashboard.
async fn execute_admin_action(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -178,7 +178,7 @@ pub trait ApplicationApi: Send + Sync {
/// Execute an HTTP action on the root app.
async fn execute_http_action(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
http_request_metadata: HttpActionRequest,
identity: Identity,
Expand All @@ -191,7 +191,7 @@ pub trait ApplicationApi: Send + Sync {
/// calling functions outside the root component.
async fn execute_any_function(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -201,21 +201,21 @@ pub trait ApplicationApi: Send + Sync {

async fn latest_timestamp(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
) -> anyhow::Result<RepeatableTimestamp>;

async fn check_store_file_authorization(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
token: &str,
validity: Duration,
) -> anyhow::Result<ComponentId>;

async fn store_file(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -227,7 +227,7 @@ pub trait ApplicationApi: Send + Sync {

async fn get_file_range(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -237,7 +237,7 @@ pub trait ApplicationApi: Send + Sync {

async fn get_file(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
request_id: RequestId,
origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -253,7 +253,7 @@ pub trait ApplicationApi: Send + Sync {
// start this way.
async fn subscription_client(
&self,
host: &ResolvedHost,
host: &ResolvedHostname,
) -> anyhow::Result<Box<dyn SubscriptionClient>>;
}

Expand All @@ -262,7 +262,7 @@ pub trait ApplicationApi: Send + Sync {
impl<RT: Runtime> ApplicationApi for Application<RT> {
async fn authenticate(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
auth_token: AuthenticationToken,
) -> anyhow::Result<Identity> {
Expand All @@ -272,7 +272,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_public_query(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand Down Expand Up @@ -303,7 +303,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_admin_query(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_public_mutation(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand All @@ -361,7 +361,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_admin_mutation(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -387,7 +387,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_public_action(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: ExportPath,
Expand All @@ -410,7 +410,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_admin_action(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -433,7 +433,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn execute_any_function(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
identity: Identity,
path: CanonicalizedComponentFunctionPath,
Expand All @@ -449,15 +449,15 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn latest_timestamp(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
) -> anyhow::Result<RepeatableTimestamp> {
Ok(self.now_ts_for_reads())
}

async fn execute_http_action(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
request_id: RequestId,
http_request_metadata: HttpActionRequest,
identity: Identity,
Expand All @@ -476,7 +476,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn check_store_file_authorization(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
token: &str,
validity: Duration,
Expand All @@ -487,7 +487,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn store_file(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
_origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -508,7 +508,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn get_file_range(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
_origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -520,7 +520,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn get_file(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
_request_id: RequestId,
_origin: ConvexOrigin,
component: ComponentId,
Expand All @@ -531,7 +531,7 @@ impl<RT: Runtime> ApplicationApi for Application<RT> {

async fn subscription_client(
&self,
_host: &ResolvedHost,
_host: &ResolvedHostname,
) -> anyhow::Result<Box<dyn SubscriptionClient>> {
Ok(Box::new(ApplicationSubscriptionClient {
database: self.database.clone(),
Expand Down
22 changes: 11 additions & 11 deletions crates/common/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ pub async fn stats_middleware<RM: RouteMapper>(
State(route_metric_mapper): State<RM>,
matched_path: Option<axum::extract::MatchedPath>,
ExtractRequestId(request_id): ExtractRequestId,
ExtractResolvedHost(resolved_host): ExtractResolvedHost,
ExtractResolvedHostname(resolved_host): ExtractResolvedHostname,
ExtractClientVersion(client_version): ExtractClientVersion,
req: http::request::Request<Body>,
next: axum::middleware::Next,
Expand Down Expand Up @@ -839,7 +839,7 @@ pub enum RequestDestination {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ResolvedHost {
pub struct ResolvedHostname {
pub instance_name: String,
pub destination: RequestDestination,
}
Expand All @@ -851,7 +851,7 @@ pub static CONVEX_DOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| {
.unwrap()
});

pub fn resolve_convex_domain(uri: &Uri) -> anyhow::Result<Option<ResolvedHost>> {
pub fn resolve_convex_domain(uri: &Uri) -> anyhow::Result<Option<ResolvedHostname>> {
let host = uri.host().context("URI does not have valid host")?;
if let Some(captures) = CONVEX_DOMAIN_REGEX.captures(host) {
let instance_name = captures[CONVEX_DOMAIN_REGEX_INSTANCE_CAPTURE].to_string();
Expand All @@ -860,21 +860,21 @@ pub fn resolve_convex_domain(uri: &Uri) -> anyhow::Result<Option<ResolvedHost>>
"site" => RequestDestination::ConvexSite,
_ => unreachable!("Regex capture only matches cloud or site"),
};
return Ok(Some(ResolvedHost {
return Ok(Some(ResolvedHostname {
instance_name,
destination,
}));
}
Ok(None)
}

pub struct ExtractResolvedHost(pub ResolvedHost);
pub struct ExtractResolvedHostname(pub ResolvedHostname);

#[derive(Clone, Debug)]
pub struct OriginalHttpUri(pub Uri);

#[async_trait]
impl<S> FromRequestParts<S> for ExtractResolvedHost {
impl<S> FromRequestParts<S> for ExtractResolvedHostname {
type Rejection = Infallible;

async fn from_request_parts(
Expand All @@ -886,22 +886,22 @@ impl<S> FromRequestParts<S> for ExtractResolvedHost {
// extraction to be fallible and optional.
#[allow(clippy::disallowed_types)]
if let Ok(axum::Extension(resolved)) =
parts.extract::<axum::Extension<ResolvedHost>>().await
parts.extract::<axum::Extension<ResolvedHostname>>().await
{
return Ok(ExtractResolvedHost(resolved));
return Ok(ExtractResolvedHostname(resolved));
}
// Try to parse the Host header as a URI and then resolve it as a Convex domain
let host = parts.extract::<Host>().await.map_err(anyhow::Error::from);
if let Ok(Some(resolved)) = host
.and_then(|Host(host)| Uri::try_from(host).map_err(anyhow::Error::from))
.and_then(|uri| resolve_convex_domain(&uri))
{
return Ok(ExtractResolvedHost(resolved));
return Ok(ExtractResolvedHostname(resolved));
}

// No luck -- fall back to `CONVEX_SITE` and assume `convex.cloud` as this is
// likely a request to localhost.
Ok(ExtractResolvedHost(ResolvedHost {
Ok(ExtractResolvedHostname(ResolvedHostname {
instance_name: ::std::env::var("CONVEX_SITE").unwrap_or_default(),
destination: RequestDestination::ConvexCloud,
}))
Expand Down Expand Up @@ -1023,7 +1023,7 @@ where

async fn log_middleware(
remote_addr: Option<axum::extract::ConnectInfo<SocketAddr>>,
ExtractResolvedHost(resolved_host): ExtractResolvedHost,
ExtractResolvedHostname(resolved_host): ExtractResolvedHostname,
req: axum::extract::Request,
next: axum::middleware::Next,
) -> Result<Response, HttpResponseError> {
Expand Down
4 changes: 2 additions & 2 deletions crates/local_backend/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use common::{
http::{
extract::Query,
ExtractRequestId,
ExtractResolvedHost,
ExtractResolvedHostname,
HttpResponseError,
},
runtime::Runtime,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl FromRequestParts<RouterState> for TryExtractIdentity {
Err(e) => return Ok(Self(Err(e.into()))),
};

let Ok(ExtractResolvedHost(host)) = parts.extract::<ExtractResolvedHost>().await;
let Ok(ExtractResolvedHostname(host)) = parts.extract::<ExtractResolvedHostname>().await;

let request_id = match parts.extract::<ExtractRequestId>().await {
Ok(id) => id,
Expand Down
Loading

0 comments on commit 03fa608

Please sign in to comment.