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. --- Cargo.toml | 4 ++++ src/lib.rs | 3 --- src/sersve.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) delete mode 100644 src/lib.rs create mode 100644 src/sersve.rs diff --git a/Cargo.toml b/Cargo.toml index ef3bd1a..e9f0d80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,9 @@ name = "sersve" version = "0.0.1" authors = ["Till Hoeppner "] +[[bin]] +name = "sersve" +path = "src/sersve.rs" + [dependencies] iron = "*" diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index a93251b..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -#[test] -fn it_works() { -} 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