1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- use crate::{config, schema, UIDCError};
- // mod oidc;
- mod session;
- // mod um;
- pub struct ServerState {
- config: config::Config,
- db: schema::UIDCDatabase,
- templates: handlebars::Handlebars<'static>,
- }
- #[derive(Clone)]
- struct ServerStateWrapper {
- core: &'static ServerState,
- }
- async fn index(req: tide::Request<ServerStateWrapper>) -> tide::Result<tide::Response> {
- let shelper = session::SessionHelper::new(&req);
- let realm = shelper.get_realm()?;
- let session = shelper.get_session(&req);
- let auth = session
- .as_ref()
- .and_then(|session| shelper.get_auth_for_session(realm.id(), &session));
- let response = tide::Response::builder(200)
- .content_type(tide::http::mime::PLAIN)
- .body(format!(
- r#"
- realm: {realm:?}
- session: {session:?}
- auth: {auth:?}
- "#
- ))
- .build();
- Ok(response)
- }
- pub async fn run_server(
- db: schema::UIDCDatabase,
- config: config::Config,
- port: u16,
- ) -> Result<(), UIDCError> {
- let core_state = Box::leak(Box::new(ServerState {
- config,
- db,
- templates: handlebars::Handlebars::new(),
- }));
- core_state.templates.set_dev_mode(true);
- core_state
- .templates
- .register_templates_directory(".tmpl", "tmpl/")
- .expect("Couldn't open templates directory?");
- let state = ServerStateWrapper { core: core_state };
- let mut app = tide::with_state(state);
- app.with(tide::log::LogMiddleware::new());
- app.at("/:realm/").get(index);
- app.at("/favicon.ico")
- .serve_file("static/favicon.ico")
- .expect("couldn't serve favicon.ico");
- app.at("/static")
- .serve_dir("static/")
- .expect("Can't serve static files");
- session::session_v1_server(app.at("/:realm/v1/session/"));
- // oidc::oidc_server(app.at("/:realm/"));
- // um::um_server(app.at("/:realm/um/"));
- app.listen(("127.0.0.1", port))
- .await
- .map_err(|_| UIDCError::Abort("couldn't listen on port"))?;
- Ok(())
- }
|