server.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use crate::schema;
  2. mod identity;
  3. mod oidc;
  4. pub struct ServerCoreState {
  5. pool: microrm::DBPool<'static>,
  6. templates: std::sync::RwLock<handlebars::Handlebars<'static>>,
  7. }
  8. #[derive(Clone)]
  9. struct ServerState {
  10. core: &'static ServerCoreState,
  11. }
  12. fn is_auth_valid<T>(core: &'static ServerCoreState, of: &tide::Request<T>) -> Option<bool> {
  13. let cookie = of.cookie("vogt_session")?;
  14. let session_id = cookie.value();
  15. Some(
  16. core.pool
  17. .query_interface()
  18. .get_one_by(crate::schema::SessionColumns::Key, session_id)
  19. .is_some(),
  20. )
  21. }
  22. pub async fn run_server(db: microrm::DB) {
  23. let db_box = Box::new(db);
  24. let db: &'static mut microrm::DB = Box::leak(db_box);
  25. let pool = microrm::DBPool::new(db);
  26. let core_state = Box::leak(Box::new(ServerCoreState { pool, templates: std::sync::RwLock::new(handlebars::Handlebars::new()) }));
  27. let state = ServerState { core: core_state };
  28. // XXX: for development only
  29. core_state.templates.write().unwrap().set_dev_mode(true);
  30. core_state.templates.write().unwrap().register_templates_directory("tmpl", "tmpl").expect("Couldn't open templates directory?");
  31. let mut app = tide::with_state(state);
  32. app.with(tide::log::LogMiddleware::new());
  33. /*app.at("/:realm/login").get(login); // serve_file("srv/login.html").expect("Can't serve login.html");
  34. app.at("/:realm/do_login").post(do_login);
  35. app.at("/:realm/logout")
  36. .serve_file("srv/logout.html")
  37. .expect("Can't serve logout.html");*/
  38. app.at("/static")
  39. .serve_dir("static/")
  40. .expect("Can't serve static files");
  41. app.at("/:realm/v1/id/")
  42. .nest(identity::id_v1_server(core_state));
  43. app.at("/:realm/v1/oidc")
  44. .nest(oidc::oidc_v1_server(core_state));
  45. app.listen("127.0.0.1:2114").await.expect("Can listen");
  46. }