Skip to content

Commit 56c961b

Browse files
committed
Merge pull request #7 from Aigeruth/example
Add an example
2 parents 7b1c73c + a7211a8 commit 56c961b

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/target
2+
/example/target
23
/Cargo.lock
4+
/example/Cargo.lock

example/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "example"
3+
version = "0.0.1"
4+
authors = ["Example User <[email protected]>"]
5+
6+
[dependencies]
7+
civet = "0.7"
8+
conduit = "0.7"
9+
conduit-router = "0.7"

example/src/main.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate civet;
2+
extern crate conduit;
3+
extern crate conduit_router;
4+
5+
use std::collections::HashMap;
6+
use std::io::{self, Cursor};
7+
use std::sync::mpsc::channel;
8+
9+
use civet::{Config, response, Server};
10+
use conduit::{Request, Response};
11+
use conduit_router::{RouteBuilder, RequestParams};
12+
13+
14+
fn name(req: &mut Request) -> io::Result<Response> {
15+
let name = req.params().find("name").unwrap();
16+
let bytes = format!("Hello {}!", name).into_bytes();
17+
18+
Ok(response(200, HashMap::new(), Cursor::new(bytes)))
19+
}
20+
21+
22+
fn hello(_req: &mut Request) -> io::Result<Response> {
23+
let hello = "Hello world!".to_string().into_bytes();
24+
Ok(response(200, HashMap::new(), Cursor::new(hello)))
25+
}
26+
27+
28+
fn main() {
29+
let mut router = RouteBuilder::new();
30+
31+
router.get("/", hello);
32+
router.get("/:name", name);
33+
34+
let _server = Server::start(Config { port: 8888, threads: 1 }, router);
35+
36+
// Preventing process exit.
37+
let (_tx, rx) = channel::<()>();
38+
rx.recv().unwrap();
39+
}

0 commit comments

Comments
 (0)