little-hesinde/rusty-library/src/main.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

2024-05-02 16:10:29 +00:00
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, EndpointExt, Route, Server,
};
use rust_embed::RustEmbed;
mod app_state;
mod basic_auth;
mod cli;
mod config;
2024-05-06 07:09:40 +00:00
mod data {
pub mod book;
}
2024-05-02 16:10:29 +00:00
mod handlers {
2024-05-06 08:55:18 +00:00
pub mod authors;
pub mod books;
2024-05-02 16:10:29 +00:00
pub mod cover;
2024-05-06 08:40:34 +00:00
pub mod download;
2024-05-06 07:09:40 +00:00
pub mod error;
2024-05-06 11:51:49 +00:00
pub mod paginated;
2024-05-02 16:10:29 +00:00
pub mod recents;
2024-05-06 08:55:18 +00:00
pub mod series;
2024-05-02 16:10:29 +00:00
}
mod templates;
#[derive(RustEmbed)]
#[folder = "static"]
pub struct Files;
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "debug");
}
tracing_subscriber::fmt::init();
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))
2024-05-06 08:55:18 +00:00
.at("/books", get(handlers::books::handler))
2024-05-06 11:51:49 +00:00
.at("/authors", get(handlers::authors::handler_init))
.at(
"/authors/:cursor/:sort_order",
get(handlers::authors::handler),
)
2024-05-06 08:55:18 +00:00
.at("/series", get(handlers::series::handler))
2024-05-02 16:10:29 +00:00
.at("/cover/:id", get(handlers::cover::handler))
2024-05-06 08:40:34 +00:00
.at("/book/:id/:format", get(handlers::download::handler))
2024-05-02 16:10:29 +00:00
.nest("/static", EmbeddedFilesEndpoint::<Files>::new())
.data(app_state);
Server::new(TcpListener::bind("[::]:3000"))
.name("cops-web")
.run(app)
.await
}