aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-10-01 22:01:02 +0200
committerHarald Eilertsen <haraldei@anduin.net>2017-10-01 22:01:02 +0200
commite44bd988930babed7de59d78ad570b1b0a6e1cf0 (patch)
tree665c78cd7a9c82c5cdafb10c23a8146736997301
parent5fbde7064c26a00eb03bdd077a99b11362f7080f (diff)
downloadrocket-blog-e44bd988930babed7de59d78ad570b1b0a6e1cf0.tar.gz
rocket-blog-e44bd988930babed7de59d78ad570b1b0a6e1cf0.tar.bz2
rocket-blog-e44bd988930babed7de59d78ad570b1b0a6e1cf0.zip
Connect to database pool when igniting.
-rw-r--r--src/main.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 03966fc..9e29a52 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,14 +2,42 @@
#![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 {
- "Hello, for faen!"
+ "Hallo, for faen!"
}
fn main() {
rocket::ignite()
+ .manage(init_pool())
.mount("/", routes![index])
.launch();
}