Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following configuration options are available.
| debug | false | _unused_ |
| port | 8000 | connection port |
| host | 127.0.0.1 | host to listen for connections |
| public_url | _None_ | public url for performing request validation |
| database_url | mysql://root@127.0.0.1/syncstorage | database DSN |
| database_pool_max_size | _None_ | Max pool of database connections |
| master_secret| _None_ | Sync master encryption secret |
Expand Down
3 changes: 3 additions & 0 deletions syncserver-settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ static PREFIX: &str = "sync";
pub struct Settings {
pub port: u16,
pub host: String,
/// public facing URL of the server
pub public_url: Option<String>,
/// Keep-alive header value (seconds)
pub actix_keep_alive: Option<u32>,
/// The master secret, from which are derived
Expand Down Expand Up @@ -185,6 +187,7 @@ impl Default for Settings {
Settings {
port: 8000,
host: "127.0.0.1".to_string(),
public_url: None,
actix_keep_alive: None,
master_secret: Secrets::default(),
statsd_host: Some("localhost".to_owned()),
Expand Down
34 changes: 32 additions & 2 deletions syncserver/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use actix_web::{
};
use cadence::{Gauged, StatsdClient};
use futures::future::{self, Ready};
use http::Uri;
use syncserver_common::{
middleware::sentry::SentryWrapper, BlockingThreadpool, BlockingThreadpoolMetrics, Metrics,
Taggable,
Expand Down Expand Up @@ -68,6 +69,31 @@ pub struct ServerState {
pub glean_enabled: bool,
}

/// This is a state holding data about the reverse proxy configuration.
/// This state object will be made available to all HTTP API calls.
pub struct ReverseProxyState {
/// public facing URL of the server
pub public_url: Option<String>,
}

impl ReverseProxyState {
pub fn from_settings(settings: &Settings) -> ReverseProxyState {
ReverseProxyState {
public_url: settings.public_url.clone(),
}
}

pub fn get_webroot(&self) -> String {
match &self.public_url {
None => "".to_owned(),
Some(url) => match url.parse::<Uri>() {
Err(_) => "".to_owned(),
Ok(uri) => uri.path().to_owned(),
},
}
}
}

pub fn cfg_path(path: &str) -> String {
let path = path
.replace(
Expand All @@ -82,7 +108,7 @@ pub struct Server;

#[macro_export]
macro_rules! build_app {
($syncstorage_state: expr, $tokenserver_state: expr, $secrets: expr, $limits: expr, $cors: expr, $metrics: expr) => {
($reverse_proxy_state: expr, $syncstorage_state: expr, $tokenserver_state: expr, $secrets: expr, $limits: expr, $cors: expr, $metrics: expr) => {
App::new()
.configure(|cfg| {
cfg.app_data(Data::new($syncstorage_state));
Expand All @@ -93,6 +119,7 @@ macro_rules! build_app {
}
})
.app_data(Data::new($secrets))
.app_data(Data::new($reverse_proxy_state))
// Middleware is applied LIFO
// These will wrap all outbound responses with matching status codes.
.wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, ApiError::render_404))
Expand Down Expand Up @@ -196,10 +223,11 @@ macro_rules! build_app {

#[macro_export]
macro_rules! build_app_without_syncstorage {
($state: expr, $secrets: expr, $cors: expr, $metrics: expr) => {
($reverse_proxy_state: expr, $state: expr, $secrets: expr, $cors: expr, $metrics: expr) => {
App::new()
.app_data(Data::new($state))
.app_data(Data::new($secrets))
.app_data(Data::new($reverse_proxy_state))
// Middleware is applied LIFO
// These will wrap all outbound responses with matching status codes.
.wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, ApiError::render_404))
Expand Down Expand Up @@ -341,6 +369,7 @@ impl Server {
};

build_app!(
ReverseProxyState::from_settings(&settings_copy),
syncstorage_state,
tokenserver_state.clone(),
Arc::clone(&secrets),
Expand Down Expand Up @@ -399,6 +428,7 @@ impl Server {

let server = HttpServer::new(move || {
build_app_without_syncstorage!(
ReverseProxyState::from_settings(&settings_copy),
tokenserver_state.clone(),
Arc::clone(&secrets),
build_cors(&settings_copy),
Expand Down
Loading