From 77eeb10dc51ac98e50b3bd6dcdfd71725464c07c Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Tue, 18 Feb 2020 16:36:26 -0600 Subject: [PATCH 1/5] Separate dev into edge and gcs modules --- src/commands/dev/edge/headers.rs | 0 src/commands/dev/edge/mod.rs | 16 ++ src/commands/dev/edge/server.rs | 3 + src/commands/dev/edge/setup.rs | 3 + src/commands/dev/{ => gcs}/headers.rs | 0 src/commands/dev/gcs/mod.rs | 63 ++++++ src/commands/dev/gcs/server.rs | 115 +++++++++++ src/commands/dev/gcs/setup.rs | 28 +++ src/commands/dev/{ => gcs}/watch.rs | 5 +- src/commands/dev/mod.rs | 195 +----------------- src/commands/dev/server_config/host.rs | 2 +- .../dev/server_config/listening_address.rs | 2 +- src/commands/dev/server_config/mod.rs | 2 +- src/commands/dev/socket.rs | 5 +- 14 files changed, 242 insertions(+), 197 deletions(-) create mode 100644 src/commands/dev/edge/headers.rs create mode 100644 src/commands/dev/edge/mod.rs create mode 100644 src/commands/dev/edge/server.rs create mode 100644 src/commands/dev/edge/setup.rs rename src/commands/dev/{ => gcs}/headers.rs (100%) create mode 100644 src/commands/dev/gcs/mod.rs create mode 100644 src/commands/dev/gcs/server.rs create mode 100644 src/commands/dev/gcs/setup.rs rename src/commands/dev/{ => gcs}/watch.rs (84%) diff --git a/src/commands/dev/edge/headers.rs b/src/commands/dev/edge/headers.rs new file mode 100644 index 000000000..e69de29bb diff --git a/src/commands/dev/edge/mod.rs b/src/commands/dev/edge/mod.rs new file mode 100644 index 000000000..3336f3fb4 --- /dev/null +++ b/src/commands/dev/edge/mod.rs @@ -0,0 +1,16 @@ +use crate::commands::dev::ServerConfig; +use crate::settings::global_user::GlobalUser; +use crate::settings::toml::Target; + +pub fn dev( + target: Target, + user: GlobalUser, + server_config: ServerConfig, + verbose: bool, +) -> Result<(), failure::Error> { + println!( + "{:?}\n{:?}\n{:?}\n{:?}", + target, user, server_config, verbose + ); + Ok(()) +} diff --git a/src/commands/dev/edge/server.rs b/src/commands/dev/edge/server.rs new file mode 100644 index 000000000..6a227cea0 --- /dev/null +++ b/src/commands/dev/edge/server.rs @@ -0,0 +1,3 @@ +pub(super) async fn serve() -> Result<(), failure::Error> { + unimplemented!() +} \ No newline at end of file diff --git a/src/commands/dev/edge/setup.rs b/src/commands/dev/edge/setup.rs new file mode 100644 index 000000000..761c7e88b --- /dev/null +++ b/src/commands/dev/edge/setup.rs @@ -0,0 +1,3 @@ +pub(super) fn get_preview_id() -> Result { + unimplemented!() +} diff --git a/src/commands/dev/headers.rs b/src/commands/dev/gcs/headers.rs similarity index 100% rename from src/commands/dev/headers.rs rename to src/commands/dev/gcs/headers.rs diff --git a/src/commands/dev/gcs/mod.rs b/src/commands/dev/gcs/mod.rs new file mode 100644 index 000000000..6ef96916b --- /dev/null +++ b/src/commands/dev/gcs/mod.rs @@ -0,0 +1,63 @@ +mod headers; +mod server; +mod setup; +mod watch; + +use server::serve; +use setup::{get_preview_id, get_session_id}; +use watch::watch_for_changes; + +use crate::commands; +use crate::commands::dev::{socket, ServerConfig}; +use crate::settings::toml::Target; + +use std::sync::{Arc, Mutex}; +use std::thread; + +use tokio::runtime::Runtime as TokioRuntime; +use url::Url; + +pub fn dev( + target: Target, + server_config: ServerConfig, + verbose: bool, +) -> Result<(), failure::Error> { + commands::build(&target)?; + let session_id = get_session_id()?; + let preview_id = get_preview_id( + target.clone(), + None, + &server_config, + &session_id.clone(), + verbose, + )?; + let preview_id = Arc::new(Mutex::new(preview_id)); + + { + let session_id = session_id.clone(); + let preview_id = preview_id.clone(); + let server_config = server_config.clone(); + thread::spawn(move || { + watch_for_changes( + target, + None, + &server_config, + Arc::clone(&preview_id), + &session_id, + verbose, + ) + }); + } + let socket_url = format!("wss://rawhttp.cloudflareworkers.com/inspect/{}", session_id); + let socket_url = Url::parse(&socket_url)?; + let devtools_listener = socket::listen(socket_url); + let server = serve(server_config, Arc::clone(&preview_id)); + + let runners = futures::future::join(devtools_listener, server); + let mut runtime = TokioRuntime::new()?; + runtime.block_on(async { + let (devtools_listener, server) = runners.await; + devtools_listener?; + server + }) +} diff --git a/src/commands/dev/gcs/server.rs b/src/commands/dev/gcs/server.rs new file mode 100644 index 000000000..cbb3a08c4 --- /dev/null +++ b/src/commands/dev/gcs/server.rs @@ -0,0 +1,115 @@ +use crate::commands::dev::gcs::headers::{destructure_response, structure_request}; +use crate::commands::dev::server_config::ServerConfig; +use crate::terminal::emoji; + +use std::sync::{Arc, Mutex}; + +use chrono::prelude::*; +use hyper::client::{HttpConnector, ResponseFuture}; +use hyper::header::{HeaderName, HeaderValue}; +use hyper::http::uri::InvalidUri; +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Client as HyperClient, Request, Response, Server, Uri}; +use hyper_tls::HttpsConnector; + +const PREVIEW_HOST: &str = "rawhttp.cloudflareworkers.com"; + +pub(super) async fn serve( + server_config: ServerConfig, + preview_id: Arc>, +) -> Result<(), failure::Error> { +// set up https client to connect to the preview service +let https = HttpsConnector::new(); +let client = HyperClient::builder().build::<_, Body>(https); + +let listening_address = server_config.listening_address.clone(); +// create a closure that hyper will use later to handle HTTP requests +let make_service = make_service_fn(move |_| { + let client = client.to_owned(); + let preview_id = preview_id.lock().unwrap().to_owned(); + let server_config = server_config.to_owned(); + async move { + Ok::<_, failure::Error>(service_fn(move |req| { + let client = client.to_owned(); + let preview_id = preview_id.to_owned(); + let server_config = server_config.to_owned(); + let version = req.version(); + let (parts, body) = req.into_parts(); + let req_method = parts.method.to_string(); + let now: DateTime = Local::now(); + let path = get_path_as_str(&parts.uri); + async move { + let resp = preview_request( + Request::from_parts(parts, body), + client, + preview_id.to_owned(), + ) + .await?; + let (mut parts, body) = resp.into_parts(); + + destructure_response(&mut parts)?; + let resp = Response::from_parts(parts, body); + + println!( + "[{}] {} {}{} {:?} {}", + now.format("%Y-%m-%d %H:%M:%S"), + req_method, + server_config.host, + path, + version, + resp.status() + ); + Ok::<_, failure::Error>(resp) + } + })) + } +}); + +let server = Server::bind(&listening_address.address).serve(make_service); +println!("{} Listening on http://{}", emoji::EAR, listening_address); +if let Err(e) = server.await { + eprintln!("server error: {}", e); +} +Ok(()) +} + +fn get_preview_url(path_string: &str) -> Result { + format!("https://{}{}", PREVIEW_HOST, path_string).parse() +} + +fn get_path_as_str(uri: &Uri) -> String { + uri.path_and_query() + .map(|x| x.as_str()) + .unwrap_or("") + .to_string() +} + +fn preview_request( + req: Request, + client: HyperClient>, + preview_id: String, + server_config: ServerConfig, +) -> ResponseFuture { + let (mut parts, body) = req.into_parts(); + + let path = get_path_as_str(&parts.uri); + let preview_id = &preview_id; + + structure_request(&mut parts); + + parts.headers.insert( + HeaderName::from_static("host"), + HeaderValue::from_static(PREVIEW_HOST), + ); + + parts.headers.insert( + HeaderName::from_static("cf-ew-preview"), + HeaderValue::from_str(preview_id).expect("Could not create header for preview id"), + ); + + parts.uri = get_preview_url(&path).expect("Could not get preview url"); + + let req = Request::from_parts(parts, body); + + client.request(req) +} diff --git a/src/commands/dev/gcs/setup.rs b/src/commands/dev/gcs/setup.rs new file mode 100644 index 000000000..3ce7573bd --- /dev/null +++ b/src/commands/dev/gcs/setup.rs @@ -0,0 +1,28 @@ +use uuid::Uuid; + +use crate::commands::dev::ServerConfig; +use crate::commands::preview::upload; +use crate::settings::global_user::GlobalUser; +use crate::settings::toml::Target; + +pub(super) fn get_preview_id( + mut target: Target, + user: Option, + server_config: &ServerConfig, + session_id: &str, + verbose: bool, +) -> Result { + let sites_preview = false; + let script_id = upload(&mut target, user.as_ref(), sites_preview, verbose)?; + Ok(format!( + "{}{}{}{}", + &script_id, + session_id, + server_config.host.is_https() as u8, + server_config.host + )) +} + +pub(super) fn get_session_id() -> Result { + Ok(Uuid::new_v4().to_simple().to_string()) +} diff --git a/src/commands/dev/watch.rs b/src/commands/dev/gcs/watch.rs similarity index 84% rename from src/commands/dev/watch.rs rename to src/commands/dev/gcs/watch.rs index aba8237bd..d4a4a38a4 100644 --- a/src/commands/dev/watch.rs +++ b/src/commands/dev/gcs/watch.rs @@ -1,8 +1,9 @@ use std::sync::{mpsc, Arc, Mutex}; use crate::commands; -use crate::commands::dev::get_preview_id; +use crate::commands::dev::gcs::setup::get_preview_id; use crate::commands::dev::server_config::ServerConfig; +use crate::commands::watch_and_build; use crate::settings::global_user::GlobalUser; use crate::settings::toml::Target; @@ -16,7 +17,7 @@ pub fn watch_for_changes( verbose: bool, ) -> Result<(), failure::Error> { let (sender, receiver) = mpsc::channel(); - commands::watch_and_build(&target, Some(sender))?; + watch_and_build(&target, Some(sender))?; while let Ok(_) = receiver.recv() { let user = user.clone(); diff --git a/src/commands/dev/mod.rs b/src/commands/dev/mod.rs index 838e13627..f9aaaef3f 100644 --- a/src/commands/dev/mod.rs +++ b/src/commands/dev/mod.rs @@ -1,38 +1,13 @@ +mod edge; +mod gcs; mod server_config; mod socket; -use server_config::ServerConfig; -mod headers; -use headers::{destructure_response, structure_request}; -mod watch; -use watch::watch_for_changes; - -use std::sync::{Arc, Mutex}; -use std::thread; - -use chrono::prelude::*; - -use hyper::client::{HttpConnector, ResponseFuture}; -use hyper::header::{HeaderName, HeaderValue}; - -use hyper::http::uri::InvalidUri; -use hyper::service::{make_service_fn, service_fn}; -use hyper::{Body, Client as HyperClient, Request, Response, Server, Uri}; - -use hyper_tls::HttpsConnector; -use tokio::runtime::Runtime as TokioRuntime; - -use uuid::Uuid; - -use crate::commands; -use crate::commands::preview::upload; +use server_config::ServerConfig; use crate::settings::global_user::GlobalUser; use crate::settings::toml::Target; - -use crate::terminal::{emoji, message_box}; - -const PREVIEW_HOST: &str = "rawhttp.cloudflareworkers.com"; +use crate::terminal::message_box; pub fn dev( target: Target, @@ -43,165 +18,9 @@ pub fn dev( verbose: bool, ) -> Result<(), failure::Error> { message_box::dev_alpha_warning(); - commands::build(&target)?; let server_config = ServerConfig::new(host, ip, port)?; - let session_id = get_session_id()?; - let preview_id = get_preview_id( - target.clone(), - user.clone(), - &server_config, - &session_id.clone(), - verbose, - )?; - let preview_id = Arc::new(Mutex::new(preview_id)); - - { - let session_id = session_id.clone(); - let preview_id = preview_id.clone(); - let server_config = server_config.clone(); - thread::spawn(move || { - watch_for_changes( - target, - user, - &server_config, - Arc::clone(&preview_id), - &session_id, - verbose, - ) - }); + match user { + Some(user) => edge::dev(target, user, server_config, verbose), + None => gcs::dev(target, server_config, verbose), } - - let mut runtime = TokioRuntime::new()?; - - let devtools_listener = socket::listen(&session_id); - let server = serve(server_config, Arc::clone(&preview_id)); - - let runners = futures::future::join(devtools_listener, server); - - runtime.block_on(async { - let (devtools_listener, server) = runners.await; - devtools_listener?; - server - }) -} - -async fn serve( - server_config: ServerConfig, - preview_id: Arc>, -) -> Result<(), failure::Error> { - // set up https client to connect to the preview service - let https = HttpsConnector::new(); - let client = HyperClient::builder().build::<_, Body>(https); - - let listening_address = server_config.listening_address.clone(); - // create a closure that hyper will use later to handle HTTP requests - let make_service = make_service_fn(move |_| { - let client = client.to_owned(); - let preview_id = preview_id.lock().unwrap().to_owned(); - let server_config = server_config.to_owned(); - async move { - Ok::<_, failure::Error>(service_fn(move |req| { - let client = client.to_owned(); - let preview_id = preview_id.to_owned(); - let server_config = server_config.to_owned(); - let version = req.version(); - let (parts, body) = req.into_parts(); - let req_method = parts.method.to_string(); - let now: DateTime = Local::now(); - let path = get_path_as_str(&parts.uri); - async move { - let resp = preview_request( - Request::from_parts(parts, body), - client, - preview_id.to_owned(), - ) - .await?; - let (mut parts, body) = resp.into_parts(); - - destructure_response(&mut parts)?; - let resp = Response::from_parts(parts, body); - - println!( - "[{}] {} {}{} {:?} {}", - now.format("%Y-%m-%d %H:%M:%S"), - req_method, - server_config.host, - path, - version, - resp.status() - ); - Ok::<_, failure::Error>(resp) - } - })) - } - }); - - let server = Server::bind(&listening_address.address).serve(make_service); - println!("{} Listening on http://{}", emoji::EAR, listening_address); - if let Err(e) = server.await { - eprintln!("server error: {}", e); - } - Ok(()) -} - -fn get_preview_url(path_string: &str) -> Result { - format!("https://{}{}", PREVIEW_HOST, path_string).parse() -} - -fn get_path_as_str(uri: &Uri) -> String { - uri.path_and_query() - .map(|x| x.as_str()) - .unwrap_or("") - .to_string() -} - -fn preview_request( - req: Request, - client: HyperClient>, - preview_id: String, -) -> ResponseFuture { - let (mut parts, body) = req.into_parts(); - - let path = get_path_as_str(&parts.uri); - let preview_id = &preview_id; - - structure_request(&mut parts); - - parts.headers.insert( - HeaderName::from_static("host"), - HeaderValue::from_static(PREVIEW_HOST), - ); - - parts.headers.insert( - HeaderName::from_static("cf-ew-preview"), - HeaderValue::from_str(preview_id).expect("Could not create header for preview id"), - ); - - parts.uri = get_preview_url(&path).expect("Could not get preview url"); - - let req = Request::from_parts(parts, body); - - client.request(req) -} - -fn get_session_id() -> Result { - Ok(Uuid::new_v4().to_simple().to_string()) -} - -pub fn get_preview_id( - mut target: Target, - user: Option, - server_config: &ServerConfig, - session_id: &str, - verbose: bool, -) -> Result { - let sites_preview = false; - let script_id = upload(&mut target, user.as_ref(), sites_preview, verbose)?; - Ok(format!( - "{}{}{}{}", - &script_id, - session_id, - server_config.host.is_https() as u8, - server_config.host - )) } diff --git a/src/commands/dev/server_config/host.rs b/src/commands/dev/server_config/host.rs index 6f0f572a6..6b44f626e 100644 --- a/src/commands/dev/server_config/host.rs +++ b/src/commands/dev/server_config/host.rs @@ -3,7 +3,7 @@ use std::fmt; use failure::format_err; use url::Url; -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct Host { url: Url, } diff --git a/src/commands/dev/server_config/listening_address.rs b/src/commands/dev/server_config/listening_address.rs index 161b12c3d..a74f89eae 100644 --- a/src/commands/dev/server_config/listening_address.rs +++ b/src/commands/dev/server_config/listening_address.rs @@ -3,7 +3,7 @@ use std::net::{SocketAddr, ToSocketAddrs}; use failure::format_err; -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct ListeningAddress { pub address: SocketAddr, } diff --git a/src/commands/dev/server_config/mod.rs b/src/commands/dev/server_config/mod.rs index b30bba893..79114bb06 100644 --- a/src/commands/dev/server_config/mod.rs +++ b/src/commands/dev/server_config/mod.rs @@ -4,7 +4,7 @@ mod listening_address; use host::Host; use listening_address::ListeningAddress; -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct ServerConfig { pub host: Host, pub listening_address: ListeningAddress, diff --git a/src/commands/dev/socket.rs b/src/commands/dev/socket.rs index 7fb9d3f32..c32044d49 100644 --- a/src/commands/dev/socket.rs +++ b/src/commands/dev/socket.rs @@ -12,10 +12,7 @@ use url::Url; const KEEP_ALIVE_INTERVAL: u64 = 10; -pub async fn listen(session_id: &str) -> Result<(), failure::Error> { - let socket_url = format!("wss://rawhttp.cloudflareworkers.com/inspect/{}", session_id); - let socket_url = Url::parse(&socket_url)?; - +pub async fn listen(socket_url: Url) -> Result<(), failure::Error> { let (ws_stream, _) = connect_async(socket_url) .await .expect("Failed to connect to devtools instance"); From 0ee938ddd70def50b0e36eb430950c06fdd69864 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Thu, 20 Feb 2020 15:41:26 -0600 Subject: [PATCH 2/5] cargo fmt --- src/commands/dev/gcs/server.rs | 105 ++++++++++++++++----------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/src/commands/dev/gcs/server.rs b/src/commands/dev/gcs/server.rs index cbb3a08c4..7da4b2388 100644 --- a/src/commands/dev/gcs/server.rs +++ b/src/commands/dev/gcs/server.rs @@ -18,59 +18,59 @@ pub(super) async fn serve( server_config: ServerConfig, preview_id: Arc>, ) -> Result<(), failure::Error> { -// set up https client to connect to the preview service -let https = HttpsConnector::new(); -let client = HyperClient::builder().build::<_, Body>(https); - -let listening_address = server_config.listening_address.clone(); -// create a closure that hyper will use later to handle HTTP requests -let make_service = make_service_fn(move |_| { - let client = client.to_owned(); - let preview_id = preview_id.lock().unwrap().to_owned(); - let server_config = server_config.to_owned(); - async move { - Ok::<_, failure::Error>(service_fn(move |req| { - let client = client.to_owned(); - let preview_id = preview_id.to_owned(); - let server_config = server_config.to_owned(); - let version = req.version(); - let (parts, body) = req.into_parts(); - let req_method = parts.method.to_string(); - let now: DateTime = Local::now(); - let path = get_path_as_str(&parts.uri); - async move { - let resp = preview_request( - Request::from_parts(parts, body), - client, - preview_id.to_owned(), - ) - .await?; - let (mut parts, body) = resp.into_parts(); - - destructure_response(&mut parts)?; - let resp = Response::from_parts(parts, body); - - println!( - "[{}] {} {}{} {:?} {}", - now.format("%Y-%m-%d %H:%M:%S"), - req_method, - server_config.host, - path, - version, - resp.status() - ); - Ok::<_, failure::Error>(resp) - } - })) + // set up https client to connect to the preview service + let https = HttpsConnector::new(); + let client = HyperClient::builder().build::<_, Body>(https); + + let listening_address = server_config.listening_address.clone(); + // create a closure that hyper will use later to handle HTTP requests + let make_service = make_service_fn(move |_| { + let client = client.to_owned(); + let preview_id = preview_id.lock().unwrap().to_owned(); + let server_config = server_config.to_owned(); + async move { + Ok::<_, failure::Error>(service_fn(move |req| { + let client = client.to_owned(); + let preview_id = preview_id.to_owned(); + let server_config = server_config.to_owned(); + let version = req.version(); + let (parts, body) = req.into_parts(); + let req_method = parts.method.to_string(); + let now: DateTime = Local::now(); + let path = get_path_as_str(&parts.uri); + async move { + let resp = preview_request( + Request::from_parts(parts, body), + client, + preview_id.to_owned(), + ) + .await?; + let (mut parts, body) = resp.into_parts(); + + destructure_response(&mut parts)?; + let resp = Response::from_parts(parts, body); + + println!( + "[{}] {} {}{} {:?} {}", + now.format("%Y-%m-%d %H:%M:%S"), + req_method, + server_config.host, + path, + version, + resp.status() + ); + Ok::<_, failure::Error>(resp) + } + })) + } + }); + + let server = Server::bind(&listening_address.address).serve(make_service); + println!("{} Listening on http://{}", emoji::EAR, listening_address); + if let Err(e) = server.await { + eprintln!("server error: {}", e); } -}); - -let server = Server::bind(&listening_address.address).serve(make_service); -println!("{} Listening on http://{}", emoji::EAR, listening_address); -if let Err(e) = server.await { - eprintln!("server error: {}", e); -} -Ok(()) + Ok(()) } fn get_preview_url(path_string: &str) -> Result { @@ -88,7 +88,6 @@ fn preview_request( req: Request, client: HyperClient>, preview_id: String, - server_config: ServerConfig, ) -> ResponseFuture { let (mut parts, body) = req.into_parts(); From 7b2616c45997446d061d24f6b2da3f89a4680240 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Fri, 17 Jul 2020 14:30:56 -0500 Subject: [PATCH 3/5] [dev] route authenticated dev requests to the edge (#1098) Co-authored-by: Josh Wheeler --- src/commands/dev/edge/mod.rs | 30 ++++- src/commands/dev/edge/server.rs | 102 +++++++++++++++- src/commands/dev/edge/setup.rs | 186 ++++++++++++++++++++++++++++- src/commands/dev/gcs/mod.rs | 2 + src/commands/dev/gcs/server.rs | 18 +-- src/commands/dev/mod.rs | 6 +- src/commands/dev/utils.rs | 8 ++ src/main.rs | 3 +- src/preview/upload.rs | 5 +- src/settings/toml/deploy_config.rs | 12 +- src/upload/form/mod.rs | 28 ++++- src/upload/mod.rs | 2 +- 12 files changed, 363 insertions(+), 39 deletions(-) create mode 100644 src/commands/dev/utils.rs diff --git a/src/commands/dev/edge/mod.rs b/src/commands/dev/edge/mod.rs index 3336f3fb4..346bc8e85 100644 --- a/src/commands/dev/edge/mod.rs +++ b/src/commands/dev/edge/mod.rs @@ -1,16 +1,34 @@ +mod server; +mod setup; + +use server::serve; +use setup::Session; + use crate::commands::dev::ServerConfig; use crate::settings::global_user::GlobalUser; -use crate::settings::toml::Target; +use crate::settings::toml::{DeployConfig, Target}; + +use tokio::runtime::Runtime as TokioRuntime; pub fn dev( target: Target, user: GlobalUser, server_config: ServerConfig, + deploy_config: DeployConfig, verbose: bool, ) -> Result<(), failure::Error> { - println!( - "{:?}\n{:?}\n{:?}\n{:?}", - target, user, server_config, verbose - ); - Ok(()) + let session = Session::new(&target, &user, &deploy_config)?; + let mut target = target; + + let preview_token = setup::upload( + &mut target, + &deploy_config, + &user, + session.preview_token, + verbose, + )?; + + let server = serve(server_config, preview_token, session.host); + let mut runtime = TokioRuntime::new()?; + runtime.block_on(server) } diff --git a/src/commands/dev/edge/server.rs b/src/commands/dev/edge/server.rs index 6a227cea0..11a4bb4d7 100644 --- a/src/commands/dev/edge/server.rs +++ b/src/commands/dev/edge/server.rs @@ -1,3 +1,99 @@ -pub(super) async fn serve() -> Result<(), failure::Error> { - unimplemented!() -} \ No newline at end of file +use crate::commands::dev::server_config::ServerConfig; +use crate::commands::dev::utils::get_path_as_str; +use crate::terminal::emoji; + +use chrono::prelude::*; +use hyper::client::{HttpConnector, ResponseFuture}; +use hyper::header::{HeaderName, HeaderValue}; +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Client as HyperClient, Request, Server}; +use hyper_tls::HttpsConnector; + +pub(super) async fn serve( + server_config: ServerConfig, + preview_token: String, + host: String, +) -> Result<(), failure::Error> { + // set up https client to connect to the preview service + let https = HttpsConnector::new(); + let client = HyperClient::builder().build::<_, Body>(https); + + let listening_address = server_config.listening_address; + + // create a closure that hyper will use later to handle HTTP requests + let make_service = make_service_fn(move |_| { + let client = client.to_owned(); + let preview_token = preview_token.to_owned(); + let host = host.to_owned(); + + async move { + Ok::<_, failure::Error>(service_fn(move |req| { + let client = client.to_owned(); + let preview_token = preview_token.to_owned(); + let host = host.to_owned(); + let version = req.version(); + let (parts, body) = req.into_parts(); + let req_method = parts.method.to_string(); + let now: DateTime = Local::now(); + let path = get_path_as_str(&parts.uri); + async move { + let resp = preview_request( + Request::from_parts(parts, body), + client, + preview_token.to_owned(), + host.clone(), + ) + .await?; + + println!( + "[{}] {} {}{} {:?} {}", + now.format("%Y-%m-%d %H:%M:%S"), + req_method, + host, + path, + version, + resp.status() + ); + Ok::<_, failure::Error>(resp) + } + })) + } + }); + + let server = Server::bind(&listening_address).serve(make_service); + println!("{} Listening on http://{}", emoji::EAR, listening_address); + if let Err(e) = server.await { + eprintln!("server error: {}", e) + } + Ok(()) +} + +fn preview_request( + req: Request, + client: HyperClient>, + preview_token: String, + host: String, +) -> ResponseFuture { + let (mut parts, body) = req.into_parts(); + + let path = get_path_as_str(&parts.uri); + + parts.headers.insert( + HeaderName::from_static("host"), + HeaderValue::from_str(&host).expect("Could not create host header"), + ); + + parts.headers.insert( + HeaderName::from_static("cf-workers-preview-token"), + HeaderValue::from_str(&preview_token).expect("Could not create token header"), + ); + + // TODO: figure out how to http _or_ https + parts.uri = format!("https://{}{}", host, path) + .parse() + .expect("Could not construct preview url"); + + let req = Request::from_parts(parts, body); + + client.request(req) +} diff --git a/src/commands/dev/edge/setup.rs b/src/commands/dev/edge/setup.rs index 761c7e88b..5c0116f1a 100644 --- a/src/commands/dev/edge/setup.rs +++ b/src/commands/dev/edge/setup.rs @@ -1,3 +1,185 @@ -pub(super) fn get_preview_id() -> Result { - unimplemented!() +use std::path::Path; + +use crate::kv::bulk; +use crate::settings::global_user::GlobalUser; +use crate::settings::toml::{DeployConfig, Target}; +use crate::sites::{add_namespace, sync}; +use crate::terminal::message; +use crate::upload; + +use reqwest::Url; +use serde::{Deserialize, Serialize}; +use serde_json::json; + +pub(super) fn upload( + target: &mut Target, + deploy_config: &DeployConfig, + user: &GlobalUser, + preview_token: String, + verbose: bool, +) -> Result { + let client = crate::http::legacy_auth_client(&user); + + let (to_delete, asset_manifest, site_namespace_id) = if let Some(site_config) = + target.site.clone() + { + let site_namespace = add_namespace(user, target, true)?; + let path = Path::new(&site_config.bucket); + let (to_upload, to_delete, asset_manifest) = sync(target, user, &site_namespace.id, path)?; + + // First, upload all existing files in given directory + if verbose { + message::info("Uploading updated files..."); + } + + bulk::put(target, user, &site_namespace.id, to_upload, &None)?; + (to_delete, Some(asset_manifest), Some(site_namespace.id)) + } else { + (Vec::new(), None, None) + }; + + let session_config = get_session_config(deploy_config); + let address = get_upload_address(target); + + let script_upload_form = upload::form::build(target, asset_manifest, Some(session_config))?; + + let response = client + .post(&address) + .header("cf-preview-upload-config-token", preview_token) + .multipart(script_upload_form) + .send()? + .error_for_status()?; + + if !to_delete.is_empty() { + if verbose { + message::info("Deleting stale files..."); + } + + bulk::delete(target, user, &site_namespace_id.unwrap(), to_delete, &None)?; + } + + let text = &response.text()?; + + // TODO: use cloudflare-rs for this :) + let response: PreviewV4ApiResponse = serde_json::from_str(text)?; + Ok(response.result.preview_token) +} + +pub struct Session { + pub host: String, + pub websocket_url: Url, + pub preview_token: String, +} + +impl Session { + pub fn new( + target: &Target, + user: &GlobalUser, + deploy_config: &DeployConfig, + ) -> Result { + let exchange_url = get_exchange_url(deploy_config, 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 namespaces: Vec<&str> = host.as_str().split('.').collect(); + let subdomain = namespaces[1]; + format!("{}.{}.workers.dev", target.name, subdomain) + } + }; + + let client = crate::http::legacy_auth_client(&user); + let response = client.get(exchange_url).send()?.error_for_status()?; + let text = &response.text()?; + let response: InspectorV4ApiResponse = serde_json::from_str(text)?; + let websocket_url = Url::parse(&response.inspector_websocket)?; + let preview_token = response.token; + + Ok(Session { + host, + websocket_url, + preview_token, + }) + } +} + +fn get_session_config(deploy_config: &DeployConfig) -> serde_json::Value { + match deploy_config { + DeployConfig::Zoned(config) => { + let mut routes: Vec = Vec::new(); + for route in &config.routes { + routes.push(route.pattern.clone()); + } + json!({ "routes": routes }) + } + DeployConfig::Zoneless(_) => json!({"workers_dev": true}), + } +} + +fn get_session_address(deploy_config: &DeployConfig) -> String { + match deploy_config { + DeployConfig::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!( + "https://api.cloudflare.com/client/v4/accounts/{}/workers/subdomain/edge-preview", + config.account_id + ), + } +} + +fn get_upload_address(target: &mut Target) -> String { + format!( + "https://api.cloudflare.com/client/v4/accounts/{}/workers/scripts/{}/edge-preview", + target.account_id, target.name + ) +} + +fn get_exchange_url( + deploy_config: &DeployConfig, + user: &GlobalUser, +) -> Result { + let client = crate::http::legacy_auth_client(&user); + let address = get_session_address(deploy_config); + let url = Url::parse(&address)?; + let response = client.get(url).send()?.error_for_status()?; + let text = &response.text()?; + let response: SessionV4ApiResponse = serde_json::from_str(text)?; + let url = Url::parse(&response.result.exchange_url)?; + Ok(url) +} + +#[derive(Debug, Serialize, Deserialize)] +struct SessionResponse { + pub exchange_url: String, + pub token: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct SessionV4ApiResponse { + pub result: SessionResponse, +} + +#[derive(Debug, Serialize, Deserialize)] +struct InspectorV4ApiResponse { + pub inspector_websocket: String, + pub token: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Preview { + pub preview_token: String, +} + +#[derive(Debug, Serialize, Deserialize)] +struct PreviewV4ApiResponse { + pub result: Preview, } diff --git a/src/commands/dev/gcs/mod.rs b/src/commands/dev/gcs/mod.rs index 983c19a98..c89fa3500 100644 --- a/src/commands/dev/gcs/mod.rs +++ b/src/commands/dev/gcs/mod.rs @@ -22,6 +22,8 @@ pub fn dev( server_config: ServerConfig, verbose: bool, ) -> Result<(), failure::Error> { + println!("unauthenticated"); + // setup the session let session_id = get_session_id()?; diff --git a/src/commands/dev/gcs/server.rs b/src/commands/dev/gcs/server.rs index 887d8df93..06a3e0f4e 100644 --- a/src/commands/dev/gcs/server.rs +++ b/src/commands/dev/gcs/server.rs @@ -1,5 +1,6 @@ use crate::commands::dev::gcs::headers::{destructure_response, structure_request}; -use crate::commands::dev::ServerConfig; +use crate::commands::dev::server_config::ServerConfig; +use crate::commands::dev::utils::get_path_as_str; use crate::terminal::emoji; use std::sync::{Arc, Mutex}; @@ -96,6 +97,10 @@ pub(super) async fn serve( Ok(()) } +fn get_preview_url(path_string: &str) -> Result { + format!("https://{}{}", PREVIEW_HOST, path_string).parse() +} + fn preview_request( req: Request, client: HyperClient>, @@ -124,14 +129,3 @@ fn preview_request( client.request(req) } - -fn get_preview_url(path_string: &str) -> Result { - format!("https://{}{}", PREVIEW_HOST, path_string).parse() -} - -fn get_path_as_str(uri: &Uri) -> String { - uri.path_and_query() - .map(|x| x.as_str()) - .unwrap_or("") - .to_string() -} diff --git a/src/commands/dev/mod.rs b/src/commands/dev/mod.rs index 39c178be2..a8b411d1d 100644 --- a/src/commands/dev/mod.rs +++ b/src/commands/dev/mod.rs @@ -2,18 +2,20 @@ mod edge; mod gcs; mod server_config; mod socket; +mod utils; use server_config::ServerConfig; use crate::build; use crate::settings::global_user::GlobalUser; -use crate::settings::toml::Target; +use crate::settings::toml::{DeployConfig, Target}; use crate::terminal::{message, styles}; /// `wrangler dev` starts a server on a dev machine that routes incoming HTTP requests /// to a Cloudflare Workers runtime and returns HTTP responses pub fn dev( target: Target, + deploy_config: DeployConfig, user: Option, host: Option<&str>, port: Option, @@ -30,7 +32,7 @@ pub fn dev( match user { // authenticated users connect to the edge - Some(user) => edge::dev(target, user, server_config, verbose), + Some(user) => edge::dev(target, user, server_config, deploy_config, verbose), // unauthenticated users connect to gcs None => gcs::dev(target, server_config, verbose), diff --git a/src/commands/dev/utils.rs b/src/commands/dev/utils.rs new file mode 100644 index 000000000..840ef950a --- /dev/null +++ b/src/commands/dev/utils.rs @@ -0,0 +1,8 @@ +use hyper::Uri; + +pub(super) fn get_path_as_str(uri: &Uri) -> String { + uri.path_and_query() + .map(|x| x.as_str()) + .unwrap_or("") + .to_string() +} diff --git a/src/main.rs b/src/main.rs index 15f05ee4c..ada707707 100644 --- a/src/main.rs +++ b/src/main.rs @@ -760,11 +760,12 @@ fn run() -> Result<(), failure::Error> { ); let manifest = settings::toml::Manifest::new(config_path)?; let env = matches.value_of("env"); + let deploy_config = manifest.deploy_config(env)?; is_preview = true; let target = manifest.get_target(env, is_preview)?; let user = settings::global_user::GlobalUser::new().ok(); let verbose = matches.is_present("verbose"); - commands::dev::dev(target, user, host, port, ip, verbose)?; + commands::dev::dev(target, deploy_config, user, host, port, ip, verbose)?; } else if matches.subcommand_matches("whoami").is_some() { log::info!("Getting User settings"); let user = settings::global_user::GlobalUser::new()?; diff --git a/src/preview/upload.rs b/src/preview/upload.rs index 7252268c2..c2b3ad66e 100644 --- a/src/preview/upload.rs +++ b/src/preview/upload.rs @@ -151,7 +151,7 @@ fn authenticated_upload( ); log::info!("address: {}", create_address); - let script_upload_form = upload::form::build(target, asset_manifest)?; + let script_upload_form = upload::form::build(target, asset_manifest, None)?; let res = client .post(&create_address) @@ -187,6 +187,7 @@ fn unauthenticated_upload(target: &Target) -> Result { message::warn( "KV Namespaces are not supported in preview without setting API credentials and account_id", ); + target.kv_namespaces = Vec::new(); } if target.site.is_some() { @@ -196,7 +197,7 @@ fn unauthenticated_upload(target: &Target) -> Result { target.site = None; } - let script_upload_form = upload::form::build(&target, None)?; + let script_upload_form = upload::form::build(&target, None, None)?; let client = http::client(); let res = client .post(create_address) diff --git a/src/settings/toml/deploy_config.rs b/src/settings/toml/deploy_config.rs index 58a59a068..73faf9ae8 100644 --- a/src/settings/toml/deploy_config.rs +++ b/src/settings/toml/deploy_config.rs @@ -1,6 +1,12 @@ use crate::settings::toml::Route; use crate::terminal::message; +#[derive(Clone, Debug, PartialEq)] +pub enum DeployConfig { + Zoneless(Zoneless), + Zoned(Zoned), +} + impl DeployConfig { pub fn build( script_name: &str, @@ -91,12 +97,6 @@ impl DeployConfig { } } -#[derive(Clone, Debug, PartialEq)] -pub enum DeployConfig { - Zoneless(Zoneless), - Zoned(Zoned), -} - #[derive(Clone, Debug, PartialEq)] pub struct Zoneless { pub account_id: String, diff --git a/src/upload/form/mod.rs b/src/upload/form/mod.rs index 99e6daa53..09de2c0e8 100644 --- a/src/upload/form/mod.rs +++ b/src/upload/form/mod.rs @@ -25,6 +25,7 @@ use super::{krate, Package}; pub fn build( target: &Target, asset_manifest: Option, + session_config: Option, ) -> Result { let target_type = &target.target_type; let kv_namespaces = &target.kv_namespaces; @@ -60,7 +61,7 @@ pub fn build( plain_texts, )?; - build_form(&assets) + build_form(&assets, session_config) } TargetType::JavaScript => { log::info!("JavaScript project detected. Publishing..."); @@ -77,7 +78,7 @@ pub fn build( plain_texts, )?; - build_form(&assets) + build_form(&assets, session_config) } TargetType::Webpack => { log::info!("webpack project detected. Publishing..."); @@ -110,7 +111,7 @@ pub fn build( plain_texts, )?; - build_form(&assets) + build_form(&assets, session_config) } } } @@ -120,13 +121,19 @@ fn get_asset_manifest_blob(asset_manifest: AssetManifest) -> Result Result { +fn build_form( + assets: &ProjectAssets, + session_config: Option, +) -> Result { let mut form = Form::new(); // The preview service in particular streams the request form, and requires that the // "metadata" part be set first, so this order is important. form = add_metadata(form, assets)?; form = add_files(form, assets)?; + if let Some(session_config) = session_config { + form = add_session_config(form, session_config)? + } log::info!("building form"); log::info!("{:?}", &form); @@ -167,6 +174,19 @@ fn add_metadata(mut form: Form, assets: &ProjectAssets) -> Result Result { + let wrangler_session_config = Part::text((session_config).to_string()) + .file_name("") + .mime_str("application/json")?; + + form = form.part("wrangler-session-config", wrangler_session_config); + + Ok(form) +} + fn filename_from_path(path: &PathBuf) -> Option { path.file_stem()?.to_str().map(|s| s.to_string()) } diff --git a/src/upload/mod.rs b/src/upload/mod.rs index faf1e88c5..1cfdf3861 100644 --- a/src/upload/mod.rs +++ b/src/upload/mod.rs @@ -19,7 +19,7 @@ pub fn script( target.account_id, target.name, ); - let script_upload_form = form::build(target, asset_manifest)?; + let script_upload_form = form::build(target, asset_manifest, None)?; let res = client .put(&worker_addr) From fc5bad30f47aad9f85db4ab4422eacfde74194c1 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Tue, 28 Jul 2020 16:33:45 -0500 Subject: [PATCH 4/5] [dev] connect to edge websocket (#1291) --- src/commands/dev/edge/mod.rs | 20 ++++++++++++++------ src/commands/dev/edge/setup.rs | 7 ++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/commands/dev/edge/mod.rs b/src/commands/dev/edge/mod.rs index 346bc8e85..3c328a622 100644 --- a/src/commands/dev/edge/mod.rs +++ b/src/commands/dev/edge/mod.rs @@ -2,9 +2,9 @@ mod server; mod setup; use server::serve; -use setup::Session; +use setup::{upload, Session}; -use crate::commands::dev::ServerConfig; +use crate::commands::dev::{socket, ServerConfig}; use crate::settings::global_user::GlobalUser; use crate::settings::toml::{DeployConfig, Target}; @@ -20,15 +20,23 @@ pub fn dev( let session = Session::new(&target, &user, &deploy_config)?; let mut target = target; - let preview_token = setup::upload( + let preview_token = upload( &mut target, &deploy_config, &user, - session.preview_token, + session.preview_token.clone(), verbose, )?; - let server = serve(server_config, preview_token, session.host); let mut runtime = TokioRuntime::new()?; - runtime.block_on(server) + runtime.block_on(async { + let devtools_listener = tokio::spawn(socket::listen(session.websocket_url)); + let server = tokio::spawn(serve(server_config, preview_token, session.host)); + let res = tokio::try_join!(async { devtools_listener.await? }, async { server.await? }); + + match res { + Ok(_) => Ok(()), + Err(e) => Err(e), + } + }) } diff --git a/src/commands/dev/edge/setup.rs b/src/commands/dev/edge/setup.rs index 5c0116f1a..75533dd3c 100644 --- a/src/commands/dev/edge/setup.rs +++ b/src/commands/dev/edge/setup.rs @@ -65,6 +65,7 @@ pub(super) fn upload( Ok(response.result.preview_token) } +#[derive(Debug, Clone)] pub struct Session { pub host: String, pub websocket_url: Url, @@ -98,7 +99,11 @@ impl Session { let response = client.get(exchange_url).send()?.error_for_status()?; let text = &response.text()?; let response: InspectorV4ApiResponse = serde_json::from_str(text)?; - let websocket_url = Url::parse(&response.inspector_websocket)?; + let full_url = format!( + "{}?{}={}", + &response.inspector_websocket, "cf_workers_preview_token", &response.token + ); + let websocket_url = Url::parse(&full_url)?; let preview_token = response.token; Ok(Session { From 8a687bf9123ed351cee74ce097431d671b2432f8 Mon Sep 17 00:00:00 2001 From: Avery Harnish Date: Wed, 29 Jul 2020 14:31:15 -0500 Subject: [PATCH 5/5] [dev] live reload for edge dev (#1452) --- src/commands/dev/edge/mod.rs | 29 +++++++++++++++++++++++- src/commands/dev/edge/server.rs | 6 +++-- src/commands/dev/edge/setup.rs | 4 ++-- src/commands/dev/edge/watch.rs | 39 +++++++++++++++++++++++++++++++++ src/commands/dev/gcs/mod.rs | 2 -- src/commands/dev/gcs/watch.rs | 5 +---- 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/commands/dev/edge/watch.rs diff --git a/src/commands/dev/edge/mod.rs b/src/commands/dev/edge/mod.rs index 3c328a622..e96de2f90 100644 --- a/src/commands/dev/edge/mod.rs +++ b/src/commands/dev/edge/mod.rs @@ -1,8 +1,10 @@ mod server; mod setup; +mod watch; use server::serve; use setup::{upload, Session}; +use watch::watch_for_changes; use crate::commands::dev::{socket, ServerConfig}; use crate::settings::global_user::GlobalUser; @@ -10,6 +12,9 @@ use crate::settings::toml::{DeployConfig, Target}; use tokio::runtime::Runtime as TokioRuntime; +use std::sync::{Arc, Mutex}; +use std::thread; + pub fn dev( target: Target, user: GlobalUser, @@ -28,10 +33,32 @@ pub fn dev( verbose, )?; + let preview_token = Arc::new(Mutex::new(preview_token)); + + { + let preview_token = preview_token.clone(); + let session_token = session.preview_token.clone(); + + thread::spawn(move || { + watch_for_changes( + target, + &deploy_config, + &user, + Arc::clone(&preview_token), + session_token, + verbose, + ) + }); + } + let mut runtime = TokioRuntime::new()?; runtime.block_on(async { let devtools_listener = tokio::spawn(socket::listen(session.websocket_url)); - let server = tokio::spawn(serve(server_config, preview_token, session.host)); + let server = tokio::spawn(serve( + server_config, + Arc::clone(&preview_token), + session.host, + )); let res = tokio::try_join!(async { devtools_listener.await? }, async { server.await? }); match res { diff --git a/src/commands/dev/edge/server.rs b/src/commands/dev/edge/server.rs index 11a4bb4d7..1b78720fe 100644 --- a/src/commands/dev/edge/server.rs +++ b/src/commands/dev/edge/server.rs @@ -2,6 +2,8 @@ use crate::commands::dev::server_config::ServerConfig; use crate::commands::dev::utils::get_path_as_str; use crate::terminal::emoji; +use std::sync::{Arc, Mutex}; + use chrono::prelude::*; use hyper::client::{HttpConnector, ResponseFuture}; use hyper::header::{HeaderName, HeaderValue}; @@ -11,7 +13,7 @@ use hyper_tls::HttpsConnector; pub(super) async fn serve( server_config: ServerConfig, - preview_token: String, + preview_token: Arc>, host: String, ) -> Result<(), failure::Error> { // set up https client to connect to the preview service @@ -29,7 +31,7 @@ pub(super) async fn serve( async move { Ok::<_, failure::Error>(service_fn(move |req| { let client = client.to_owned(); - let preview_token = preview_token.to_owned(); + let preview_token = preview_token.lock().unwrap().to_owned(); let host = host.to_owned(); let version = req.version(); let (parts, body) = req.into_parts(); diff --git a/src/commands/dev/edge/setup.rs b/src/commands/dev/edge/setup.rs index 75533dd3c..8b274ce70 100644 --- a/src/commands/dev/edge/setup.rs +++ b/src/commands/dev/edge/setup.rs @@ -15,7 +15,7 @@ pub(super) fn upload( target: &mut Target, deploy_config: &DeployConfig, user: &GlobalUser, - preview_token: String, + session_token: String, verbose: bool, ) -> Result { let client = crate::http::legacy_auth_client(&user); @@ -45,7 +45,7 @@ pub(super) fn upload( let response = client .post(&address) - .header("cf-preview-upload-config-token", preview_token) + .header("cf-preview-upload-config-token", session_token) .multipart(script_upload_form) .send()? .error_for_status()?; diff --git a/src/commands/dev/edge/watch.rs b/src/commands/dev/edge/watch.rs new file mode 100644 index 000000000..c7b8f36ae --- /dev/null +++ b/src/commands/dev/edge/watch.rs @@ -0,0 +1,39 @@ +use std::sync::{mpsc, Arc, Mutex}; + +use crate::commands::dev::edge::setup; + +use crate::settings::global_user::GlobalUser; +use crate::settings::toml::{DeployConfig, Target}; +use crate::watch::watch_and_build; + +pub fn watch_for_changes( + target: Target, + deploy_config: &DeployConfig, + user: &GlobalUser, + preview_token: Arc>, + session_token: String, + verbose: bool, +) -> Result<(), failure::Error> { + let (sender, receiver) = mpsc::channel(); + watch_and_build(&target, Some(sender))?; + + while receiver.recv().is_ok() { + let user = user.clone(); + let target = target.clone(); + let deploy_config = deploy_config.clone(); + let session_token = session_token.clone(); + let mut target = target; + + // acquire the lock so incoming requests are halted + // until the new script is ready for them + let mut preview_token = preview_token.lock().unwrap(); + + // while holding the lock, assign a new preview id + // + // this allows the server to route subsequent requests + // to the proper script + *preview_token = setup::upload(&mut target, &deploy_config, &user, session_token, verbose)?; + } + + Ok(()) +} diff --git a/src/commands/dev/gcs/mod.rs b/src/commands/dev/gcs/mod.rs index c89fa3500..3311a7342 100644 --- a/src/commands/dev/gcs/mod.rs +++ b/src/commands/dev/gcs/mod.rs @@ -57,8 +57,6 @@ pub fn dev( thread::spawn(move || { watch_for_changes( target, - // there is no user for unauthenticated dev - None, &server_config, Arc::clone(&preview_id), &session_id, diff --git a/src/commands/dev/gcs/watch.rs b/src/commands/dev/gcs/watch.rs index c4c9b5f6b..906a46435 100644 --- a/src/commands/dev/gcs/watch.rs +++ b/src/commands/dev/gcs/watch.rs @@ -3,13 +3,11 @@ use std::sync::{mpsc, Arc, Mutex}; use crate::commands::dev::gcs::setup::get_preview_id; use crate::commands::dev::server_config::ServerConfig; -use crate::settings::global_user::GlobalUser; use crate::settings::toml::Target; use crate::watch::watch_and_build; pub fn watch_for_changes( target: Target, - user: Option, server_config: &ServerConfig, preview_id: Arc>, session_id: &str, @@ -19,7 +17,6 @@ pub fn watch_for_changes( watch_and_build(&target, Some(sender))?; while receiver.recv().is_ok() { - let user = user.clone(); let target = target.clone(); // acquire the lock so incoming requests are halted @@ -30,7 +27,7 @@ pub fn watch_for_changes( // // this allows the server to route subsequent requests // to the proper script - *preview_id = get_preview_id(target, user, server_config, session_id, verbose)?; + *preview_id = get_preview_id(target, None, server_config, session_id, verbose)?; } Ok(())