Support website publishing #7

Merged
lx merged 61 commits from feature/website into master 2021-01-15 16:49:51 +00:00
3 changed files with 38 additions and 39 deletions
Showing only changes of commit 27795a390c - Show all commits

View file

@ -9,9 +9,9 @@ use garage_util::config::*;
use garage_util::error::Error; use garage_util::error::Error;
use garage_api::api_server; use garage_api::api_server;
use garage_web::web_server;
use garage_model::garage::Garage; use garage_model::garage::Garage;
use garage_rpc::rpc_server::RpcServer; use garage_rpc::rpc_server::RpcServer;
use garage_web::web_server;
use crate::admin_rpc::*; use crate::admin_rpc::*;

View file

@ -2,4 +2,3 @@
extern crate log; extern crate log;
pub mod web_server; pub mod web_server;

View file

@ -1,15 +1,15 @@
use std::sync::Arc;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc;
use futures::future::Future; use futures::future::Future;
use hyper::server::conn::AddrStream;
use hyper::{Body,Request,Response,Server,Uri};
use hyper::header::HOST; use hyper::header::HOST;
use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server, Uri};
use garage_util::error::Error;
use garage_model::garage::Garage; use garage_model::garage::Garage;
use garage_util::error::Error;
pub async fn run_web_server( pub async fn run_web_server(
garage: Arc<Garage>, garage: Arc<Garage>,
@ -42,7 +42,6 @@ async fn handler(
req: Request<Body>, req: Request<Body>,
addr: SocketAddr, addr: SocketAddr,
) -> Result<Response<Body>, Error> { ) -> Result<Response<Body>, Error> {
// Get http authority string (eg. [::1]:3902 or garage.tld:80) // Get http authority string (eg. [::1]:3902 or garage.tld:80)
let authority = req let authority = req
.headers() .headers()
@ -78,12 +77,14 @@ fn authority_to_host(authority: &str) -> Result<String, Error> {
match uri_str.parse::<Uri>() { match uri_str.parse::<Uri>() {
Ok(uri) => { Ok(uri) => {
let host = uri let host = uri.host().ok_or(Error::BadRequest(format!(
.host() "Unable to extract host from authority"
.ok_or(Error::BadRequest(format!("Unable to extract host from authority")))?; )))?;
Ok(String::from(host)) Ok(String::from(host))
} }
_ => Err(Error::BadRequest(format!("Unable to parse authority (host HTTP header)"))), _ => Err(Error::BadRequest(format!(
"Unable to parse authority (host HTTP header)"
))),
} }
} }
@ -94,12 +95,14 @@ fn host_to_bucket<'a>(host: &'a str, root: &str) -> &'a str {
let len_diff = host.len() - root.len(); let len_diff = host.len() - root.len();
let missing_starting_dot = root.chars().next() != Some('.'); let missing_starting_dot = root.chars().next() != Some('.');
let cursor = if missing_starting_dot { len_diff - 1 } else { len_diff }; let cursor = if missing_starting_dot {
len_diff - 1
} else {
len_diff
};
&host[..cursor] &host[..cursor]
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -130,26 +133,23 @@ mod tests {
fn host_to_bucket_test() { fn host_to_bucket_test() {
assert_eq!( assert_eq!(
host_to_bucket("john.doe.garage.tld", ".garage.tld"), host_to_bucket("john.doe.garage.tld", ".garage.tld"),
"john.doe"); "john.doe"
);
assert_eq!( assert_eq!(
host_to_bucket("john.doe.garage.tld", "garage.tld"), host_to_bucket("john.doe.garage.tld", "garage.tld"),
"john.doe"); "john.doe"
);
assert_eq!( assert_eq!(host_to_bucket("john.doe.com", "garage.tld"), "john.doe.com");
host_to_bucket("john.doe.com", "garage.tld"),
"john.doe.com");
assert_eq!( assert_eq!(
host_to_bucket("john.doe.com", ".garage.tld"), host_to_bucket("john.doe.com", ".garage.tld"),
"john.doe.com"); "john.doe.com"
);
assert_eq!( assert_eq!(host_to_bucket("garage.tld", "garage.tld"), "garage.tld");
host_to_bucket("garage.tld", "garage.tld"),
"garage.tld");
assert_eq!( assert_eq!(host_to_bucket("garage.tld", ".garage.tld"), "garage.tld");
host_to_bucket("garage.tld", ".garage.tld"),
"garage.tld");
} }
} }