<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use context::{ ContextTrait, Shared };
use iron::{Request, Response, IronResult};
use iron::headers::ContentType;
use iron::status::Status;
use router::Router;
use core::marker::Reflect;


pub fn create<Ctx: Send + Reflect + ContextTrait + 'static> (context: Shared<Ctx>) -> Router {

    let mut router = Router::new();
    let context1 = context.clone();
    router.get("list.json", move |_: &mut Request| -> IronResult<Response> {
        // Build a json representation of the services.
        let ctx = context1.lock().unwrap();
        let serialized = itry!(ctx.services_as_json());

        let mut response = Response::with(serialized);
        response.status = Some(Status::Ok);
        response.headers.set(ContentType::json());

        Ok(response)
    });

    let context2 = context.clone();
    router.get(":service/:command", move |req: &mut Request| -> IronResult<Response> {
        // Call a function on a service.
        let ctx = context2.lock().unwrap();

        let id = req.extensions.get::<Router>().unwrap().find("service").unwrap_or("");
        match ctx.get_service(id) {
            None => {
                let mut response = Response::with(format!("No Such Service: {}", id));
                response.status = Some(Status::BadRequest);
                response.headers.set(ContentType::plaintext());
                Ok(response)
            }
            Some(service) => {
                service.process_request(req)
            }
        }
    });
    router
}


// FIXME: Declare this crate in the test module, and avoid the use of pub
// When this comment was written, putting these 2 lines in before_each returns a compile error:
// ``` error: unresolved import `self::iron_test::request`. Could not find `iron_test` in
// service_router::service_router` [E0432] ```
extern crate iron_test;
pub use self::iron_test::{request, response};

describe! service_router {
    before_each {<commit_msg>Fix compilation error<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use context::{ ContextTrait, Shared };
use iron::{Request, Response, IronResult};
use iron::headers::ContentType;
use iron::status::Status;
use router::Router;
use core::marker::Reflect;


pub fn create<Ctx: Send + Reflect + ContextTrait + 'static> (context: Shared<Ctx>) -> Router {

    let mut router = Router::new();
    let context1 = context.clone();
    router.get("list.json", move |_: &mut Request| -> IronResult<Response> {
        // Build a json representation of the services.
        let ctx = context1.lock().unwrap();
        let serialized = itry!(ctx.services_as_json());

        let mut response = Response::with(serialized);
        response.status = Some(Status::Ok);
        response.headers.set(ContentType::json());

        Ok(response)
    });

    let context2 = context.clone();
    router.get(":service/:command", move |req: &mut Request| -> IronResult<Response> {
        // Call a function on a service.
        let ctx = context2.lock().unwrap();

        let id = req.extensions.get::<Router>().unwrap().find("service").unwrap_or("");
        match ctx.get_service(id) {
            None => {
                let mut response = Response::with(format!("No Such Service: {}", id));
                response.status = Some(Status::BadRequest);
                response.headers.set(ContentType::plaintext());
                Ok(response)
            }
            Some(service) => {
                service.process_request(req)
            }
        }
    });
    router
}


// FIXME: Declare this crate in the test module, and avoid the use of pub
// When this comment was written, putting these 2 lines in before_each returns a compile error:
// ``` error: unresolved import `self::iron_test::request`. Could not find `iron_test` in
// service_router::service_router` [E0432] ```
extern crate iron_test;
pub use self::iron_test::{request, response};

#[cfg(test)]
describe! service_router {
    before_each {
{'fraction_non_alphanumeric': 0.13805024199124222, 'fraction_numerical': 0.005992164093109011, 'mean_word_length': 2.888888888888889, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7019915', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1211, 'n_words': 367}