Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tide example #80

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ matrix:
- cd ../simple && cargo fmt -- --check
- cd ../static-sass && cargo fmt -- --check
- cd ../statics && cargo fmt -- --check
- cd ../tide && cargo fmt -- --check
- cd ../warp02 && cargo fmt -- --check
- rust: stable
env: EXAMPLE=ed2018
Expand All @@ -33,6 +34,9 @@ matrix:
- rust: stable
env: EXAMPLE=warp02
script: cd examples/$EXAMPLE && cargo test
- rust: stable
env: EXAMPLE=tide
script: cd examples/$EXAMPLE && cargo test
- rust: stable
env: EXAMPLE=gotham
script: cd examples/$EXAMPLE && cargo test
Expand Down
14 changes: 14 additions & 0 deletions examples/tide/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "tide"
version = "0.4.0"
authors = ["Rasmus Kaj <[email protected]>"]
edition = "2018"

build = "src/build.rs"

[build-dependencies]
ructe = { path = "../.." }

[dependencies]
async-std = { version = "1.6.0", features = ["attributes"] }
tide = "0.10.0"
9 changes: 9 additions & 0 deletions examples/tide/src/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! This job builds rust source from templates,
//! which can then be `include!`d in `main.rs`.

use ructe::{Ructe, RucteError};

fn main() -> Result<(), RucteError> {
let mut ructe = Ructe::from_env()?;
ructe.compile_templates("templates")
}
24 changes: 24 additions & 0 deletions examples/tide/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// And finally, include the generated code for templates and static files.
include!(concat!(env!("OUT_DIR"), "/templates.rs"));

mod ructe_tide;
use ructe_tide::Render;

use tide::{Response, StatusCode};

#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();

app.at("/").get(|_| async {
let mut res = Response::new(StatusCode::Ok);
res.render_html(|o| Ok(templates::hello(o, "world")?))?;
Ok(res)
});

let addr = "127.0.0.1:3000";
println!("Starting server on http://{}/", addr);
app.listen(addr).await?;

Ok(())
}
30 changes: 30 additions & 0 deletions examples/tide/src/ructe_tide.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pub trait Render {
fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;

fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>;
}

impl Render for tide::Response {
fn render<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
{
let mut buf = Vec::new();
call(&mut buf)?;
self.set_body(buf);
Ok(())
}

fn render_html<Call>(&mut self, call: Call) -> std::io::Result<()>
where
Call: FnOnce(&mut dyn std::io::Write) -> std::io::Result<()>,
{
self.render(call)?;
self.set_content_type(tide::http::mime::HTML);
Ok(())
}
}
3 changes: 3 additions & 0 deletions examples/tide/templates/hello.rs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@(msg: &str)

hello @msg