aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/bin/fatcatd.rs
blob: 34652105d3d9499dbd3a43a32d26046da8f08f0f (plain)
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
#![allow(missing_docs)]

#[macro_use]
extern crate slog;
#[macro_use]
extern crate hyper;

use clap::{App, Arg};
use fatcat::errors::*;
use fatcat::server::*;
use iron::middleware::AfterMiddleware;
use iron::modifiers::RedirectRaw;
use iron::{status, Chain, Iron, IronResult, Request, Response};
use iron_slog::{DefaultLogFormatter, LoggerMiddleware};
use slog::{Drain, Logger};

// HTTP header middleware
header! { (XClacksOverhead, "X-Clacks-Overhead") => [String] }

pub struct XClacksOverheadMiddleware;

impl AfterMiddleware for XClacksOverheadMiddleware {
    fn after(&self, _req: &mut Request, mut res: Response) -> iron::IronResult<Response> {
        res.headers
            .set(XClacksOverhead("GNU aaronsw, jpb".to_owned()));
        Ok(res)
    }
}

/// Create custom server, wire it to the autogenerated router,
/// and pass it to the web server.
fn main() -> Result<()> {
    let matches = App::new("server")
        .arg(
            Arg::with_name("https")
                .long("https")
                .help("Whether to use HTTPS or not"),
        )
        .get_matches();

    let decorator = slog_term::TermDecorator::new().build();
    let drain = slog_term::CompactFormat::new(decorator).build().fuse();
    let drain = slog_async::Async::new(drain).build().fuse();
    let logger = Logger::root(drain, o!());
    let formatter = DefaultLogFormatter;

    let server = create_server()?;
    info!(
        logger,
        "using primary auth key: {}", server.auth_confectionary.identifier,
    );
    info!(
        logger,
        "all auth keys: {:?}",
        server
            .auth_confectionary
            .root_keys
            .keys()
            .collect::<Vec<&String>>(),
    );
    let mut router = fatcat_api_spec::router(server);

    router.get("/", root_handler, "root-redirect");
    router.get("/swagger-ui", swaggerui_handler, "swagger-ui-html");
    router.get("/v0/openapi2.yml", yaml_handler, "openapi2-spec-yaml");

    fn root_handler(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((
            status::Found,
            RedirectRaw("/swagger-ui".to_string()),
        )))
    }
    fn swaggerui_handler(_: &mut Request) -> IronResult<Response> {
        let html_type = "text/html".parse::<iron::mime::Mime>().unwrap();
        Ok(Response::with((
            html_type,
            status::Ok,
            include_str!("../../swagger-ui/index.html"),
        )))
    }
    fn yaml_handler(_: &mut Request) -> IronResult<Response> {
        Ok(Response::with((
            status::Ok,
            include_str!("../../../fatcat-openapi2.yml"),
        )))
    }

    let host_port = "localhost:9411";
    info!(
        logger,
        "Starting fatcatd API server on http://{}", &host_port
    );

    let mut chain = Chain::new(LoggerMiddleware::new(router, logger, formatter));

    // authentication
    chain.link_before(fatcat_api_spec::server::ExtractAuthData);
    chain.link_before(fatcat::auth::MacaroonAuthMiddleware::new());

    chain.link_after(XClacksOverheadMiddleware);

    if matches.is_present("https") {
        unimplemented!()
    } else {
        // Using HTTP
        Iron::new(chain)
            .http(host_port)
            .expect("failed to start HTTP server");
    }
    Ok(())
}