summaryrefslogtreecommitdiffstats
path: root/rust/src/bin/fatcat-iron.rs
blob: 8c87f55fbba97606a100dd0e2f9a2f6e7a447f42 (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
//! Main binary entry point for fatcat implementation.

#![allow(missing_docs)]

// Imports required by this file.
extern crate fatcat;
extern crate fatcat_api;
extern crate swagger;
extern crate iron;
//extern crate hyper_openssl;
extern crate clap;

// Imports required by server library.
// extern crate fatcat;
// extern crate swagger;
extern crate futures;
extern crate chrono;
#[macro_use]
extern crate error_chain;

//use hyper_openssl::OpensslServer;
//use hyper_openssl::openssl::x509::X509_FILETYPE_PEM;
//use hyper_openssl::openssl::ssl::{SslAcceptorBuilder, SslMethod};
//use hyper_openssl::openssl::error::ErrorStack;
use clap::{App, Arg};
use iron::{Iron, Chain};
use swagger::auth::AllowAllMiddleware;

/*
/// Builds an SSL implementation for Simple HTTPS from some hard-coded file names
fn ssl() -> Result<OpensslServer, ErrorStack> {
    let mut ssl = SslAcceptorBuilder::mozilla_intermediate_raw(SslMethod::tls())?;

    // Server authentication
    ssl.set_private_key_file("examples/server-key.pem", X509_FILETYPE_PEM)?;
    ssl.set_certificate_chain_file("examples/server-chain.pem")?;
    ssl.check_private_key()?;

    Ok(OpensslServer::from(ssl.build()))
}
*/

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

    let server = fatcat::server().unwrap();
    let router = fatcat_api::router(server);

    let mut chain = Chain::new(router);
    chain.link_before(fatcat_api::server::ExtractAuthData);
    // add authentication middlewares into the chain here
    // for the purpose of this example, pretend we have authenticated a user
    chain.link_before(AllowAllMiddleware::new("cosmo"));

    if matches.is_present("https") {
        unimplemented!()
        // Using Simple HTTPS
        //Iron::new(chain).https("localhost:8080", ssl().expect("Failed to load SSL keys")).expect("Failed to start HTTPS server");
    } else {
        // Using HTTP
        Iron::new(chain).http("localhost:8080").expect("Failed to start HTTP server");
    }
}