1
use axum::{
2
    Extension,
3
    response::{IntoResponse, Response},
4
};
5

            
6
use crate::jwt_auth::JWTAuthMiddleware;
7
use crate::response::WebError;
8
use server::command::CmdResult;
9

            
10
2
pub async fn get_version() -> Result<impl IntoResponse, WebError> {
11
    if let Some(CmdResult::String(version)) =
12
2
        server::command::config::GetVersion::new().run().await?
13
        && let Some(CmdResult::String(build_date)) =
14
2
            server::command::config::GetBuildDate::new().run().await?
15
    {
16
2
        Ok(Response::new(format!(
17
2
            "<span class=\"version\">{version}</span><span class=\"build_date\" data-iso=\"{build_date}\"><script>
18
2
    const el = document.currentScript.parentElement;
19
2
    el.textContent = new Date(el.dataset.iso.trim()).toLocaleString();
20
2
  </script></span>"
21
2
        )))
22
    } else {
23
        Ok(Response::new("Unversioned".to_string()))
24
    }
25
2
}
26

            
27
4
pub 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
4
    let admin_link = match jwtauth.user.role.as_str() {
31
1
        "admin" => "<a href=\"/register\">Register user</a> ",
32
3
        _ => "",
33
    };
34
4
    Response::new(
35
4
        format!(
36
4
            "<a href=\"/account/ssh-key\">Account</a> {admin_link}\
37
4
             <a hx-get=\"/api/auth/logout\">Logout</a>"
38
4
        )
39
4
        .into(),
40
4
    )
41
4
}
42

            
43
pub async fn get_home_link() -> Response {
44
    Response::new("<a href='/'>Back to main</a>".into())
45
}