12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- use crate::schema;
- mod identity;
- mod oidc;
- pub struct ServerCoreState {
- pool: microrm::DBPool<'static>,
- templates: std::sync::RwLock<handlebars::Handlebars<'static>>,
- }
- #[derive(Clone)]
- struct ServerState {
- core: &'static ServerCoreState,
- }
- fn is_auth_valid<T>(core: &'static ServerCoreState, of: &tide::Request<T>) -> Option<bool> {
- let cookie = of.cookie("vogt_session")?;
- let session_id = cookie.value();
- Some(
- core.pool
- .query_interface()
- .get_one_by(crate::schema::SessionColumns::Key, session_id)
- .is_some(),
- )
- }
- pub async fn run_server(db: microrm::DB) {
- let db_box = Box::new(db);
- let db: &'static mut microrm::DB = Box::leak(db_box);
- let pool = microrm::DBPool::new(db);
- let core_state = Box::leak(Box::new(ServerCoreState { pool, templates: std::sync::RwLock::new(handlebars::Handlebars::new()) }));
- let state = ServerState { core: core_state };
- // XXX: for development only
- core_state.templates.write().unwrap().set_dev_mode(true);
- core_state.templates.write().unwrap().register_templates_directory("tmpl", "tmpl").expect("Couldn't open templates directory?");
- let mut app = tide::with_state(state);
- app.with(tide::log::LogMiddleware::new());
- /*app.at("/:realm/login").get(login); // serve_file("srv/login.html").expect("Can't serve login.html");
- app.at("/:realm/do_login").post(do_login);
- app.at("/:realm/logout")
- .serve_file("srv/logout.html")
- .expect("Can't serve logout.html");*/
- app.at("/static")
- .serve_dir("static/")
- .expect("Can't serve static files");
- app.at("/:realm/v1/id/")
- .nest(identity::id_v1_server(core_state));
- app.at("/:realm/v1/oidc")
- .nest(oidc::oidc_v1_server(core_state));
- app.listen("127.0.0.1:2114").await.expect("Can listen");
- }
|