bugspray/src/main.rs
2022-03-05 17:53:59 -06:00

78 lines
1.9 KiB
Rust

#![feature(decl_macro)]
mod api;
mod database;
use std::{env, io, thread};
use database::DbExecutor;
use lazy_static::lazy_static;
use rocket::{
fs::FileServer,
get,
http::uri::{fmt::Path, Segments},
response, routes,
tokio::fs,
};
use sycamore::prelude::*;
use tokio::sync::RwLock;
use crate::database::ExecutorConnection;
#[get("/<path..>", rank = 2)]
async fn index(path: Segments<'_, Path>) -> io::Result<response::content::Html<String>> {
let index_html = String::from_utf8(fs::read("app/dist/index.html").await?)
.expect("index.html should be valid utf-8");
let mut pathname = String::new();
for segment in path {
pathname.push_str(segment);
pathname.push('/');
}
let rendered = sycamore::render_to_string(|| {
view! {
app::App(Some(pathname))
}
});
let index_html = index_html.replace("%sycamore.body", &rendered);
Ok(response::content::Html(index_html))
}
lazy_static! {
pub static ref EXECONN: RwLock<Option<ExecutorConnection>> = RwLock::new(None);
}
#[rocket::main]
async fn main() {
let database_path = env::var("BUGSPRAY_DB").expect("database path was not provided");
let (mut exec, execonn) = DbExecutor::create(&database_path).unwrap();
let executor_thread = thread::spawn(move || exec.run());
*(EXECONN.write().await) = Some(execonn);
rocket::build()
.mount(
"/",
routes![
index,
api::all_issues,
api::get_issue,
api::add_issue,
api::register,
api::get_user_by_username,
api::get_user_by_id,
],
)
.mount("/", FileServer::from("app/dist").rank(1))
.launch()
.await
.unwrap();
*(EXECONN.write().await) = None; // drop the connection
executor_thread.join().unwrap();
}