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
48
mod host;
mod protocol;

pub use protocol::Protocol;

use host::Host;

use std::net::{SocketAddr, TcpListener};

#[derive(Debug, Clone)]
pub struct ServerConfig {
    pub host: Host,
    pub listening_address: SocketAddr,
}

impl ServerConfig {
    pub fn new(
        host: Option<&str>,
        ip: Option<&str>,
        port: Option<u16>,
        upstream_protocol: Protocol,
    ) -> Result<Self, failure::Error> {
        let ip = ip.unwrap_or("127.0.0.1");
        let port = port.unwrap_or(8787);
        let addr = format!("{}:{}", ip, port);
        let listening_address = match TcpListener::bind(&addr) {
            Ok(socket) => socket.local_addr(),
            Err(_) => failure::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,
        })
    }
}