| 44 | pub const SWAGGER_FALLBACK: &str = "/swagger-ui"; |
| 45 | |
| 46 | pub fn parse_options( |
| 47 | plugin: &ConfiguredPlugin<PluginState, tokio::io::Stdin, tokio::io::Stdout>, |
| 48 | ) -> Result<ClnrestOptions, anyhow::Error> { |
| 49 | let port = if let Some(p) = plugin.option(&OPT_CLNREST_PORT)? { |
| 50 | if !(1024..=65535).contains(&p) { |
| 51 | return Err(anyhow!( |
| 52 | "`clnrest-port` {}, should be a valid available port between 1024 and 65535.", |
| 53 | p |
| 54 | )); |
| 55 | } |
| 56 | p as u16 |
| 57 | } else { |
| 58 | log::info!("`clnrest-port` option is not configured"); |
| 59 | return Err(anyhow!("`clnrest-port` option is not configured")); |
| 60 | }; |
| 61 | |
| 62 | let protocol = match plugin.option(&OPT_CLNREST_PROTOCOL)? { |
| 63 | p if p.eq_ignore_ascii_case("https") => ClnrestProtocol::Https, |
| 64 | p if p.eq_ignore_ascii_case("http") => ClnrestProtocol::Http, |
| 65 | _ => { |
| 66 | return Err(anyhow!("`clnrest-protocol` can either be http or https.")); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | let address_str = format!("{}:{}", plugin.option(&OPT_CLNREST_HOST)?, port); |
| 71 | let address: SocketAddr = match plugin.option(&OPT_CLNREST_HOST)? { |
| 72 | i if i.eq("localhost") => address_str |
| 73 | .to_socket_addrs()? |
| 74 | .next() |
| 75 | .ok_or(anyhow!("No address found for localhost"))?, |
| 76 | _ => { |
| 77 | if let Ok(addr) = address_str.parse() { |
| 78 | addr |
| 79 | } else { |
| 80 | return Err(anyhow!("`clnrest-host` should be a valid IP.")); |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | let cors = create_cors_layer(&plugin.option(&OPT_CLNREST_CORS)?)?; |
| 86 | |
| 87 | let certs = if let Some(cert_opt) = plugin.option(&OPT_CLNREST_CERTS)? { |
| 88 | PathBuf::from(cert_opt) |
| 89 | } else { |
| 90 | env::current_dir()? |
| 91 | }; |
| 92 | |
| 93 | let csp = plugin.option(&OPT_CLNREST_CSP)?; |
| 94 | |
| 95 | let swagger = match plugin.option(&OPT_CLNREST_SWAGGER)? { |
| 96 | swag if !swag.starts_with('/') => { |
| 97 | return Err(anyhow!("`clnrest-swagger-root` must start with `/`")); |
| 98 | } |
| 99 | swag => swag, |
| 100 | }; |
| 101 | |
| 102 | Ok(ClnrestOptions { |
| 103 | certs, |