-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.html
229 lines (164 loc) · 7.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/pygment_trac.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="stylesheets/highlight_default.css" media="all" />
<script src="javascripts/highlight.pack.js"></script>
<script src="javascripts/main.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-52880198-1', 'auto');
ga('send', 'pageview');
</script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>nickel.rs - web application framework for rust</title>
</head>
<body>
<header>
<div class="inner">
<h1>nickel.rs</h1>
<h2>web application framework for rust</h2>
<div class="header-buttons">
<iframe src="http://ghbtns.com/github-btn.html?user=nickel-org&repo=nickel.rs&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="130" height="30"></iframe>
<a href="/getting-started.html" class="button">Getting Started</a>
<a href="https://docs.rs/nickel/" class="button">API docs</a>
</div>
</div>
</header>
<div id="content-wrapper">
<div class="inner clearfix">
<section id="main-content">
<h3>
<a name="hello-world" class="anchor" href="#hello-world"><span class="octicon octicon-link"></span>Your first nickel app</a></h3>
<!-- <p>This automatic page generator is the easiest way to create beautiful pages for all of your projects. Author your page content here using GitHub Flavored Markdown, select a template crafted by a designer, and publish. After your page is generated, you can check out the new branch:</p> -->
<pre><code class="rust">#[macro_use] extern crate nickel;
use nickel::Nickel;
fn main() {
let mut server = Nickel::new();
server.utilize(router! {
get "**" => |_req, _res| {
"Hello world!"
}
});
server.listen("127.0.0.1:6767").unwrap();
}
</code></pre>
<h3>
<a name="routing" class="anchor" href="#routing"><span class="octicon octicon-link"></span>Flexible routing</a></h3>
<p>Routes can be as simple as <code>/foo</code>, use parameters, wildcards or even double wildcards.</p>
<pre><code class="rust">#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter};
fn main() {
let mut server = Nickel::new();
server.get("/bar", middleware!("This is the /bar handler"));
server.get("/user/:userid", middleware! { |request|
format!("This is user: {:?}", request.param("userid"))
});
server.get("/a/*/d", middleware!("matches /a/b/d but not /a/b/c/d"));
server.get("/a/**/d", middleware!("This matches /a/b/d and also /a/b/c/d"));
server.listen("127.0.0.1:6767").unwrap();
}</code></pre>
<h3>
<a name="middleware" class="anchor" href="#middleware"><span class="octicon octicon-link"></span>Middleware</a></h3>
<p>Middleware are the extensibility points of nickel. Batteries included! A bunch of existing Middleware comes right with nickel.</p>
<pre><code class="rust">#[macro_use] extern crate nickel;
use nickel::{Nickel, StaticFilesHandler};
fn main() {
let mut server = Nickel::new();
server.utilize(StaticFilesHandler::new("examples/assets/"));
server.listen("127.0.0.1:6767").unwrap();
}</code></pre>
<h3>
<a name="json-handling" class="anchor" href="#json-handling"><span class="octicon octicon-link"></span>JSON handling</a></h3>
<p>nickel makes it easy to map JSON data right onto your <code>struct</code>.</p>
<p>This example requires adding the <code>rustc-serialize</code> dependency to your Cargo.toml</p>
<pre><code class="rust">extern crate rustc_serialize;
#[macro_use] extern crate nickel;
use nickel::{Nickel, HttpRouter, JsonBody};
#[derive(RustcDecodable, RustcEncodable)]
struct Person {
firstname: String,
lastname: String,
}
fn main() {
let mut server = Nickel::new();
server.post("/a/post/request", middleware! { |request, response|
let person = request.json_as::<Person>().unwrap();
format!("Hello {} {}", person.firstname, person.lastname)
});
server.listen("127.0.0.1:6767").unwrap();
}</code></pre>
<h3>
<a name="error-handling" class="anchor" href="#error-handling"><span class="octicon octicon-link"></span>Custom error handler</a></h3>
<p>
By default nickel catches all errors with its default <code>ErrorHandler</code> and tries to take reasonable actions. In cases where one wants to provide an own <code>ErrorHandler</code> (e.g. for custom 404 pages), it's trivial to write one.
</p>
<pre><code class="rust">#[macro_use] extern crate nickel;
use std::io::Write;
use nickel::status::StatusCode::NotFound;
use nickel::{Nickel, NickelError, Action, Continue, Halt, Request};
fn main() {
let mut server = Nickel::new();
//this is how to overwrite the default error handler to handle 404 cases with a custom view
fn custom_404<'a>(err: &mut NickelError, _req: &mut Request) -> Action {
if let Some(ref mut res) = err.stream {
if res.status() == NotFound {
let _ = res.write_all(b"<h1>Call the police!</h1>");
return Halt(())
}
}
Continue(())
}
// issue #20178
let custom_handler: fn(&mut NickelError, &mut Request) -> Action = custom_404;
server.handle_error(custom_handler);
server.listen("127.0.0.1:6767").unwrap();
}</code></pre>
<h3>
<a name="easy-templating" class="anchor" href="#easy-templating"><span class="octicon octicon-link"></span>Easy Templating</a></h3>
<p>
Nickel supports defining templates with mustache.rs. All you need is to create the template...
</p>
<pre><code class="html"><html>
<body>
<h1>
Hello {{ name }}!
</h1>
</body>
</html>
</code></pre>
<p>
...and pass it to <code>response.render(template, data)</code>.
</p>
<pre><code class="rust">#[macro_use] extern crate nickel;
use std::collections::HashMap;
use nickel::{Nickel, HttpRouter};
fn main() {
let mut server = Nickel::new();
server.get("/", middleware! { |_, response|
let mut data = HashMap::new();
data.insert("name", "user");
return response.render("examples/assets/template.tpl", &data);
});
server.listen("127.0.0.1:6767").unwrap();
}</code></pre>
<h3>
<a name="about" class="anchor" href="#about"><span class="octicon octicon-link"></span>About</a></h3>
<p>nickel is brought to you by the people of <a href="http://thoughtram.io/">thoughtram</a> and friends. It's MIT licensed. We welcome your contribution.</p>
</section>
</div>
</div>
</body>
</html>