make it a library crate

This commit is contained in:
Sebastian Hugentobler 2024-05-09 08:58:45 +02:00
parent 93aeb80c56
commit 603c2fbe48
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0
4 changed files with 75 additions and 71 deletions

View File

@ -18,7 +18,7 @@ pub enum HandlerError {
impl ResponseError for HandlerError { impl ResponseError for HandlerError {
fn status(&self) -> StatusCode { fn status(&self) -> StatusCode {
match &self { match &self {
HandlerError::OpdsError(e) => StatusCode::INTERNAL_SERVER_ERROR, HandlerError::OpdsError(_) => StatusCode::INTERNAL_SERVER_ERROR,
HandlerError::DataError(e) => match e { HandlerError::DataError(e) => match e {
DataStoreError::NoResults(_) => StatusCode::NOT_FOUND, DataStoreError::NoResults(_) => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR, _ => StatusCode::INTERNAL_SERVER_ERROR,
@ -30,7 +30,7 @@ impl ResponseError for HandlerError {
let id = Uuid::new_v4(); let id = Uuid::new_v4();
let internal_msg = format!("{:?}", self); let internal_msg = format!("{:?}", self);
let external_msg = match &self { let external_msg = match &self {
HandlerError::OpdsError(e) => "internal server error", HandlerError::OpdsError(_) => "internal server error",
HandlerError::DataError(e) => match e { HandlerError::DataError(e) => match e {
DataStoreError::NoResults(_) => "item not found", DataStoreError::NoResults(_) => "item not found",
_ => "internal server error", _ => "internal server error",

70
rusty-library/src/lib.rs Normal file
View File

@ -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::<Files>::new())
.data(app_state)
.with(Tracing);
Server::new(TcpListener::bind("[::]:3000"))
.name("cops-web")
.run(app)
.await
}

View File

@ -1,42 +1,5 @@
use std::sync::Arc;
use app_state::AppState;
use calibre_db::calibre::Calibre;
use clap::Parser; use clap::Parser;
use cli::Cli; use rusty_library::{cli::Cli, config::Config};
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;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
@ -48,34 +11,5 @@ async fn main() -> Result<(), std::io::Error> {
let args = Cli::parse(); let args = Cli::parse();
let config = Config::load(&args).expect("failed to load configuration"); let config = Config::load(&args).expect("failed to load configuration");
let calibre = Calibre::load(&config.metadata_path).expect("failed to load calibre database"); rusty_library::run(config).await
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::<Files>::new())
.data(app_state)
.with(Tracing);
Server::new(TcpListener::bind("[::]:3000"))
.name("cops-web")
.run(app)
.await
} }

View File

@ -50,7 +50,7 @@ impl Feed {
} }
Ok(Event::Eof) => break, Ok(Event::Eof) => break,
Ok(e) => writer.write_event(e)?, Ok(e) => writer.write_event(e)?,
Err(e) => (), Err(e) => return Err(e)?,
} }
} }
let result = writer.into_inner().into_inner(); let result = writer.into_inner().into_inner();