diff options
author | Till Hoeppner | 2014-11-25 16:04:07 +0100 |
---|---|---|
committer | Till Hoeppner | 2014-11-25 16:04:14 +0100 |
commit | 0e54c6360cb57265c27d10217fbb6eccf6ac734c (patch) | |
tree | f96826e6d1feb771e2438c5b5ab67d1add1c7df5 /src/sersve.rs | |
parent | 8ddcc712b3b353f27e08163ae43e7844079755bc (diff) | |
download | sersve-0e54c6360cb57265c27d10217fbb6eccf6ac734c.tar.gz sersve-0e54c6360cb57265c27d10217fbb6eccf6ac734c.tar.xz sersve-0e54c6360cb57265c27d10217fbb6eccf6ac734c.zip |
Actual functionality. Serves files by path, but no directory view yet.
Diffstat (limited to 'src/sersve.rs')
-rw-r--r-- | src/sersve.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/sersve.rs b/src/sersve.rs new file mode 100644 index 0000000..ad7f001 --- /dev/null +++ b/src/sersve.rs @@ -0,0 +1,32 @@ +#![feature(globs, slicing_syntax)] + +extern crate iron; + +use std::os; +use std::path::{ Path, GenericPath }; +use std::io::fs::{ File, PathExtensions }; + +use iron::prelude::*; +use iron::response::modifiers::*; +use iron::status; + +fn text(desc: &str) -> IronResult<Response> { + Ok(Response::new().set(Status(status::Ok)).set(Body(desc))) +} + +fn serve(req: &mut Request) -> IronResult<Response> { + let mut path = os::getcwd().ok().unwrap(); + for part in req.url.path.iter() { path.push(part[]) } + if !path.exists() { return text("404"); } //Ok(Response::new().set(Status(status::NotFound))); } + + let content = match File::open(&path).read_to_string() { + Ok(s) => s, + Err(e) => return text(e.desc) + }; + + text(content[]) +} + +fn main() { + Iron::new(serve).listen("127.0.0.1:80").unwrap(); +} |