Skip to content

Commit 67e13e8

Browse files
committed
Initial commit
0 parents  commit 67e13e8

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
3+
name = "conduit-static"
4+
version = "0.5.0"
5+
authors = ["[email protected]"]
6+
7+
[[lib]]
8+
9+
name = "conduit-static"
10+
11+
[[bin]]
12+
13+
name = "simple-file-server"
14+
path = "examples/simple-file-server.rs"
15+
16+
[dependencies.civet]
17+
18+
git = "https://github.com/wycats/rust-civet.git"
19+
20+
[dependencies.conduit]
21+
22+
git = "https://github.com/conduit-rust/conduit.git"
23+
24+
[dependencies.conduit-test]
25+
26+
git = "https://github.com/conduit-rust/conduit-test.git"

examples/simple-file-server.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate green;
2+
extern crate rustuv;
3+
4+
extern crate civet;
5+
extern crate conduit;
6+
extern crate conduit_static = "conduit-static";
7+
8+
use std::os;
9+
10+
use civet::{Config, Server};
11+
use conduit_static::Static;
12+
13+
fn main() {
14+
let handler = Static::new(os::getcwd());
15+
let _a = Server::start(Config { port: 8888, threads: 50 }, handler);
16+
17+
wait_for_sigint();
18+
}
19+
20+
fn wait_for_sigint() {
21+
use green::{SchedPool, PoolConfig, GreenTaskBuilder};
22+
use std::io::signal::{Listener, Interrupt};
23+
use std::task::TaskBuilder;
24+
25+
let mut config = PoolConfig::new();
26+
config.event_loop_factory = rustuv::event_loop;
27+
28+
let mut pool = SchedPool::new(config);
29+
TaskBuilder::new().green(&mut pool).spawn(proc() {
30+
let mut l = Listener::new();
31+
l.register(Interrupt).unwrap();
32+
l.rx.recv();
33+
});
34+
pool.shutdown();
35+
}

src/conduit-static.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
extern crate conduit;
2+
3+
use std::fmt::Show;
4+
use std::collections::HashMap;
5+
use std::io::fs::File;
6+
use std::io::util::NullReader;
7+
use conduit::{Request, Response, Handler};
8+
9+
pub struct Static {
10+
path: Path
11+
}
12+
13+
impl Static {
14+
pub fn new(path: Path) -> Static {
15+
Static { path: path }
16+
}
17+
}
18+
19+
impl Handler for Static {
20+
fn call(&self, request: &mut Request) -> Result<Response, Box<Show>> {
21+
let request_path = request.path().slice_from(1);
22+
let path = self.path.join(request_path);
23+
24+
if !self.path.is_ancestor_of(&path) {
25+
return Ok(Response {
26+
status: (404, "Not Found"),
27+
headers: HashMap::new(),
28+
body: box NullReader
29+
})
30+
}
31+
32+
let file = try!(File::open(&path).map_err(|e| box e as Box<Show>));
33+
34+
Ok(Response {
35+
status: (200, "OK"),
36+
headers: HashMap::new(),
37+
body: box file as Box<Reader + Send>
38+
})
39+
}
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
extern crate test = "conduit-test";
45+
use conduit;
46+
use conduit::Handler;
47+
use Static;
48+
49+
#[test]
50+
fn test_static() {
51+
let root = Path::new(file!()).dir_path().dir_path();
52+
let handler = Static::new(root);
53+
let mut req = test::MockRequest::new(conduit::Get, "/Cargo.toml");
54+
let mut res = handler.call(&mut req).ok().expect("No response");
55+
let body = res.body.read_to_str().ok().expect("No body");
56+
assert!(body.as_slice().contains("[package]"), "The Cargo.toml was provided");
57+
}
58+
}

0 commit comments

Comments
 (0)