1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
pub extern crate futures;
pub extern crate hyper;
extern crate url;

use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;

use futures::future::{err, FutureResult, ok};
use hyper::{Method, Request, Response, StatusCode};
use hyper::server::{Http, Service};
use url::Url;

macro_rules! ftry {
    ($exp: expr) => {
        match $exp {
            Ok(exp) => exp,
            Err(e) => return err(Io(Error::new(ErrorKind::Other, e))),
        }
    }
}

macro_rules! ftry_opt {
    ($exp: expr) => {
        match $exp {
            Some(exp) => exp,
            None => continue,
        }
    }
}

pub struct Blink {
    routes: HashMap<(Method, String), Box<Route<Method=Method>>>,
    port: u64,
}

impl Blink {
    pub fn new() -> Self {
        Self {
            routes: HashMap::new(),
            port: 6767,
        }
    }

    pub fn routes(mut self, routes: HashMap<(Method, String), Box<Route<Method=Method>>>) -> Self {
        self.routes = routes;
        self
    }

    pub fn port(mut self, port_num: u64) -> Self {
        self.port = port_num;
        self
    }

    pub fn run(self) -> Result<(), hyper::Error> {
        let address_str = "127.0.0.1:".to_string() + &self.port.to_string();
        let address: SocketAddr = address_str.parse().unwrap();
        println!("Running server on {}", address);
        Http::new()
            .bind(&address, move || Ok(&self))?
            .run()?;
        Ok(())
    }
}

impl<'b> Service for &'b Blink {
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    type Future = FutureResult<Response, hyper::Error>;

    fn call(&self, req: Request) -> Self::Future {
        let path = req.path().to_owned();
        let method = req.method().to_owned();
        match self.routes.get(&(method, path)) {
            Some(route) => {
                route.handler(req)
            }
            None => {
                // We couldn't find the url in our list of routes. This means that either:
                // - It has url parameters
                // - It doesn't match at all
                // We first check if it's a possible url parameter match and if it is call the
                // correct hanlder, otherwise we return a 404.
                use hyper::Error::Io;
                let base = ftry!(Url::parse("https://localhost"));
                for &(ref method, ref url) in self.routes.keys() {
                    // The methods don't match so skip them
                    if method != req.method() {
                        continue;
                    }
                    let url_in = ftry!(base.clone().join(&url));
                    let test = ftry!(base.clone().join(&req.path()));
                    let mut url_in = ftry_opt!(url_in.path_segments());
                    let test = ftry_opt!(test.path_segments());

                    let size1 = url_in.clone().count();
                    let size2 = test.clone().count();

                    if size1 != size2 {
                        continue;
                    }

                    let mut matched = true;
                    for (i, j) in url_in.zip(test) {
                        if i.starts_with(':') {
                            continue;
                        }

                        if i != j {
                            matched = false;
                            break;
                        }
                    }

                    if matched {
                        return self.routes.get(&(method.to_owned(), url.to_owned())).unwrap().handler(req);
                    }
                }
                ok(Response::new().with_status(StatusCode::NotFound))
            }
        }
    }
}

pub trait Route {
    type Method;
    fn handler(&self, _: Request) -> FutureResult<Response, hyper::Error>;
    fn method(&self) -> Self::Method;
}

#[macro_export]
macro_rules! router {
    ($( ($route: expr, $handler: expr))*) => {
        {
            use std::collections::HashMap;
            let mut map = HashMap::new();
            $(
                let boxed: Box<Route<Method = hyper::Method>> = Box::new($handler);
                map.insert((boxed.method(), $route.to_string()), boxed);
            )*
            map
        }
    };
}

#[macro_export]
macro_rules! routes {
    ($(($method: ident, $type: ident, $function: expr))*) => {
        use hyper::server::{ Request, Response };
        use futures::future::FutureResult;
        $(
        struct $type;
        impl Route for $type {
            type Method = hyper::Method;
            fn handler(&self, _req: Request) -> FutureResult<Response, hyper::Error> {
                #[inline(always)]
                $function(_req)
            }
            #[inline(always)]
            fn method(&self) -> Self::Method {
                hyper::Method::$method
            }
        }
        )*
    }
}