diff options
author | metamuffin <metamuffin@disroot.org> | 2023-02-12 19:42:39 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-02-12 19:42:39 +0100 |
commit | 4423c2e86179168339d90adbda53d9777e953db5 (patch) | |
tree | 776147c3045610698c66edc92b99cfed9669f5d8 /src | |
parent | 1163ff8782eb10545c3bc1d044e9fe4bd7f3e0dd (diff) | |
download | metamuffin-website-4423c2e86179168339d90adbda53d9777e953db5.tar metamuffin-website-4423c2e86179168339d90adbda53d9777e953db5.tar.bz2 metamuffin-website-4423c2e86179168339d90adbda53d9777e953db5.tar.zst |
switching to rocket
Diffstat (limited to 'src')
-rw-r--r-- | src/layout.rs | 62 | ||||
-rw-r--r-- | src/main.rs | 62 |
2 files changed, 124 insertions, 0 deletions
diff --git a/src/layout.rs b/src/layout.rs new file mode 100644 index 0000000..f1d0564 --- /dev/null +++ b/src/layout.rs @@ -0,0 +1,62 @@ +/* + This file is part of metamuffins website (https://codeberg.org/metamuffin/website) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin <metamuffin.org> +*/ +use markup::Render; +use rocket::{ + http::ContentType, + response::{self, Responder}, + Request, Response, +}; +use std::io::Cursor; + +markup::define! { + Layout<Main: Render>(title: String, main: Main) { + @markup::doctype() + html { + head { + title { @title " - " "metamuffin's website" } + } + body { + h1 { "metamuffin's personal website" } + nav { + + } + hr; + @main + hr; + footer { + p { "metamuffin's website; " a[href="https://"] {} } + } + } + } + } +} + +pub type DynLayoutPage<'a> = LayoutPage<markup::DynRender<'a>>; + +pub struct LayoutPage<T> { + pub title: String, + pub content: T, +} + +impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> { + fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> { + // TODO blocking the event loop here. it seems like there is no other way to + // TODO offload this, since the guard references `req` which has a lifetime. + // TODO therefore we just block. that is fine since the database is somewhat fast. + let mut out = String::new(); + Layout { + main: self.content, + title: self.title, + } + .render(&mut out) + .unwrap(); + + Response::build() + .header(ContentType::HTML) + .streamed_body(Cursor::new(out)) + .ok() + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..746dfa3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,62 @@ +/* + This file is part of metamuffins website (https://codeberg.org/metamuffin/website) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin <metamuffin.org> +*/ +pub mod layout; + +use rocket::{ + fairing::AdHoc, + fs::FileServer, + get, + http::Header, + response::{self, Responder}, + routes, + serde::json::{json, Value}, + Request, +}; + +#[tokio::main] +async fn main() { + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .parse_env("LOG") + .init(); + + let _ = rocket::build() + .attach(AdHoc::on_response("set server header", |_req, res| { + res.set_header(Header::new("server", "blub")); + Box::pin(async {}) + })) + .mount( + "/", + routes![r_wellknown_matrix_server, r_wellknown_matrix_client,], + ) + .mount("/", FileServer::from("modules")) + .launch() + .await + .unwrap(); +} + +pub struct Cors<T>(pub T); + +#[rocket::async_trait] +impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Cors<T> { + fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> { + let mut resp = self.0.respond_to(request); + if let Ok(resp) = &mut resp { + resp.set_header(Header::new("access-control-allow-origin", "*")); + } + resp + } +} + +#[get("/.well-known/matrix/client")] +fn r_wellknown_matrix_client() -> Cors<Value> { + Cors(json!({"m.homeserver": {"base_url": "https://matrix.metamuffin.org"}} )) +} + +#[get("/.well-known/matrix/server")] +fn r_wellknown_matrix_server() -> Cors<Value> { + Cors(json!({"m.server": "matrix.metamuffin.org:443"} )) +} |