From 672c50e5c5a7d528f764165dff7e9d992c57d9e3 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Wed, 26 Jun 2024 08:06:48 +0200 Subject: [PATCH] make listening address configurable --- Cargo.lock | 2 +- Containerfile | 4 ++-- README.md | 15 ++++++++++++--- little-hesinde/Cargo.toml | 2 +- little-hesinde/src/cli.rs | 3 +++ little-hesinde/src/config.rs | 23 ++++++++++++++++++++++- little-hesinde/src/lib.rs | 9 ++++++--- 7 files changed, 47 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5821e7b..0ce62c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -889,7 +889,7 @@ dependencies = [ [[package]] name = "little-hesinde" -version = "0.1.3" +version = "0.1.4" dependencies = [ "calibre-db", "clap", diff --git a/Containerfile b/Containerfile index a27f509..fbdb116 100644 --- a/Containerfile +++ b/Containerfile @@ -1,4 +1,4 @@ -FROM docker.io/rust:1-alpine3.19 AS builder +FROM docker.io/rust:1-alpine3.20 AS builder RUN apk --no-cache add \ musl-dev @@ -17,7 +17,7 @@ RUN cp "./target/$(arch)-unknown-linux-musl/release/little-hesinde" /app FROM scratch COPY --from=builder /app /app -CMD ["/app", "--", "/library"] +CMD ["/app", "--listen-address", "[::]:3000", "--", "/library"] VOLUME ["/library"] EXPOSE 3000 diff --git a/README.md b/README.md index 12af0f0..73b837b 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,19 @@ From there on `cargo run` and `cargo build` and so on can be used. # Configuration -The binary takes exactly one argument, the path to the calibre library folder. +``` +Usage: little-hesinde [OPTIONS] -- -The listening port is hardcoded to `3000` for now, as is the listening on all -interfaces. +Arguments: + Calibre library path + +Options: + -l, --listen-address Address to listen on [default: [::1]:3000] + -h, --help Print help + -V, --version Print version +``` + +Example: `little-hesinde -l [::]4000 -- ~/Documents/library/` # Usage diff --git a/little-hesinde/Cargo.toml b/little-hesinde/Cargo.toml index ce5b5b5..b5f280f 100644 --- a/little-hesinde/Cargo.toml +++ b/little-hesinde/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "little-hesinde" -version = "0.1.3" +version = "0.1.4" edition = "2021" license = { workspace = true } authors = { workspace = true } diff --git a/little-hesinde/src/cli.rs b/little-hesinde/src/cli.rs index 6f1c404..8002b29 100644 --- a/little-hesinde/src/cli.rs +++ b/little-hesinde/src/cli.rs @@ -6,6 +6,9 @@ use clap::Parser; #[derive(Parser)] #[command(version, about, long_about = None)] pub struct Cli { + /// Address to listen on + #[arg(short, long, default_value = "[::1]:3000")] + pub listen_address: String, /// Calibre library path #[arg(last = true)] pub library_path: String, diff --git a/little-hesinde/src/config.rs b/little-hesinde/src/config.rs index 9d0b97e..546d833 100644 --- a/little-hesinde/src/config.rs +++ b/little-hesinde/src/config.rs @@ -1,6 +1,10 @@ //! Configuration data. -use std::path::{Path, PathBuf}; +use std::{ + net::SocketAddr, + net::ToSocketAddrs, + path::{Path, PathBuf}, +}; use thiserror::Error; @@ -15,14 +19,20 @@ pub enum ConfigError { /// Calibre database does not exist. #[error("no metadata.db in {0}")] MetadataNotFound(String), + /// Error converting a string to a listening address. + #[error("failed to convert into listening address")] + ListeningAddressError(String), } /// Application configuration. +#[derive(Debug, Clone)] pub struct Config { /// Calibre library folder. pub library_path: PathBuf, /// Calibre metadata file path. pub metadata_path: PathBuf, + /// Address to listen on. + pub listen_address: SocketAddr, } impl Config { @@ -48,10 +58,21 @@ impl Config { .to_string(); return Err(ConfigError::MetadataNotFound(metadata_path)); } + let listen_address = args + .listen_address + .to_socket_addrs() + .map_err(|e| { + ConfigError::ListeningAddressError(format!("{}: {e:?}", args.listen_address)) + })? + .next() + .ok_or(ConfigError::ListeningAddressError( + args.listen_address.clone(), + ))?; Ok(Self { library_path, metadata_path, + listen_address, }) } } diff --git a/little-hesinde/src/lib.rs b/little-hesinde/src/lib.rs index 86af4f6..855d0ce 100644 --- a/little-hesinde/src/lib.rs +++ b/little-hesinde/src/lib.rs @@ -71,7 +71,7 @@ pub mod opds { pub mod templates; pub const APP_NAME: &str = "little-hesinde"; -pub const VERSION: &str = "0.1.3"; +pub const VERSION: &str = "0.1.4"; /// Internal marker data in lieu of a proper `Accept` header. #[derive(Debug, Clone, Copy)] @@ -90,7 +90,10 @@ pub struct Files; /// Main entry point to run the ebook server with a calibre library specified in `config`. pub async fn run(config: Config) -> Result<(), std::io::Error> { let calibre = Calibre::load(&config.metadata_path).expect("failed to load calibre database"); - let app_state = Arc::new(AppState { calibre, config }); + let app_state = Arc::new(AppState { + calibre, + config: config.clone(), + }); let html_routes = Route::new() .at("/", get(handlers::recent::handler)) @@ -130,7 +133,7 @@ pub async fn run(config: Config) -> Result<(), std::io::Error> { .data(app_state) .with(Tracing); - let server = Server::new(TcpListener::bind("[::]:3000")) + let server = Server::new(TcpListener::bind(config.listen_address)) .name("cops-web") .run(app);