forked from nickel-org/nickel.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(examples): Fix examples triggering issue nickel-org#399
Fix the examples that were returning MiddlewareResults in the middlware! macro. A couple could be changed to return a Responder, but most needed to be reimplemented as seperate functions. Updated the middleware! macro documentaion to note the limitations from issues nickel-org#399 and nickel-org#389.
- Loading branch information
Showing
14 changed files
with
78 additions
and
53 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 |
---|---|---|
@@ -1,30 +1,34 @@ | ||
#[macro_use] extern crate nickel; | ||
use nickel::{Nickel, HttpRouter, FormBody}; | ||
use nickel::{Nickel, HttpRouter, FormBody, Request, Response, MiddlewareResult}; | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let mut server = Nickel::new(); | ||
fn root<'mw, 'conn>(_req: &mut Request<'mw, 'conn>, res: Response<'mw>) -> MiddlewareResult<'mw> { | ||
let mut data = HashMap::new(); | ||
data.insert("title","Contact"); | ||
|
||
return res.render("examples/form_data/views/contact.html", &data) | ||
} | ||
|
||
server.get("/", middleware! { |_, res| | ||
let mut data = HashMap::new(); | ||
data.insert("title","Contact"); | ||
fn confirmation<'mw, 'conn>(req: &mut Request<'mw, 'conn>, res: Response<'mw>) -> MiddlewareResult<'mw> { | ||
let form_data = try_with!(res, req.form_body()); | ||
|
||
return res.render("examples/form_data/views/contact.html", &data) | ||
}); | ||
println!("{:?}", form_data); | ||
|
||
server.post("/confirmation", middleware!{ |req, res| | ||
let form_data = try_with!(res, req.form_body()); | ||
let mut data = HashMap::new(); | ||
data.insert("title", "Confirmation"); | ||
data.insert("firstname", form_data.get("firstname").unwrap_or("First name?")); | ||
data.insert("lastname", form_data.get("lastname").unwrap_or("Last name?")); | ||
data.insert("phone", form_data.get("phone").unwrap_or("Phone?")); | ||
data.insert("email", form_data.get("email").unwrap_or("Email?")); | ||
return res.render("examples/form_data/views/confirmation.html", &data) | ||
} | ||
|
||
fn main() { | ||
let mut server = Nickel::new(); | ||
|
||
println!("{:?}", form_data); | ||
server.get("/", root); | ||
|
||
let mut data = HashMap::new(); | ||
data.insert("title", "Confirmation"); | ||
data.insert("firstname", form_data.get("firstname").unwrap_or("First name?")); | ||
data.insert("lastname", form_data.get("lastname").unwrap_or("Last name?")); | ||
data.insert("phone", form_data.get("phone").unwrap_or("Phone?")); | ||
data.insert("email", form_data.get("email").unwrap_or("Email?")); | ||
return res.render("examples/form_data/views/confirmation.html", &data) | ||
}); | ||
server.post("/confirmation", confirmation); | ||
|
||
server.listen("0.0.0.0:8080").unwrap(); | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#[macro_use] extern crate nickel; | ||
extern crate nickel; | ||
|
||
use nickel::{Nickel, StaticFilesHandler}; | ||
|
||
|
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
#[macro_use] extern crate nickel; | ||
extern crate nickel; | ||
|
||
use nickel::{Nickel, HttpRouter}; | ||
use nickel::{Nickel, HttpRouter, Request, Response, MiddlewareResult}; | ||
use std::collections::HashMap; | ||
|
||
fn render<'mw, 'conn>(_req: &mut Request<'mw, 'conn>, res: Response<'mw>) -> MiddlewareResult<'mw> { | ||
let mut data = HashMap::<&str, &str>::new(); | ||
data.insert("name", "user"); | ||
return res.render("examples/assets/template.tpl", &data) | ||
} | ||
|
||
fn main() { | ||
let mut server = Nickel::new(); | ||
|
||
// * NOTE * | ||
// This example is deprecated, you should use nickel_mustache from crates.io | ||
server.get("/", middleware! { |_, res| | ||
let mut data = HashMap::<&str, &str>::new(); | ||
data.insert("name", "user"); | ||
return res.render("examples/assets/template.tpl", &data) | ||
}); | ||
server.get("/", render); | ||
|
||
server.listen("127.0.0.1:6767").unwrap(); | ||
} |
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
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
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
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
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
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
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