subscribe-list/src/lib.rs
2024-04-08 18:25:32 -07:00

66 lines
1.9 KiB
Rust

use crate::{
cli::{Listen, ListenFdSocket, ListenFdSockets},
db::DbThread,
};
use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::{cookie::Key, web, App, HttpServer};
use clap::Parser;
use diesel::Connection;
use listenfd::ListenFd;
mod app;
mod cli;
mod client;
mod config;
mod db;
mod models;
mod schema;
pub async fn main() -> eyre::Result<()> {
env_logger::init();
let mut listenfd = ListenFd::from_env();
color_eyre::install()?;
let mut args = cli::CLI::parse();
let db = web::Data::new(DbThread::new(
diesel::SqliteConnection::establish(&args.config.sqlite_db)?,
args.config.db_thread_channel_capacity.get().into(),
));
args.resolve(&mut listenfd).await?;
log::info!("retrieved config from auth servers");
let config = web::Data::new(args.config);
let cookie_session_key = Key::generate();
let mut server = HttpServer::new(move || {
let cookie_session =
SessionMiddleware::builder(CookieSessionStore::default(), cookie_session_key.clone())
.cookie_name("session".into())
.build();
App::new()
.wrap(cookie_session)
.app_data(config.clone())
.app_data(db.clone())
.configure(app::all_services)
});
let Listen {
listen_tcp,
listen_unix,
listen_fd,
} = &mut args.listen;
for sock in listen_tcp.iter() {
server = server.bind_auto_h2c(sock)?;
}
for sock in listen_unix.iter() {
server = server.bind_uds(sock)?;
}
if let Some(ListenFdSockets(sockets)) = listen_fd {
for sock in sockets.drain(..) {
server = match sock {
ListenFdSocket::Tcp(sock) => server.listen_auto_h2c(sock)?,
ListenFdSocket::Unix(sock) => server.listen_uds(sock)?,
};
}
}
log::info!("started server");
server.run().await?;
Ok(())
}