| 1 | 1 |
extern crate hyper;
|
| 2 |
|
| 3 |
mod error;
|
| 4 |
mod helper;
|
| 5 |
mod matching;
|
| 2 | 6 |
|
| 3 | 7 |
use hyper::Server;
|
| 4 | 8 |
use hyper::server::{Handler,Request,Response};
|
| 5 | 9 |
use hyper::net::Fresh;
|
| 6 | 10 |
|
| 7 | 11 |
use std::env::{args,current_dir,set_current_dir};
|
| 8 | |
use std::fs::{File,read_dir};
|
| 9 | |
use std::io::prelude::Read;
|
| 12 |
use std::fs::{read_dir};
|
| 10 | 13 |
use std::iter::Iterator;
|
| 11 | 14 |
use std::path::Path;
|
| 12 | 15 |
|
| 13 | |
mod matching;
|
| 14 | |
|
| 15 | |
#[derive(Debug)]
|
| 16 | |
enum AloysError {
|
| 17 | |
IOError(std::io::Error),
|
| 18 | |
HyperError(hyper::error::Error),
|
| 19 | |
}
|
| 20 | |
|
| 21 | |
impl From<std::io::Error> for AloysError {
|
| 22 | |
fn from(e: std::io::Error) -> AloysError {
|
| 23 | |
AloysError::IOError(e)
|
| 24 | |
}
|
| 25 | |
}
|
| 26 | |
|
| 27 | |
impl From<hyper::error::Error> for AloysError {
|
| 28 | |
fn from(e: hyper::error::Error) -> AloysError {
|
| 29 | |
AloysError::HyperError(e)
|
| 30 | |
}
|
| 31 | |
}
|
| 16 |
use error::{AloysError,aloys_error};
|
| 17 |
use helper::with_file_contents;
|
| 32 | 18 |
|
| 33 | 19 |
#[derive(Debug, Clone)]
|
| 34 | 20 |
enum Delegate {
|
|
| 54 | 40 |
routes: Vec<RouteSpec>,
|
| 55 | 41 |
}
|
| 56 | 42 |
|
| 57 | |
fn with_file_contents<A, F>(path: &Path, default: A, callback: F) -> A
|
| 58 | |
where F: FnOnce(String) -> A + 'static {
|
| 59 | |
match File::open(path) {
|
| 60 | |
Ok(mut f) => {
|
| 61 | |
let mut s = String::new();
|
| 62 | |
match f.read_to_string(&mut s) {
|
| 63 | |
Ok(_) => callback(s),
|
| 64 | |
Err(_) => default,
|
| 65 | |
}
|
| 66 | |
},
|
| 67 | |
Err(_) => default,
|
| 68 | |
}
|
| 69 | |
}
|
| 70 | |
|
| 71 | |
impl RouteSpec {
|
| 72 | |
fn from_path(dir: &Path) -> Result<RouteSpec,AloysError> {
|
| 73 | |
let path = with_file_contents(&dir.join("path"), None, |s| Some(s));
|
| 74 | |
let domain = with_file_contents(&dir.join("domain"), None, |s| Some(s));
|
| 75 | |
let mode = with_file_contents(&dir.join("mode"), "http", |s| s);
|
| 76 | |
if mode == "http" {
|
| 77 | |
} else if mode == "alyos" {
|
| 78 | |
let fwd = with_file_contents(&dir.join("conf"),
|
| 79 | |
Path::new(&"/dev/null"),
|
| 80 | |
|s| Path::new(&s));
|
| 81 | |
let rts = try!(Routes::load_paths(fwd));
|
| 82 | |
Ok(RouteSpec { for_path: path,
|
| 83 | |
for_domain: domain,
|
| 84 | |
delegate_to: Delegate::
|
| 85 | |
} else {
|
| 86 | |
|
| 87 | |
}
|
| 88 | |
// let forPath = match File::open(dir.join("path")) {
|
| 89 | |
// Ok(f) => {};
|
| 90 | |
// Err
|
| 91 | |
// };
|
| 92 | |
Ok(RouteSpec { for_path: None,
|
| 93 | |
for_domain: None,
|
| 94 | |
delegate_to: Delegate::Defer(Routes { routes: vec![] }),
|
| 95 | |
})
|
| 96 | |
}
|
| 97 | |
}
|
| 98 | |
|
| 99 | 43 |
impl Routes {
|
| 100 | 44 |
fn load_paths(dir: &Path) -> Result<Routes,AloysError> {
|
| 101 | 45 |
let mut routes = vec![];
|
|
| 117 | 61 |
|
| 118 | 62 |
fn find_delegate(&self, req: &Request) -> Option<&Delegate> {
|
| 119 | 63 |
for d in self.routes.iter() {
|
| 64 |
let mut matches = true;
|
| 120 | 65 |
if let Some(ref d) = d.for_domain {
|
| 121 | 66 |
println!("Matching against {:?}", d);
|
| 122 | 67 |
}
|
|
| 132 | 77 |
impl Handler for Routes {
|
| 133 | 78 |
fn handle(&self, req: Request, res: Response<Fresh>) {
|
| 134 | 79 |
self.dispatch(&req, res);
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
impl RouteSpec {
|
| 84 |
fn from_path(dir: &Path) -> Result<RouteSpec,AloysError> {
|
| 85 |
let path = with_file_contents(&dir.join("path"), None, |s| Some(s));
|
| 86 |
let domain = with_file_contents(&dir.join("domain"), None, |s| Some(s));
|
| 87 |
let mode = with_file_contents(&dir.join("mode"), "http".to_string(), |s| s);
|
| 88 |
if mode == "http" {
|
| 89 |
aloys_error("Unimplemented".to_string())
|
| 90 |
} else if mode == "alyos" {
|
| 91 |
let fwd = with_file_contents(&dir.join("conf"),
|
| 92 |
"/dev/null".to_string(),
|
| 93 |
|s| s);
|
| 94 |
let rts = try!(Routes::load_paths(Path::new(&fwd)));
|
| 95 |
Ok(RouteSpec { for_path: path,
|
| 96 |
for_domain: domain,
|
| 97 |
delegate_to: Delegate::Defer(rts) })
|
| 98 |
} else {
|
| 99 |
aloys_error(format!("Unknown mode `{:?}` in `{:?}", mode, dir))
|
| 100 |
}
|
| 135 | 101 |
}
|
| 136 | 102 |
}
|
| 137 | 103 |
|