Skip to content

Commit

Permalink
moved rest related config struct to jormungandr-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Jul 1, 2020
1 parent bcb116a commit 4022a2e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 78 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jormungandr-lib/Cargo.toml
Expand Up @@ -23,6 +23,7 @@ thiserror = "1.0"
poldercast = "0.13.1"
hex = "0.4"
multiaddr = "0.3.1"
warp = { version = "0.2.3", features = ["tls"] }

[dev-dependencies]
rand = "0.7"
Expand Down
53 changes: 49 additions & 4 deletions jormungandr-lib/src/interfaces/config/node.rs
Expand Up @@ -2,9 +2,8 @@ use crate::{
interfaces::{Log, Mempool},
time::Duration,
};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::PathBuf;
use serde::{de::Visitor, Deserialize, Deserializer, Serialize};
use std::{fmt, net::SocketAddr, path::PathBuf, str::FromStr};
const DEFAULT_PREFERRED_VIEW_MAX: usize = 20;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -33,11 +32,57 @@ pub struct Tls {
pub struct Cors {
/// If none provided, echos request origin
#[serde(default)]
pub allowed_origins: Vec<String>,
pub allowed_origins: Vec<CorsOrigin>,
/// If none provided, CORS responses won't be cached
pub max_age_secs: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, PartialEq, Eq)]
pub struct CorsOrigin(String);

impl<'de> Deserialize<'de> for CorsOrigin {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CorsOriginVisitor;
impl<'de> Visitor<'de> for CorsOriginVisitor {
type Value = CorsOrigin;

fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "an origin in format http[s]://example.com[:3000]",)
}

fn visit_str<'a, E>(self, v: &'a str) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
use serde::de::Unexpected;

let uri = warp::http::uri::Uri::from_str(v).map_err(E::custom)?;
if let Some(s) = uri.scheme_str() {
if s != "http" && s != "https" {
return Err(E::invalid_value(Unexpected::Str(v), &self));
}
}
if let Some(p) = uri.path_and_query() {
if p.as_str() != "/" {
return Err(E::invalid_value(Unexpected::Str(v), &self));
}
}
Ok(CorsOrigin(v.trim_end_matches('/').to_owned()))
}
}
deserializer.deserialize_str(CorsOriginVisitor)
}
}

impl AsRef<str> for CorsOrigin {
fn as_ref(&self) -> &str {
&self.0
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct P2p {
/// The public address to which other peers may connect to
Expand Down
74 changes: 0 additions & 74 deletions jormungandr/src/settings/start/config.rs
Expand Up @@ -57,37 +57,6 @@ pub struct ConfigLogSettingsEntry {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ConfigLogSettings(pub Vec<ConfigLogSettingsEntry>);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Rest {
pub listen: SocketAddr,
/// Enables TLS and disables plain HTTP if provided
pub tls: Option<Tls>,
/// Enables CORS if provided
pub cors: Option<Cors>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Tls {
/// Path to server X.509 certificate chain file, must be PEM-encoded and contain at least 1 item
pub cert_file: String,
/// Path to server private key file, must be PKCS8 with single PEM-encoded, unencrypted key
pub priv_key_file: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Cors {
/// If none provided, echos request origin
#[serde(default)]
pub allowed_origins: Vec<CorsOrigin>,
/// If none provided, CORS responses won't be cached
pub max_age_secs: Option<u64>,
}
#[derive(Debug, Clone, Default, Serialize, PartialEq, Eq)]
pub struct CorsOrigin(String);

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct P2pConfig {
Expand Down Expand Up @@ -360,49 +329,6 @@ impl<'de> Deserialize<'de> for InterestLevel {
}
}

impl<'de> Deserialize<'de> for CorsOrigin {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CorsOriginVisitor;
impl<'de> Visitor<'de> for CorsOriginVisitor {
type Value = CorsOrigin;

fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "an origin in format http[s]://example.com[:3000]",)
}

fn visit_str<'a, E>(self, v: &'a str) -> std::result::Result<Self::Value, E>
where
E: serde::de::Error,
{
use serde::de::Unexpected;

let uri = warp::http::uri::Uri::from_str(v).map_err(E::custom)?;
if let Some(s) = uri.scheme_str() {
if s != "http" && s != "https" {
return Err(E::invalid_value(Unexpected::Str(v), &self));
}
}
if let Some(p) = uri.path_and_query() {
if p.as_str() != "/" {
return Err(E::invalid_value(Unexpected::Str(v), &self));
}
}
Ok(CorsOrigin(v.trim_end_matches('/').to_owned()))
}
}
deserializer.deserialize_str(CorsOriginVisitor)
}
}

impl AsRef<str> for CorsOrigin {
fn as_ref(&self) -> &str {
&self.0
}
}

mod filter_level_opt_serde {
use super::*;

Expand Down

0 comments on commit 4022a2e

Please sign in to comment.