#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
#[macro_use]
extern crate serde_json;

#[macro_export]
macro_rules! get_pg_connection {
    ($db_context:expr) => (&*$db_context.postgres_connection_pool.get().expect("Failed to get connection from postgres pool"));
}

extern crate iron;
extern crate bodyparser;
extern crate serde_cbor;
extern crate base64;
extern crate crypto;
extern crate unicase;
extern crate chrono;
extern crate env_logger;
extern crate r2d2;
extern crate r2d2_diesel;
extern crate r2d2_redis;
extern crate redis;
extern crate rand;
extern crate serde;

use std::env;
use iron::Iron;

pub mod common {
    pub type Pubkey = Vec<u8>;
}

pub mod db;
mod api;


fn get_server_port() -> u16 {
    let port_str = env::var("PORT").unwrap_or(String::new());
    port_str.parse().unwrap_or(8080)
}

pub fn start() {
    env_logger::init().unwrap();
    info!("starting spotlease...");

    let api = api::ApiHandler::new();
    Iron::new(api).http(("0.0.0.0", get_server_port())).unwrap();
}