From 0e54c6360cb57265c27d10217fbb6eccf6ac734c Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Tue, 25 Nov 2014 16:04:07 +0100 Subject: Actual functionality. Serves files by path, but no directory view yet. --- src/sersve.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/sersve.rs (limited to 'src/sersve.rs') 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 { + Ok(Response::new().set(Status(status::Ok)).set(Body(desc))) +} + +fn serve(req: &mut Request) -> IronResult { + 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(); +} -- cgit v1.2.3