Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into add-browser-url
Browse files Browse the repository at this point in the history
  • Loading branch information
encadyma authored Oct 23, 2020
2 parents a5af1b8 + 86be5e7 commit 945ca5d
Show file tree
Hide file tree
Showing 23 changed files with 837 additions and 546 deletions.
66 changes: 44 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ base64 = "0.12.3"
billboard = "0.1.0"
binary-install = "0.0.3-alpha.1"
chrome-devtools-rs = { version = "0.0.0-alpha.1", features = ["color"] }
chrono = "0.4.15"
chrono = "0.4.19"
clap = "2.33.3"
cloudflare = "0.6.6"
config = "0.10.1"
Expand Down
11 changes: 6 additions & 5 deletions src/commands/dev/edge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use setup::{upload, Session};
use watch::watch_for_changes;

use crate::commands::dev::{socket, Protocol, ServerConfig};
use crate::deploy::DeployTarget;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{DeployConfig, Target};
use crate::settings::toml::Target;

use tokio::runtime::Runtime as TokioRuntime;

Expand All @@ -18,17 +19,17 @@ pub fn dev(
target: Target,
user: GlobalUser,
server_config: ServerConfig,
deploy_config: DeployConfig,
deploy_target: DeployTarget,
local_protocol: Protocol,
upstream_protocol: Protocol,
verbose: bool,
) -> Result<(), failure::Error> {
let session = Session::new(&target, &user, &deploy_config)?;
let session = Session::new(&target, &user, &deploy_target)?;
let mut target = target;

let preview_token = upload(
&mut target,
&deploy_config,
&deploy_target,
&user,
session.preview_token.clone(),
verbose,
Expand All @@ -43,7 +44,7 @@ pub fn dev(
thread::spawn(move || {
watch_for_changes(
target,
&deploy_config,
&deploy_target,
&user,
Arc::clone(&preview_token),
session_token,
Expand Down
40 changes: 22 additions & 18 deletions src/commands/dev/edge/setup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::path::Path;

use crate::deploy::DeployTarget;
use crate::kv::bulk;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{DeployConfig, Target};
use crate::settings::toml::Target;
use crate::sites::{add_namespace, sync};
use crate::terminal::message::{Message, StdOut};
use crate::upload;
Expand All @@ -13,7 +14,7 @@ use serde_json::json;

pub(super) fn upload(
target: &mut Target,
deploy_config: &DeployConfig,
deploy_target: &DeployTarget,
user: &GlobalUser,
session_token: String,
verbose: bool,
Expand All @@ -38,7 +39,7 @@ pub(super) fn upload(
(Vec::new(), None, None)
};

let session_config = get_session_config(deploy_config);
let session_config = get_session_config(deploy_target);
let address = get_upload_address(target);

let script_upload_form = upload::form::build(target, asset_manifest, Some(session_config))?;
Expand Down Expand Up @@ -76,23 +77,24 @@ impl Session {
pub fn new(
target: &Target,
user: &GlobalUser,
deploy_config: &DeployConfig,
deploy_target: &DeployTarget,
) -> Result<Session, failure::Error> {
let exchange_url = get_exchange_url(deploy_config, user)?;
let exchange_url = get_exchange_url(deploy_target, user)?;
let host = match exchange_url.host_str() {
Some(host) => Ok(host.to_string()),
None => Err(failure::format_err!(
"Could not parse host from exchange url"
)),
}?;

let host = match deploy_config {
DeployConfig::Zoned(_) => host,
DeployConfig::Zoneless(_) => {
let host = match deploy_target {
DeployTarget::Zoned(_) => host,
DeployTarget::Zoneless(_) => {
let namespaces: Vec<&str> = host.as_str().split('.').collect();
let subdomain = namespaces[1];
format!("{}.{}.workers.dev", target.name, subdomain)
}
_ => unreachable!(),
};

let client = crate::http::legacy_auth_client(&user);
Expand All @@ -114,30 +116,32 @@ impl Session {
}
}

fn get_session_config(deploy_config: &DeployConfig) -> serde_json::Value {
match deploy_config {
DeployConfig::Zoned(config) => {
fn get_session_config(target: &DeployTarget) -> serde_json::Value {
match target {
DeployTarget::Zoned(config) => {
let mut routes: Vec<String> = Vec::new();
for route in &config.routes {
routes.push(route.pattern.clone());
}
json!({ "routes": routes })
}
DeployConfig::Zoneless(_) => json!({"workers_dev": true}),
DeployTarget::Zoneless(_) => json!({"workers_dev": true}),
_ => unreachable!(),
}
}

fn get_session_address(deploy_config: &DeployConfig) -> String {
match deploy_config {
DeployConfig::Zoned(config) => format!(
fn get_session_address(target: &DeployTarget) -> String {
match target {
DeployTarget::Zoned(config) => format!(
"https://api.cloudflare.com/client/v4/zones/{}/workers/edge-preview",
config.zone_id
),
// TODO: zoneless is probably wrong
DeployConfig::Zoneless(config) => format!(
DeployTarget::Zoneless(config) => format!(
"https://api.cloudflare.com/client/v4/accounts/{}/workers/subdomain/edge-preview",
config.account_id
),
_ => unreachable!(),
}
}

Expand All @@ -149,11 +153,11 @@ fn get_upload_address(target: &mut Target) -> String {
}

fn get_exchange_url(
deploy_config: &DeployConfig,
deploy_target: &DeployTarget,
user: &GlobalUser,
) -> Result<Url, failure::Error> {
let client = crate::http::legacy_auth_client(&user);
let address = get_session_address(deploy_config);
let address = get_session_address(deploy_target);
let url = Url::parse(&address)?;
let response = client.get(url).send()?.error_for_status()?;
let text = &response.text()?;
Expand Down
10 changes: 5 additions & 5 deletions src/commands/dev/edge/watch.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::{mpsc, Arc, Mutex};

use crate::commands::dev::edge::setup;

use crate::deploy::DeployTarget;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{DeployConfig, Target};
use crate::settings::toml::Target;
use crate::watch::watch_and_build;

pub fn watch_for_changes(
target: Target,
deploy_config: &DeployConfig,
deploy_target: &DeployTarget,
user: &GlobalUser,
preview_token: Arc<Mutex<String>>,
session_token: String,
Expand All @@ -20,7 +20,7 @@ pub fn watch_for_changes(
while receiver.recv().is_ok() {
let user = user.clone();
let target = target.clone();
let deploy_config = deploy_config.clone();
let deploy_target = deploy_target.clone();
let session_token = session_token.clone();
let mut target = target;

Expand All @@ -32,7 +32,7 @@ pub fn watch_for_changes(
//
// this allows the server to route subsequent requests
// to the proper script
*preview_token = setup::upload(&mut target, &deploy_config, &user, session_token, verbose)?;
*preview_token = setup::upload(&mut target, &deploy_target, &user, session_token, verbose)?;
}

Ok(())
Expand Down
Loading

0 comments on commit 945ca5d

Please sign in to comment.