Created
April 5, 2023 17:06
Tide Web Application - main file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use async_std::path::PathBuf; | |
use simple_tmpl_lib::TemplateEngine; | |
use tide::prelude::json; | |
use tide::Response; | |
use tide::StatusCode; | |
#[derive(Debug, serde::Serialize)] | |
struct MyError { | |
message: String, | |
} | |
#[async_std::main] | |
async fn main() -> tide::Result<()> { | |
tide::log::start(); | |
let engine = TemplateEngine::new(PathBuf::from("./templates/").into()); | |
let mut app = tide::with_state(engine); | |
app.at("/").get(|req: tide::Request<TemplateEngine>| { | |
let engine = req.state().clone(); | |
let template_string = engine.read_file("layout.hbs").unwrap(); | |
async move { | |
let rendered = engine.apply_template( | |
&template_string, | |
&json!({ | |
"title": "My Awesome Title", | |
"body": "This is the body of my template", | |
}), | |
)?; | |
Ok(Response::builder(StatusCode::Ok) | |
.body(rendered) | |
.content_type("text/html; charset=utf-8") | |
.build()) | |
} | |
}); | |
app.listen("127.0.0.1:8080").await?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment