aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9e29a522a8a96790232c0d3ee767857629e109c5 (plain) (blame)
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
#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_codegen;
extern crate dotenv;
extern crate r2d2_diesel;
extern crate r2d2;

use diesel::pg::PgConnection;
use r2d2_diesel::ConnectionManager;
use dotenv::dotenv;
use std::env;

// An alias to the type for a pool of Diesel PostgreSql connections.
type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;

/// Initializes a database pool.
fn init_pool() -> Pool {
    dotenv().ok();

    let config = r2d2::Config::default();
    let dburl = env::var("DATABASE_URL")
        .expect("DATABASE_URL environment variable must be set");

    let manager = ConnectionManager::<PgConnection>::new(dburl);
    r2d2::Pool::new(config, manager).expect("db pool")
}

#[get("/")]
fn index() -> &'static str {
    "Hallo, for faen!"
}

fn main() {
    rocket::ignite()
        .manage(init_pool())
        .mount("/", routes![index])
        .launch();
}