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_api::api_server;
use garage_web::web_server;
use garage_model::garage::Garage;
use garage_rpc::rpc_server::RpcServer;
use garage_web::web_server;
use crate::admin_rpc::*;

View file

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

View file

@ -1,15 +1,15 @@
use std::sync::Arc;
use std::net::SocketAddr;
use std::sync::Arc;
use futures::future::Future;
use hyper::server::conn::AddrStream;
use hyper::{Body,Request,Response,Server,Uri};
use hyper::header::HOST;
use hyper::server::conn::AddrStream;
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_util::error::Error;
pub async fn run_web_server(
garage: Arc<Garage>,
@ -21,7 +21,7 @@ pub async fn run_web_server(
let garage = garage.clone();
let client_addr = conn.remote_addr();
info!("{:?}", client_addr);
async move {
async move {
Ok::<_, Error>(service_fn(move |req: Request<Body>| {
let garage = garage.clone();
handler(garage, req, client_addr)
@ -42,7 +42,6 @@ async fn handler(
req: Request<Body>,
addr: SocketAddr,
) -> Result<Response<Body>, Error> {
// Get http authority string (eg. [::1]:3902 or garage.tld:80)
let authority = req
.headers()
@ -52,8 +51,8 @@ async fn handler(
// Get bucket
let host = authority_to_host(authority)?;
let root = &garage.config.s3_web.root_domain;
let bucket = host_to_bucket(&host, root);
let root = &garage.config.s3_web.root_domain;
let bucket = host_to_bucket(&host, root);
// Get path
let path = req.uri().path().to_string();
@ -78,12 +77,14 @@ fn authority_to_host(authority: &str) -> Result<String, Error> {
match uri_str.parse::<Uri>() {
Ok(uri) => {
let host = uri
.host()
.ok_or(Error::BadRequest(format!("Unable to extract host from authority")))?;
let host = uri.host().ok_or(Error::BadRequest(format!(
"Unable to extract host from authority"
)))?;
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 missing_starting_dot = root.chars().next() != Some('.');
let cursor = if missing_starting_dot { len_diff - 1 } else { len_diff };
&host[..cursor]
let cursor = if missing_starting_dot {
len_diff - 1
} else {
len_diff
};
&host[..cursor]
}
#[cfg(test)]
mod tests {
use super::*;
@ -128,28 +131,25 @@ mod tests {
#[test]
fn host_to_bucket_test() {
assert_eq!(
assert_eq!(
host_to_bucket("john.doe.garage.tld", ".garage.tld"),
"john.doe");
assert_eq!(
host_to_bucket("john.doe.garage.tld", "garage.tld"),
"john.doe");
"john.doe"
);
assert_eq!(
host_to_bucket("john.doe.com", "garage.tld"),
"john.doe.com");
host_to_bucket("john.doe.garage.tld", "garage.tld"),
"john.doe"
);
assert_eq!(host_to_bucket("john.doe.com", "garage.tld"), "john.doe.com");
assert_eq!(
host_to_bucket("john.doe.com", ".garage.tld"),
"john.doe.com");
"john.doe.com"
);
assert_eq!(
host_to_bucket("garage.tld", "garage.tld"),
"garage.tld");
assert_eq!(host_to_bucket("garage.tld", "garage.tld"), "garage.tld");
assert_eq!(
host_to_bucket("garage.tld", ".garage.tld"),
"garage.tld");
assert_eq!(host_to_bucket("garage.tld", ".garage.tld"), "garage.tld");
}
}