Skip to content

Instantly share code, notes, and snippets.

@raphael-brand
Created April 5, 2023 17:06
Show Gist options
  • Save raphael-brand/d1bd0d8eea8f08a9203a6640454dbfbb to your computer and use it in GitHub Desktop.
Save raphael-brand/d1bd0d8eea8f08a9203a6640454dbfbb to your computer and use it in GitHub Desktop.
Tide Web Application - main file
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