aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/mod.rs
blob: e6da485dca7425f010af1a072dfc46a8240e3f96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use crate::{
    helper::{language::AcceptLanguage, A},
    CONF,
};
use error::MyResult;
use home::rocket_uri_macro_r_home;
use jellylogic::session::Session;
use jellyui::{render_page, scaffold::RenderInfo, CustomPage};
use rocket::{
    futures::FutureExt,
    get,
    response::{content::RawHtml, Redirect},
    Either,
};
use std::{future::Future, pin::Pin};
use tokio::{
    fs::{read_to_string, File},
    io::AsyncRead,
};

pub mod account;
pub mod admin;
pub mod assets;
pub mod error;
pub mod home;
pub mod items;
pub mod node;
pub mod player;
pub mod search;
pub mod stats;
pub mod style;

#[get("/")]
pub async fn r_index(
    lang: AcceptLanguage,
    sess: Option<A<Session>>,
) -> MyResult<Either<Redirect, RawHtml<String>>> {
    let AcceptLanguage(lang) = lang;
    if sess.is_some() {
        Ok(Either::Left(Redirect::temporary(rocket::uri!(r_home()))))
    } else {
        let front = read_to_string(CONF.asset_path.join("front.htm")).await?;
        Ok(Either::Right(RawHtml(render_page(
            &CustomPage {
                title: "Jellything".to_string(),
                body: front,
            },
            RenderInfo {
                importing: false,
                session: None,
                lang,
            },
        ))))
    }
}

#[get("/favicon.ico")]
pub async fn r_favicon() -> MyResult<File> {
    Ok(File::open(CONF.asset_path.join("favicon.ico")).await?)
}

pub struct Defer(Pin<Box<dyn Future<Output = String> + Send>>);

impl AsyncRead for Defer {
    fn poll_read(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut tokio::io::ReadBuf<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        match self.0.poll_unpin(cx) {
            std::task::Poll::Ready(r) => {
                buf.put_slice(r.as_bytes());
                std::task::Poll::Ready(Ok(()))
            }
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }
}