From a1d799a3f28cf0c50a7fcb8a91e17dac899c2f4d Mon Sep 17 00:00:00 2001 From: Pirmin Kalberer Date: Wed, 12 Jun 2019 23:03:20 +0200 Subject: [PATCH] Read SQLite path from .env and fix DATABASE_URL --- diesel/.env | 2 +- diesel/README.md | 2 +- diesel/src/main.rs | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/diesel/.env b/diesel/.env index 1fbc5af7..186be7ce 100644 --- a/diesel/.env +++ b/diesel/.env @@ -1 +1 @@ -DATABASE_URL=file:test.db +DATABASE_URL=test.db diff --git a/diesel/README.md b/diesel/README.md index a989377e..a0143c38 100644 --- a/diesel/README.md +++ b/diesel/README.md @@ -9,7 +9,7 @@ Diesel's `Getting Started` guide using SQLite for Actix web ```bash cargo install diesel_cli --no-default-features --features sqlite cd examples/diesel -echo "DATABASE_URL=file:test.db" > .env +echo "DATABASE_URL=test.db" > .env diesel migration run ``` diff --git a/diesel/src/main.rs b/diesel/src/main.rs index 80dbc8b4..36453495 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -13,6 +13,7 @@ use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer}; use bytes::BytesMut; use diesel::prelude::*; use diesel::r2d2::{self, ConnectionManager}; +use dotenv; use futures::future::{err, Either}; use futures::{Future, Stream}; @@ -120,8 +121,10 @@ fn main() -> std::io::Result<()> { std::env::set_var("RUST_LOG", "actix_web=info"); env_logger::init(); - // Start 3 db executor actors - let manager = ConnectionManager::::new("test.db"); + dotenv::dotenv().ok(); + + let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); + let manager = ConnectionManager::::new(connspec); let pool = r2d2::Pool::builder() .build(manager) .expect("Failed to create pool.");