lykin/src/main.rs

56 lines
1.2 KiB
Rust

mod db;
mod routes;
mod sbot;
mod task_loop;
mod utils;
use std::{env, path::Path};
use async_std::channel;
use log::info;
use rocket::{
fairing::AdHoc,
fs::{relative, FileServer},
launch, routes,
};
use rocket_dyn_templates::Template;
use crate::{db::Database, routes::*, task_loop::Task};
pub struct WhoAmI {
pub public_key: String,
}
#[launch]
async fn rocket() -> _ {
env_logger::init();
let public_key: String = sbot::whoami().await.expect("whoami sbot call failed");
let whoami = WhoAmI { public_key };
let db = Database::init(Path::new("lykin_db"));
let db_clone = db.clone();
let (tx, rx) = channel::unbounded();
let tx_clone = tx.clone();
task_loop::spawn(rx, db_clone).await;
info!("launching the web server");
rocket::build()
.manage(db)
.manage(whoami)
.manage(tx)
.mount(
"/",
routes![home, subscribe_form, unsubscribe_form, posts, post],
)
.mount("/", FileServer::from(relative!("static")))
.attach(Template::fairing())
.attach(AdHoc::on_shutdown("cancel task loop", |_| {
Box::pin(async move {
tx_clone.send(Task::Cancel).await;
})
}))
}