Initial commit
This commit is contained in:
88
src/main.rs
Normal file
88
src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use clap::Parser;
|
||||
use tracing_actix_web::TracingLogger;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
|
||||
use shanty_db::Database;
|
||||
use shanty_search::MusicBrainzSearch;
|
||||
use shanty_tag::MusicBrainzClient;
|
||||
|
||||
use shanty_web::config::AppConfig;
|
||||
use shanty_web::routes;
|
||||
use shanty_web::state::AppState;
|
||||
use shanty_web::tasks::TaskManager;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "shanty-web", about = "Shanty web interface backend")]
|
||||
struct Cli {
|
||||
/// Path to config file.
|
||||
#[arg(long, env = "SHANTY_CONFIG")]
|
||||
config: Option<String>,
|
||||
|
||||
/// Override the port.
|
||||
#[arg(long)]
|
||||
port: Option<u16>,
|
||||
|
||||
/// Increase verbosity (-v info, -vv debug, -vvv trace).
|
||||
#[arg(short, long, action = clap::ArgAction::Count)]
|
||||
verbose: u8,
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let filter = match cli.verbose {
|
||||
0 => "info,shanty_web=info",
|
||||
1 => "info,shanty_web=debug",
|
||||
_ => "debug,shanty_web=trace",
|
||||
};
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(filter)),
|
||||
)
|
||||
.init();
|
||||
|
||||
let mut config = AppConfig::load(cli.config.as_deref());
|
||||
if let Some(port) = cli.port {
|
||||
config.web.port = port;
|
||||
}
|
||||
|
||||
tracing::info!(url = %config.database_url, "connecting to database");
|
||||
let db = Database::new(&config.database_url).await?;
|
||||
|
||||
let mb_client = MusicBrainzClient::new()?;
|
||||
let search = MusicBrainzSearch::new()?;
|
||||
|
||||
let bind = format!("{}:{}", config.web.bind, config.web.port);
|
||||
tracing::info!(bind = %bind, "starting server");
|
||||
|
||||
let state = web::Data::new(AppState {
|
||||
db,
|
||||
mb_client,
|
||||
search,
|
||||
config,
|
||||
tasks: TaskManager::new(),
|
||||
});
|
||||
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::permissive();
|
||||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
.wrap(TracingLogger::default())
|
||||
.app_data(state.clone())
|
||||
.configure(routes::configure)
|
||||
.service(
|
||||
actix_files::Files::new("/", "static/")
|
||||
.index_file("index.html")
|
||||
.prefer_utf8(true),
|
||||
)
|
||||
})
|
||||
.bind(&bind)?
|
||||
.run()
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user