diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-07-19 11:46:25 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-07-19 13:29:39 +0300 |
commit | 62b3cd170e603367629f7b1dc2abd02a9524277a (patch) | |
tree | 09bd5c29fc486e990773dfc29f79ac261ce7807c | |
parent | ee3c218fea70d02d0bc0e1e4baae8cb8c988074c (diff) | |
download | ewww-62b3cd170e603367629f7b1dc2abd02a9524277a.tar.gz |
feat: serve files from configured webroot
-rw-r--r-- | ewww.md | 15 | ||||
-rw-r--r-- | src/main.rs | 14 |
2 files changed, 20 insertions, 9 deletions
@@ -88,13 +88,15 @@ for static content only. Every other method returns an error. ## Minimal smoke test ~~~scenario +when I create webroot/foo.html with "this is your web page" given a self-signed certificate as snakeoil.pem, using key snakeoil.key and a running server using config file minimal.yaml -when I request GET https://example.com/ +when I request GET https://example.com/foo.html then I get status code 200 ~~~ ~~~{#minimal.yaml .file .yaml} +webroot: webroot tls_key: snakeoil.key tls_cert: snakeoil.pem ~~~ @@ -104,12 +106,12 @@ tls_cert: snakeoil.pem ~~~scenario given a self-signed certificate as snakeoil.pem, using key snakeoil.key -and a running server using config file smoke.yaml -when I create webroot/foo with "hello, world" -and I request GET https://example.com/foo +when I create webroot/foo.html with "this is your web page" +given a running server using config file smoke.yaml +when I request GET https://example.com/foo.html then I get status code 200 -and header content-type is "text/plain" -and body is "hello, world\n" +and header content-type is "text/html" +and body is "this is your web page" ~~~ ~~~scenario-disabled @@ -145,6 +147,7 @@ scaffolding adds randomly chosen port numbers so that the test can run without being root. ~~~{#smoke.yaml .file .yaml .numberLines} +webroot: webroot tls_cert: snakeoil.pem tls_key: snakeoil.key ~~~ diff --git a/src/main.rs b/src/main.rs index d74a148..37e60d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use warp::Filter; #[derive(Debug, Deserialize)] struct Config { + webroot: PathBuf, port: u16, tls_key: PathBuf, tls_cert: PathBuf, @@ -19,6 +20,9 @@ struct Opt { #[derive(Debug, thiserror::Error)] enum EwwwError { + #[error("Web root {0} does not exist")] + WebrootNotFound(PathBuf), + #[error("TLS certificate {0} does not exist")] TlsCertNotFound(PathBuf), @@ -33,11 +37,12 @@ async fn main() { let opt = Opt::from_args(); let config = read_config(&opt.config).unwrap(); - let hello = warp::any() - .map(|| "hello, world\n".to_string()); + let webroot = config.webroot.canonicalize().unwrap(); + eprintln!("webroot: {:?}", webroot); + let webroot = warp::any().and(warp::fs::dir(webroot)); eprintln!("starting server: {:?}", config); - warp::serve(hello) + warp::serve(webroot) .tls() .key_path(config.tls_key) .cert_path(config.tls_cert) @@ -53,6 +58,9 @@ fn read_config(filename: &Path) -> anyhow::Result<Config> { } fn check_config(config: &Config) -> anyhow::Result<()> { + if !config.webroot.exists() { + return Err(EwwwError::WebrootNotFound(config.webroot.clone()).into()); + } if !config.tls_cert.exists() { return Err(EwwwError::TlsCertNotFound(config.tls_cert.clone()).into()); } |