server.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. use crate::{config, schema, UIDCError};
  2. // mod oidc;
  3. mod session;
  4. // mod um;
  5. pub struct ServerState {
  6. config: config::Config,
  7. db: schema::UIDCDatabase,
  8. templates: handlebars::Handlebars<'static>,
  9. }
  10. #[derive(Clone)]
  11. struct ServerStateWrapper {
  12. core: &'static ServerState,
  13. }
  14. async fn index(req: tide::Request<ServerStateWrapper>) -> tide::Result<tide::Response> {
  15. let shelper = session::SessionHelper::new(&req);
  16. let realm = shelper.get_realm()?;
  17. let session = shelper.get_session(&req);
  18. let auth = session
  19. .as_ref()
  20. .and_then(|session| shelper.get_auth_for_session(realm.id(), &session));
  21. let response = tide::Response::builder(200)
  22. .content_type(tide::http::mime::PLAIN)
  23. .body(format!(
  24. r#"
  25. realm: {realm:?}
  26. session: {session:?}
  27. auth: {auth:?}
  28. "#
  29. ))
  30. .build();
  31. Ok(response)
  32. }
  33. pub async fn run_server(
  34. db: schema::UIDCDatabase,
  35. config: config::Config,
  36. port: u16,
  37. ) -> Result<(), UIDCError> {
  38. let core_state = Box::leak(Box::new(ServerState {
  39. config,
  40. db,
  41. templates: handlebars::Handlebars::new(),
  42. }));
  43. core_state.templates.set_dev_mode(true);
  44. core_state
  45. .templates
  46. .register_templates_directory(".tmpl", "tmpl/")
  47. .expect("Couldn't open templates directory?");
  48. let state = ServerStateWrapper { core: core_state };
  49. let mut app = tide::with_state(state);
  50. app.with(tide::log::LogMiddleware::new());
  51. app.at("/:realm/").get(index);
  52. app.at("/favicon.ico")
  53. .serve_file("static/favicon.ico")
  54. .expect("couldn't serve favicon.ico");
  55. app.at("/static")
  56. .serve_dir("static/")
  57. .expect("Can't serve static files");
  58. session::session_v1_server(app.at("/:realm/v1/session/"));
  59. // oidc::oidc_server(app.at("/:realm/"));
  60. // um::um_server(app.at("/:realm/um/"));
  61. app.listen(("127.0.0.1", port))
  62. .await
  63. .map_err(|_| UIDCError::Abort("couldn't listen on port"))?;
  64. Ok(())
  65. }