Skip to main content

web/
handler.rs

1use axum::{
2    Extension,
3    response::{IntoResponse, Response},
4};
5
6use crate::jwt_auth::JWTAuthMiddleware;
7use crate::response::WebError;
8use server::command::CmdResult;
9
10pub async fn get_version() -> Result<impl IntoResponse, WebError> {
11    if let Some(CmdResult::String(version)) =
12        server::command::config::GetVersion::new().run().await?
13        && let Some(CmdResult::String(build_date)) =
14            server::command::config::GetBuildDate::new().run().await?
15    {
16        Ok(Response::new(format!(
17            "<span class=\"version\">{version}</span><span class=\"build_date\" data-iso=\"{build_date}\"><script>
18    const el = document.currentScript.parentElement;
19    el.textContent = new Date(el.dataset.iso.trim()).toLocaleString();
20  </script></span>"
21        )))
22    } else {
23        Ok(Response::new("Unversioned".to_string()))
24    }
25}
26
27pub async fn get_logout_link(Extension(jwtauth): Extension<JWTAuthMiddleware>) -> Response {
28    // `/register` is admin-gated server-side; showing the link to everyone
29    // would only advertise a 401.
30    let admin_link = match jwtauth.user.role.as_str() {
31        "admin" => "<a href=\"/register\">Register user</a> ",
32        _ => "",
33    };
34    Response::new(
35        format!(
36            "<a href=\"/account/ssh-key\">Account</a> {admin_link}\
37             <a hx-get=\"/api/auth/logout\">Logout</a>"
38        )
39        .into(),
40    )
41}
42
43pub async fn get_home_link() -> Response {
44    Response::new("<a href='/'>Back to main</a>".into())
45}