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

Wrangler dev https server #1485

Merged
merged 9 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 17 additions & 7 deletions src/commands/dev/edge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod server;
mod setup;
mod watch;

use server::serve;
use setup::{upload, Session};
use watch::watch_for_changes;

Expand All @@ -20,6 +19,8 @@ pub fn dev(
user: GlobalUser,
server_config: ServerConfig,
deploy_config: DeployConfig,
local_https: bool,
upstream_http: bool,
jspspike marked this conversation as resolved.
Show resolved Hide resolved
verbose: bool,
) -> Result<(), failure::Error> {
let session = Session::new(&target, &user, &deploy_config)?;
Expand Down Expand Up @@ -54,13 +55,22 @@ 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 = if local_https {
tokio::spawn(server::https(
server_config.clone(),
Arc::clone(&preview_token),
session.host.clone(),
))
} else {
tokio::spawn(server::http(
server_config,
Arc::clone(&preview_token),
session.host,
upstream_http,
))
};

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 super::preview_request;
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};
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_http: bool,
) -> 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_http,
)
.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