summaryrefslogtreecommitdiffstats
path: root/rust/src/bin/fatcat-iron.rs
blob: d89a9c5369a23f1b8ff1d8ad04d2cb7ff4d38a59 (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
#![allow(missing_docs)]

extern crate chrono;
extern crate clap;
extern crate fatcat;
extern crate fatcat_api;
extern crate futures;
extern crate dotenv;
extern crate diesel;
extern crate iron_diesel_middleware;
extern crate iron;
extern crate iron_slog;
extern crate swagger;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate slog;
extern crate slog_term;
extern crate slog_async;

use slog::{Drain, Logger};
use iron_slog::{LoggerMiddleware, DefaultLogFormatter};
use clap::{App, Arg};
use iron::{Chain, Iron};
use swagger::auth::AllowAllMiddleware;
use dotenv::dotenv;
use std::env;
use iron_diesel_middleware::{DieselMiddleware, DieselPooledConnection, DieselReqExt};

/// 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 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;

    dotenv().ok();
    let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");

    let diesel_middleware: DieselMiddleware<diesel::pg::PgConnection> = DieselMiddleware::new(&database_url).unwrap();

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

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

    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"));

    chain.link_after(fatcat::XClacksOverheadMiddleware);
    //chain.link_before(diesel_middleware);

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