Last active
July 2, 2016 20:06
-
-
Save dseg/c7ee719c4cf37b8b4769 to your computer and use it in GitHub Desktop.
nickel Webフレームワークを使ってみる ref: http://qiita.com/dseg/items/66ec3bcecadb0317f85e
This file contains hidden or 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
[package] | |
name = "nickel-helloworld-postgres" | |
version = "0.1.0" | |
authors = ["johndoe"] | |
[dependencies] | |
nickel = "*" | |
r2d2 = "*" | |
postgres = "*" | |
openssl = "*" | |
[dependencies.nickel_postgres] | |
git = "https://github.com/nickel-org/nickel-postgres.git" |
This file contains hidden or 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
-- スキーマと初期データ | |
CREATE TABLE counter ( | |
id SERIAL, | |
counter SMALLINT NOT NULL DEFAULT 0 | |
); | |
INSERT INTO counter (id, counter) VALUES (0, 1); |
This file contains hidden or 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
sudo -i | |
add-apt-repository -y ppa:hansjorg/rust | |
apt-get update | |
apt-get install rust-stable cargo-nightly | |
exit | |
rustc --version | |
rustc 1.4.0-dev | |
ls -lH $(which rustc) | |
-rwxr-xr-x 1 root root 6064 Nov 1 19:59 /usr/bin/rustc | |
cargo --version | |
cargo 0.7.0 (b6cc27a 2015-11-28) |
This file contains hidden or 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
$ cargo new nickel-helloworld --bin |
This file contains hidden or 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
cargo new nickel-helloworld-postgres --bin |
This file contains hidden or 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
$ ~/devel/rust/nickel-helloworld-postgres$ curl http://localhost:6767/count | |
<h1>Hello</h1><br>your are the visitor # 21. | |
$ ~/devel/rust/nickel-helloworld-postgres$ curl http://localhost:6767/count | |
<h1>Hello</h1><br>your are the visitor # 22. | |
$ ~/devel/rust/nickel-helloworld-postgres$ curl http://localhost:6767/count | |
<h1>Hello</h1><br>your are the visitor # 23. |
This file contains hidden or 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
$ cd devel/rust/nickel-helloworld | |
~/devel/rust/nickel-helloworld$ ab -n 10 -c 1 http://localhost:6767/ | |
This is ApacheBench, Version 2.3 <$Revision: 1528965 $> | |
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ | |
Licensed to The Apache Software Foundation, http://www.apache.org/ | |
Benchmarking localhost (be patient).....done | |
Server Software: Nickel | |
Server Hostname: localhost | |
Server Port: 6767 | |
Document Path: / | |
Document Length: 23 bytes | |
Concurrency Level: 1 | |
Time taken for tests: 0.009 seconds | |
Complete requests: 10 | |
Failed requests: 0 | |
Total transferred: 1670 bytes | |
HTML transferred: 230 bytes | |
Requests per second: 1133.92 [#/sec] (mean) | |
Time per request: 0.882 [ms] (mean) | |
Time per request: 0.882 [ms] (mean, across all concurrent requests) | |
Transfer rate: 184.93 [Kbytes/sec] received | |
Connection Times (ms) | |
min mean[+/-sd] median max | |
Connect: 0 0 0.1 0 0 | |
Processing: 0 1 0.9 1 3 | |
Waiting: 0 1 0.9 1 3 | |
Total: 1 1 1.0 1 4 | |
Percentage of the requests served within a certain time (ms) | |
50% 1 | |
66% 1 | |
75% 1 | |
80% 1 | |
90% 4 | |
95% 4 | |
98% 4 | |
99% 4 | |
100% 4 (longest request) |
This file contains hidden or 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
# cargo build --release | |
cargo run --release # build & run | |
Running `target/release/nickel-helloworld` | |
Listening on http://127.0.0.1:6767 | |
Ctrl-C to shutdown server |
This file contains hidden or 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
$ ab -n 10 -c 1 http://localhost:6767/ | |
This is ApacheBench, Version 2.3 <$Revision: 1528965 $> | |
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ | |
Licensed to The Apache Software Foundation, http://www.apache.org/ | |
Benchmarking localhost (be patient).....done | |
Server Software: Nickel | |
Server Hostname: localhost | |
Server Port: 6767 | |
Document Path: / | |
Document Length: 23 bytes | |
Concurrency Level: 1 | |
Time taken for tests: 0.001 seconds | |
Complete requests: 10 | |
Failed requests: 0 | |
Total transferred: 1670 bytes | |
HTML transferred: 230 bytes | |
Requests per second: 9433.96 [#/sec] (mean) | |
Time per request: 0.106 [ms] (mean) | |
Time per request: 0.106 [ms] (mean, across all concurrent requests) | |
Transfer rate: 1538.55 [Kbytes/sec] received | |
Connection Times (ms) | |
min mean[+/-sd] median max | |
Connect: 0 0 0.0 0 0 | |
Processing: 0 0 0.0 0 0 | |
Waiting: 0 0 0.0 0 0 | |
Total: 0 0 0.0 0 0 | |
Percentage of the requests served within a certain time (ms) | |
50% 0 | |
66% 0 | |
75% 0 | |
80% 0 | |
90% 0 | |
95% 0 | |
98% 0 | |
99% 0 | |
100% 0 (longest request) |
This file contains hidden or 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
# cargo runだと ビルド・実行を連続で行う | |
# | |
$ cargo build | |
Compiling rand v0.3.12 | |
Compiling matches v0.1.2 | |
Compiling rustc-serialize v0.3.16 | |
Compiling mustache v0.6.3 | |
Compiling num v0.1.28 | |
Compiling serde v0.6.6 | |
Compiling unicase v1.0.1 | |
Compiling modifier v0.1.0 | |
Compiling time v0.1.34 | |
Compiling num_cpus v0.2.10 | |
Compiling uuid v0.1.18 | |
Compiling url v0.2.38 | |
Compiling cookie v0.1.21 | |
Compiling mime v0.1.1 | |
Compiling typemap v0.3.3 | |
Compiling plugin v0.2.6 | |
Compiling memchr v0.1.7 | |
Compiling aho-corasick v0.4.0 | |
Compiling regex v0.1.43 | |
Compiling language-tags v0.0.7 | |
Compiling hyper v0.6.16 | |
Compiling nickel v0.7.3 | |
Compiling nickel-helloworld v0.1.0 (file:///home/ubuntu/devel/rust/nickel-helloworld) |
This file contains hidden or 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
$ cargo run | |
Running `target/debug/nickel-helloworld` | |
Listening on http://127.0.0.1:6767 | |
Ctrl-C to shutdown server | |
$ curl http://127.0.0.1:6767/user/dseg | |
<h1>This is user: "dseg"</h1> |
This file contains hidden or 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
$ cargo run | |
~/devel/rust/nickel-helloworld$ cargo run | |
Running `target/debug/nickel-helloworld` | |
Listening on http://127.0.0.1:6767 | |
Ctrl-C to shutdown server |
This file contains hidden or 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
$:~/devel/rust/nickel-helloworld$ curl http://localhost:6767 | |
<html> | |
<head> | |
<title>A Simple Mustache Demo</title> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1>A Simple Mustache Demo</h1> | |
<h4>Product Info: California Apple</h4> | |
<ul> | |
<li>Product: California Apple</li> | |
<li>Color: Green</li> | |
<li>Price: $2.50</li> | |
</ul> | |
</body> | |
</html> |
This file contains hidden or 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
<html> | |
<head> | |
<title>A Simple Mustache Demo</title> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<h1>A Simple Mustache Demo</h1> | |
<h4>Product Info: {{name}}</h4> | |
<ul> | |
<li>Product: {{name}}</li> | |
<li>Color: {{color}}</li> | |
<li>Price: ${{price}}</li> | |
</ul> | |
</body> | |
</html> |
This file contains hidden or 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
#[macro_use] extern crate nickel; | |
extern crate r2d2; | |
extern crate postgres; | |
extern crate openssl; | |
extern crate nickel_postgres; | |
use nickel::{Nickel,HttpRouter}; | |
use r2d2::NopErrorHandler; | |
use postgres::SslMode; | |
use nickel_postgres::{PostgresMiddleware, PostgresRequestExtensions}; | |
fn main() { | |
let mut serv = Nickel::new(); | |
let dsn = "postgres://dbuser:[email protected]/counter"; | |
let dbpool = PostgresMiddleware::new(&*dsn, | |
SslMode::None, | |
5, | |
Box::new(NopErrorHandler)).unwrap(); | |
serv.utilize(dbpool); | |
serv.get("/count", | |
middleware! {|req, res| | |
let conn = req.db_conn(); | |
let stmt = conn.prepare("SELECT counter FROM counter WHERE id = 0").unwrap(); | |
let rows = &stmt.query(&[]).unwrap(); | |
let mut counter:i16 = 0; // Int2(smallint) of Postgres is i16 | |
for row in rows { | |
counter = row.get(0); | |
} | |
// also print to stdout | |
println!("counter value is {}", counter); | |
// Up and save the counter value (+1) | |
conn.execute("UPDATE counter SET counter = counter + 1 WHERE id = 0", &[]).unwrap(); | |
format!("<h1>Hello</h1><br>your are the visitor # {}.\n", counter) | |
}); | |
serv.listen("localhost:6767"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment