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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use crate::commands::{subdomain::Subdomain, whoami::display_account_id_maybe};
use crate::http;
use crate::settings::global_user::GlobalUser;
use crate::settings::toml::RouteConfig;
#[derive(Clone, Debug, PartialEq)]
pub struct ZonelessTarget {
pub account_id: String,
pub script_name: String,
}
impl ZonelessTarget {
pub fn build(script_name: &str, route_config: &RouteConfig) -> Result<Self, failure::Error> {
match route_config.account_id.as_ref() {
Some(account_id) if !account_id.is_empty() => Ok(Self {
script_name: script_name.to_string(),
account_id: account_id.to_string(),
}),
_ => {
display_account_id_maybe();
failure::bail!("field `account_id` is required to deploy to workers.dev")
}
}
}
pub fn deploy(&self, user: &GlobalUser) -> Result<String, failure::Error> {
log::info!("publishing to workers.dev subdomain");
log::info!("checking that subdomain is registered");
let subdomain = match Subdomain::get(&self.account_id, user)? {
Some(subdomain) => subdomain,
None => failure::bail!("Before publishing to workers.dev, you must register a subdomain. Please choose a name for your subdomain and run `wrangler subdomain <name>`.")
};
let sd_worker_addr = format!(
"https://api.cloudflare.com/client/v4/accounts/{}/workers/scripts/{}/subdomain",
self.account_id, self.script_name,
);
let client = http::legacy_auth_client(user);
log::info!("Making public on subdomain...");
let res = client
.post(&sd_worker_addr)
.header("Content-type", "application/json")
.body(build_subdomain_request())
.send()?;
if !res.status().is_success() {
failure::bail!(
"Something went wrong! Status: {}, Details {}",
res.status(),
res.text()?
)
}
let deploy_address = format!("https://{}.{}.workers.dev", self.script_name, subdomain);
Ok(deploy_address)
}
}
fn build_subdomain_request() -> String {
serde_json::json!({ "enabled": true }).to_string()
}