Add cli & lib crates
This commit is contained in:
88
server/src/main.rs
Normal file
88
server/src/main.rs
Normal file
@ -0,0 +1,88 @@
|
||||
mod auth;
|
||||
mod database;
|
||||
mod handlebars_util;
|
||||
mod routes;
|
||||
mod status_json;
|
||||
mod util;
|
||||
|
||||
use crate::auth::MasterPassword;
|
||||
use crate::database::migrations::migrate;
|
||||
use crate::database::unversioned::global::schema_version;
|
||||
use crate::database::SCHEMA_VERSION;
|
||||
use crate::util::EventNotifier;
|
||||
use bincode::{deserialize, serialize};
|
||||
use dotenv::dotenv;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use rocket_contrib::templates::Template;
|
||||
use std::{env, io};
|
||||
|
||||
#[rocket::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
dotenv().ok();
|
||||
|
||||
let db_path = env::var("DB_PATH").expect("DB_PATH not set");
|
||||
|
||||
let master_pass: MasterPassword = env::var("STL_PASSWORD")
|
||||
.unwrap_or_else(|_| {
|
||||
eprintln!(r#"STL_PASSWORD not set, defaulting to "password""#);
|
||||
"password".into()
|
||||
})
|
||||
.into();
|
||||
|
||||
let mut sled = sled::open(db_path)?;
|
||||
match sled.insert(
|
||||
serialize(schema_version::K).unwrap(),
|
||||
serialize(&SCHEMA_VERSION).unwrap(),
|
||||
)? {
|
||||
Some(prev_schema_version) => {
|
||||
let prev_schema_version: schema_version::V = deserialize(&prev_schema_version).unwrap();
|
||||
println!(
|
||||
"Schema version: {}, previously: {}",
|
||||
SCHEMA_VERSION, prev_schema_version
|
||||
);
|
||||
|
||||
migrate(&mut sled, prev_schema_version, SCHEMA_VERSION).expect("Migration failed")
|
||||
}
|
||||
None => {
|
||||
println!("Schema version: {}, previously: None", SCHEMA_VERSION);
|
||||
}
|
||||
}
|
||||
|
||||
let rocket = rocket::build()
|
||||
.attach(Template::custom(|engines| {
|
||||
handlebars_util::register_helpers(engines)
|
||||
}))
|
||||
.manage(sled)
|
||||
.manage(master_pass)
|
||||
.manage(EventNotifier::new())
|
||||
.mount("/static", StaticFiles::from("static"))
|
||||
.mount(
|
||||
"/",
|
||||
rocket::routes![
|
||||
routes::pages::index,
|
||||
routes::pages::history,
|
||||
routes::pages::session_edit,
|
||||
routes::pages::stats::single_stats,
|
||||
routes::pages::stats::all_stats,
|
||||
],
|
||||
)
|
||||
.mount(
|
||||
"/api",
|
||||
rocket::routes![
|
||||
routes::api::edit_session,
|
||||
routes::api::get_sessions,
|
||||
routes::api::create_category,
|
||||
routes::api::start_session,
|
||||
routes::api::end_session,
|
||||
routes::api::bump_session,
|
||||
routes::api::delete_session,
|
||||
routes::api::wait_for_event,
|
||||
auth::login,
|
||||
],
|
||||
)
|
||||
.register("/", rocket::catchers![auth::login_page,]);
|
||||
|
||||
rocket.launch().await.expect("rocket failed to launch");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user