1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mod host;
mod protocol;
pub use protocol::Protocol;
use host::Host;
use anyhow::Result;
use std::net::{IpAddr, SocketAddr, TcpListener};
#[derive(Debug, Clone)]
pub struct ServerConfig {
pub host: Host,
pub listening_address: SocketAddr,
}
impl ServerConfig {
pub fn new(
host: Option<String>,
ip: IpAddr,
port: u16,
upstream_protocol: Protocol,
) -> Result<Self> {
let addr = SocketAddr::new(ip, port);
let listening_address = match TcpListener::bind(&addr) {
Ok(socket) => socket.local_addr(),
Err(_) => anyhow::bail!("{} is unavailable, try binding to another address with the --port and --ip flags, or stop other `wrangler dev` processes.", &addr)
}?;
let host = if let Some(host) = host {
Host::new(&host, false)?
} else {
Host::new(
match upstream_protocol {
Protocol::Http => "http://tutorial.cloudflareworkers.com",
Protocol::Https => "https://tutorial.cloudflareworkers.com",
},
true,
)?
};
Ok(ServerConfig {
host,
listening_address,
})
}
}