feat: Template index

This commit is contained in:
Nathan McCarty 2023-07-26 03:15:56 -04:00
parent 5d8521b646
commit 11bd5897bf
Signed by: thatonelutenist
SSH Key Fingerprint: SHA256:3elIBybO7zXuCg+/os7OlO2fwfRPXmObQjcHXBf7Hfg
6 changed files with 151 additions and 8 deletions

87
Cargo.lock generated
View File

@ -205,6 +205,33 @@ dependencies = [
"phf_codegen", "phf_codegen",
] ]
[[package]]
name = "color-eyre"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.4" version = "0.8.4"
@ -246,6 +273,16 @@ dependencies = [
"crypto-common", "crypto-common",
] ]
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -457,6 +494,12 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@ -584,6 +627,12 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.1" version = "0.12.1"
@ -912,6 +961,15 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "sharded-slab"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31"
dependencies = [
"lazy_static",
]
[[package]] [[package]]
name = "signal-hook-registry" name = "signal-hook-registry"
version = "1.4.1" version = "1.4.1"
@ -957,6 +1015,7 @@ name = "stranger-site"
version = "0.1.0-dev.0" version = "0.1.0-dev.0"
dependencies = [ dependencies = [
"axum", "axum",
"color-eyre",
"jotdown", "jotdown",
"tera", "tera",
"tokio", "tokio",
@ -1109,6 +1168,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
] ]
[[package]] [[package]]
@ -1194,6 +1275,12 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View File

@ -12,6 +12,7 @@ license-file = "LICENSE.md"
[dependencies] [dependencies]
axum = "0.6.19" axum = "0.6.19"
color-eyre = "0.6.2"
jotdown = "0.3.0" jotdown = "0.3.0"
tera = "1.19.0" tera = "1.19.0"
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }

View File

@ -0,0 +1,21 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico in the root directory -->
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
your experience.
</p>
<![endif]-->
<h1>Welcome to {{ title }}!</h1>
</body>
</html>

View File

@ -1,14 +1,15 @@
use axum::{routing::get, Router}; use axum::{routing::get, Router};
use std::{net::SocketAddr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use color_eyre::eyre::{Context, Result};
use stranger_site::{routes::index::index, AppState}; use stranger_site::{routes::index::index, AppState};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<()> {
// Install color-eyre
color_eyre::install()?;
// Build our state // Build our state
let shared_state = AppState { let shared_state = AppState::new("Stranger Systems").context("Failed building app state")?;
title: "Stranger Systems".to_string(),
};
// build our application with a route // build our application with a route
let app = Router::new() let app = Router::new()
.route("/", get(index)) .route("/", get(index))
@ -23,4 +24,6 @@ async fn main() {
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await
.unwrap(); .unwrap();
Ok(())
} }

View File

@ -1,6 +1,20 @@
use color_eyre::eyre::{Context, Result};
use tera::Tera;
pub mod routes; pub mod routes;
/// Shared state for the application /// Shared state for the application
pub struct AppState { pub struct AppState {
pub title: String, pub title: String,
pub tera: Tera,
}
impl AppState {
pub fn new(title: impl AsRef<str>) -> Result<AppState> {
let title = title.as_ref().to_string();
let mut tera = Tera::default();
tera.add_raw_template("index.html", include_str!("../assets/templates/index.html"))
.context("Failed to load built in template for index.html")?;
Ok(AppState { title, tera })
}
} }

View File

@ -1,11 +1,28 @@
use std::sync::Arc; use std::sync::Arc;
use axum::{extract::State, response::Html}; use axum::{extract::State, http::StatusCode, response::Html};
use color_eyre::eyre::Context;
use crate::AppState; use crate::AppState;
/// Render the front page /// Render the front page
pub async fn index(State(state): State<Arc<AppState>>) -> Html<String> { async fn index_inner(State(state): State<Arc<AppState>>) -> color_eyre::eyre::Result<Html<String>> {
let title: &str = state.title.as_ref(); eprintln!("Rendering index page");
Html(format!("<h1>Hello, World! Site Title: {title}</h1>")) let mut context = tera::Context::new();
context.insert("title", &state.title);
let rendered = state
.tera
.render("index.html", &context)
.context("Failed rendering index.html")?;
Ok(Html(rendered))
}
pub async fn index(state: State<Arc<AppState>>) -> Result<Html<String>, (StatusCode, String)> {
match index_inner(state).await {
Ok(x) => Ok(x),
Err(err) => {
let err = format!("{:?}", err);
Err((StatusCode::BAD_REQUEST, err))
}
}
} }