From 603c2fbe48508f5c691af9326e54e73b469bbd50 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Thu, 9 May 2024 08:58:45 +0200 Subject: [PATCH] make it a library crate --- rusty-library/src/handlers/error.rs | 4 +- rusty-library/src/lib.rs | 70 +++++++++++++++++++++++++++++ rusty-library/src/main.rs | 70 +---------------------------- rusty-library/src/opds/feed.rs | 2 +- 4 files changed, 75 insertions(+), 71 deletions(-) create mode 100644 rusty-library/src/lib.rs diff --git a/rusty-library/src/handlers/error.rs b/rusty-library/src/handlers/error.rs index bd98a4f..d3f89f1 100644 --- a/rusty-library/src/handlers/error.rs +++ b/rusty-library/src/handlers/error.rs @@ -18,7 +18,7 @@ pub enum HandlerError { impl ResponseError for HandlerError { fn status(&self) -> StatusCode { match &self { - HandlerError::OpdsError(e) => StatusCode::INTERNAL_SERVER_ERROR, + HandlerError::OpdsError(_) => StatusCode::INTERNAL_SERVER_ERROR, HandlerError::DataError(e) => match e { DataStoreError::NoResults(_) => StatusCode::NOT_FOUND, _ => StatusCode::INTERNAL_SERVER_ERROR, @@ -30,7 +30,7 @@ impl ResponseError for HandlerError { let id = Uuid::new_v4(); let internal_msg = format!("{:?}", self); let external_msg = match &self { - HandlerError::OpdsError(e) => "internal server error", + HandlerError::OpdsError(_) => "internal server error", HandlerError::DataError(e) => match e { DataStoreError::NoResults(_) => "item not found", _ => "internal server error", diff --git a/rusty-library/src/lib.rs b/rusty-library/src/lib.rs new file mode 100644 index 0000000..3a04e3d --- /dev/null +++ b/rusty-library/src/lib.rs @@ -0,0 +1,70 @@ +use std::sync::Arc; + +use app_state::AppState; +use calibre_db::calibre::Calibre; +use config::Config; +use poem::{ + endpoint::EmbeddedFilesEndpoint, get, listener::TcpListener, middleware::Tracing, EndpointExt, + Route, Server, +}; +use rust_embed::RustEmbed; + +pub mod app_state; +pub mod basic_auth; +pub mod cli; +pub mod config; +pub mod data { + pub mod book; +} +pub mod handlers { + pub mod author; + pub mod authors; + pub mod books; + pub mod cover; + pub mod download; + pub mod error; + pub mod opds; + pub mod paginated; + pub mod recents; + pub mod series; + pub mod series_single; +} +pub mod opds; +pub mod templates; + +#[derive(RustEmbed)] +#[folder = "static"] +pub struct Files; + +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 = Route::new() + .at("/", get(handlers::recents::handler)) + .at("/opds", get(handlers::opds::handler)) + .at("/books", get(handlers::books::handler_init)) + .at("/books/:cursor/:sort_order", get(handlers::books::handler)) + .at("/series", get(handlers::series::handler_init)) + .at( + "/series/:cursor/:sort_order", + get(handlers::series::handler), + ) + .at("/series/:id", get(handlers::series_single::handler)) + .at("/authors", get(handlers::authors::handler_init)) + .at("/authors/:id", get(handlers::author::handler)) + .at( + "/authors/:cursor/:sort_order", + get(handlers::authors::handler), + ) + .at("/cover/:id", get(handlers::cover::handler)) + .at("/book/:id/:format", get(handlers::download::handler)) + .nest("/static", EmbeddedFilesEndpoint::::new()) + .data(app_state) + .with(Tracing); + + Server::new(TcpListener::bind("[::]:3000")) + .name("cops-web") + .run(app) + .await +} diff --git a/rusty-library/src/main.rs b/rusty-library/src/main.rs index 97021c1..0001fb5 100644 --- a/rusty-library/src/main.rs +++ b/rusty-library/src/main.rs @@ -1,42 +1,5 @@ -use std::sync::Arc; - -use app_state::AppState; -use calibre_db::calibre::Calibre; use clap::Parser; -use cli::Cli; -use config::Config; -use poem::{ - endpoint::EmbeddedFilesEndpoint, get, listener::TcpListener, middleware::Tracing, EndpointExt, - Route, Server, -}; -use rust_embed::RustEmbed; - -mod app_state; -mod basic_auth; -mod cli; -mod config; -mod data { - pub mod book; -} -mod handlers { - pub mod author; - pub mod authors; - pub mod books; - pub mod cover; - pub mod download; - pub mod error; - pub mod opds; - pub mod paginated; - pub mod recents; - pub mod series; - pub mod series_single; -} -mod opds; -mod templates; - -#[derive(RustEmbed)] -#[folder = "static"] -pub struct Files; +use rusty_library::{cli::Cli, config::Config}; #[tokio::main] async fn main() -> Result<(), std::io::Error> { @@ -48,34 +11,5 @@ async fn main() -> Result<(), std::io::Error> { let args = Cli::parse(); let config = Config::load(&args).expect("failed to load configuration"); - let calibre = Calibre::load(&config.metadata_path).expect("failed to load calibre database"); - let app_state = Arc::new(AppState { calibre, config }); - - let app = Route::new() - .at("/", get(handlers::recents::handler)) - .at("/opds", get(handlers::opds::handler)) - .at("/books", get(handlers::books::handler_init)) - .at("/books/:cursor/:sort_order", get(handlers::books::handler)) - .at("/series", get(handlers::series::handler_init)) - .at( - "/series/:cursor/:sort_order", - get(handlers::series::handler), - ) - .at("/series/:id", get(handlers::series_single::handler)) - .at("/authors", get(handlers::authors::handler_init)) - .at("/authors/:id", get(handlers::author::handler)) - .at( - "/authors/:cursor/:sort_order", - get(handlers::authors::handler), - ) - .at("/cover/:id", get(handlers::cover::handler)) - .at("/book/:id/:format", get(handlers::download::handler)) - .nest("/static", EmbeddedFilesEndpoint::::new()) - .data(app_state) - .with(Tracing); - - Server::new(TcpListener::bind("[::]:3000")) - .name("cops-web") - .run(app) - .await + rusty_library::run(config).await } diff --git a/rusty-library/src/opds/feed.rs b/rusty-library/src/opds/feed.rs index 70dd68d..518d09b 100644 --- a/rusty-library/src/opds/feed.rs +++ b/rusty-library/src/opds/feed.rs @@ -50,7 +50,7 @@ impl Feed { } Ok(Event::Eof) => break, Ok(e) => writer.write_event(e)?, - Err(e) => (), + Err(e) => return Err(e)?, } } let result = writer.into_inner().into_inner();