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

Commit

Permalink
Merge pull request #1485 from cloudflare/josh/dev-https
Browse files Browse the repository at this point in the history
Wrangler dev https server
  • Loading branch information
EverlastingBugstopper authored Aug 12, 2020
2 parents ad32c7b + 7100c06 commit 2510a62
Show file tree
Hide file tree
Showing 17 changed files with 895 additions and 108 deletions.
144 changes: 140 additions & 4 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fs2 = "0.4.3"
futures-util = "0.3"
http = "0.2.1"
hyper = "0.13.7"
hyper-tls = "0.4.3"
hyper-rustls = "0.21.0"
ignore = "0.4.16"
indicatif = "0.15.0"
lazy_static = "1.4.0"
Expand All @@ -53,6 +53,7 @@ tempfile = "3.1.0"
term_size = "0.3"
text_io = "0.1.8"
tokio = { version = "0.2", default-features = false, features = ["io-std", "time", "macros", "process", "signal", "sync"] }
tokio-rustls = "0.14.0"
tokio-tls = "0.3.1"
tokio-tungstenite = { version = "0.10.1", features = ["tls"] }
toml = "0.5.6"
Expand All @@ -61,6 +62,7 @@ url = "2.1.1"
uuid = { version = "0.8", features = ["v4"] }
which = "4.0.2"
ws = "0.9.1"
rustls = "0.18.0"

[dev-dependencies]
assert_cmd = "1.0.1"
Expand Down
25 changes: 17 additions & 8 deletions src/commands/dev/edge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,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::commands::dev::{socket, Protocol, ServerConfig};
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::{DeployConfig, Target};

Expand All @@ -20,6 +19,8 @@ pub fn dev(
user: GlobalUser,
server_config: ServerConfig,
deploy_config: DeployConfig,
local_protocol: Protocol,
upstream_protocol: Protocol,
verbose: bool,
) -> Result<(), failure::Error> {
let session = Session::new(&target, &user, &deploy_config)?;
Expand Down Expand Up @@ -54,13 +55,21 @@ pub fn dev(
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,
Arc::clone(&preview_token),
session.host,
));
let res = tokio::try_join!(async { devtools_listener.await? }, async { server.await? });
let server = match local_protocol {
Protocol::Https => tokio::spawn(server::https(
server_config.clone(),
Arc::clone(&preview_token),
session.host.clone(),
)),
Protocol::Http => tokio::spawn(server::http(
server_config,
Arc::clone(&preview_token),
session.host,
upstream_protocol,
)),
};

let res = tokio::try_join!(async { devtools_listener.await? }, async { server.await? });
match res {
Ok(_) => Ok(()),
Err(e) => Err(e),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::commands::dev::server_config::ServerConfig;
use super::preview_request;
use crate::commands::dev::utils::get_path_as_str;
use crate::commands::dev::{Protocol, 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::service::{make_service_fn, service_fn};
use hyper::{Body, Client as HyperClient, Request, Server};
use hyper_tls::HttpsConnector;
use hyper_rustls::HttpsConnector;

pub(super) async fn serve(
pub async fn http(
server_config: ServerConfig,
preview_token: Arc<Mutex<String>>,
host: String,
upstream_protocol: Protocol,
) -> Result<(), failure::Error> {
// set up https client to connect to the preview service
let https = HttpsConnector::new();
Expand Down Expand Up @@ -44,6 +44,7 @@ pub(super) async fn serve(
client,
preview_token.to_owned(),
host.clone(),
upstream_protocol,
)
.await?;

Expand All @@ -64,38 +65,10 @@ pub(super) async fn serve(

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)
eprintln!("{}", e);
}
Ok(())
}

fn preview_request(
req: Request<Body>,
client: HyperClient<HttpsConnector<HttpConnector>>,
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)
Ok(())
}
Loading

0 comments on commit 2510a62

Please sign in to comment.