rusty_express 0.3.0

A simple http server library written in Rust and provide Express-alike APIs. We know that Rust is hard and daunting, so we will make sure your server can be easy to use without fear!
Documentation
#![allow(dead_code)]

use std::sync::RwLock;
use super::http::{Request, Response};

lazy_static! {
    static ref CONTEXT: RwLock<Box<ServerContextProvider>> = RwLock::new(Box::new(EmptyContext {}));
}

pub type ServerContextProvider = ContextProvider + Sync + Send + 'static;

pub fn set_context(context: Box<ServerContextProvider>) {
    if let Ok(mut c) = CONTEXT.write() {
        *c = context;
    }
}

pub fn update_context(req: &Box<Request>, resp: &mut Box<Response>) -> Result<(), &'static str> {
    if let Ok(mut c) = CONTEXT.write() {
        return c.update(req, resp);
    }

    Err("Unable to lock and update the context")
}

pub fn process_with_context(req: &Box<Request>, resp: &mut Box<Response>) -> Result<(), &'static str> {
    if let Ok(c) = CONTEXT.read() {
        return c.process(req, resp);
    }

    Err("Unable to lock and process the response with the context")
}

pub trait ContextProvider {
    fn update(&mut self, req: &Box<Request>, resp: &mut Box<Response>) -> Result<(), &'static str>;
    fn process(&self, req: &Box<Request>, resp: &mut Box<Response>) -> Result<(), &'static str>;
}

struct EmptyContext {}

impl ContextProvider for EmptyContext {
    #[inline]
    fn update(&mut self, _req: &Box<Request>, _resp: &mut Box<Response>) -> Result<(), &'static str> { Ok(()) }

    #[inline]
    fn process(&self, _req: &Box<Request>, _resp: &mut Box<Response>) -> Result<(), &'static str> { Ok(()) }
}

impl Clone for EmptyContext {
    #[inline]
    fn clone(&self) -> Self { EmptyContext {} }
}