Skip to content

Commit

Permalink
⚡️ Support SSR for document website.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 6, 2024
1 parent 0147230 commit 15d0734
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 43 deletions.
4 changes: 2 additions & 2 deletions website/examples/dev/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod render;
mod pages;
mod r#static;

use anyhow::Result;
Expand All @@ -21,7 +21,7 @@ async fn main() -> Result<()> {

let router = Router::new()
.nest("/", r#static::route().await?)
.nest("/", render::route().await?)
.nest("/", pages::route().await?)
.into_make_service_with_connect_info::<SocketAddr>();

info!("Site will run on http://localhost:{port}");
Expand Down
17 changes: 17 additions & 0 deletions website/examples/dev/pages/component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use anyhow::Result;
use std::str::FromStr;

use hikari_boot::Application;
use hikari_theme::types::ComponentType;
use hikari_website::app::{App, AppStates, PageData};

pub async fn html(id: String) -> Result<String> {
App::render_to_string(
format!("/component/{}", id),
AppStates {
theme: Default::default(),
data: PageData::Component(ComponentType::from_str(&id)?),
},
)
.await
}
18 changes: 18 additions & 0 deletions website/examples/dev/pages/guide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use anyhow::Result;

use hikari_boot::Application;
use hikari_website::app::{App, AppStates, PageData};

pub async fn html(id: String) -> Result<String> {
App::render_to_string(
format!("/guide/{}", id),
AppStates {
theme: Default::default(),
data: PageData::Guide {
id,
raw: "いいよ!こいよ".to_string(),
},
},
)
.await
}
62 changes: 62 additions & 0 deletions website/examples/dev/pages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
pub mod component;
pub mod guide;
pub mod portal;

use anyhow::Result;

use axum::{
extract::Path,
http::{header, HeaderMap, StatusCode},
routing::get,
Router,
};

use crate::pages;

pub async fn route() -> Result<Router> {
let router = Router::new()
.route(
"/",
get(|| async {
pages::portal::html()
.await
.map(|raw| {
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/html".parse().unwrap());

(headers, raw)
})
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))
}),
)
.route(
"/guide/:id",
get(move |Path(id): Path<String>| async move {
pages::guide::html(id)
.await
.map(|raw| {
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/html".parse().unwrap());

(headers, raw)
})
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))
}),
)
.route(
"/component/*id",
get(move |Path(id): Path<String>| async move {
pages::component::html(id)
.await
.map(|raw| {
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/html".parse().unwrap());

(headers, raw)
})
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))
}),
);

Ok(router)
}
15 changes: 15 additions & 0 deletions website/examples/dev/pages/portal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use anyhow::Result;

use hikari_boot::Application;
use hikari_website::app::{App, AppStates, PageData};

pub async fn html() -> Result<String> {
App::render_to_string(
"/".to_string(),
AppStates {
theme: Default::default(),
data: PageData::Portal,
},
)
.await
}
40 changes: 0 additions & 40 deletions website/examples/dev/render.rs

This file was deleted.

2 changes: 1 addition & 1 deletion website/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum Routes {

derive_struct!(
#[derive(PartialEq, Serialize, Deserialize)]
AppStates {
pub AppStates {
theme: Theme,
data: enum PageData {
Portal
Expand Down

0 comments on commit 15d0734

Please sign in to comment.