-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d8ffc1
commit 8d2cb48
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
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
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 = "../..", features = ["mime03"] } | ||
|
||
[dependencies] | ||
async-std = { version = "1.6.0", features = ["attributes"] } | ||
tide = "0.10.0" |
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
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") | ||
} |
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
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(()) | ||
} |
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
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(()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@(msg: &str) | ||
|
||
hello @msg |