From c41b036e076b01dc3e2562cb701d6152a015d0f4 Mon Sep 17 00:00:00 2001 From: supercmmetry Date: Wed, 24 Jun 2020 15:02:27 +0530 Subject: [PATCH] diesel: embed migrations in binary Signed-off-by: supercmmetry --- Cargo.lock | 1 + Cargo.toml | 1 + src/api/guards/auth.rs | 7 ++----- src/main.rs | 18 ++++++++++++++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87003c8..96c4740 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -601,6 +601,7 @@ dependencies = [ name = "katbin" version = "0.1.0" dependencies = [ + "chrono", "diesel", "diesel_migrations", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index 131d092..0cfc4a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,4 @@ jsonwebtoken = "7.1.2" slog = "2.5.2" slog-term = "2.6.0" slog-async = "2.5.0" +chrono = "0.4.11" diff --git a/src/api/guards/auth.rs b/src/api/guards/auth.rs index 34efd8a..af708c4 100644 --- a/src/api/guards/auth.rs +++ b/src/api/guards/auth.rs @@ -45,12 +45,9 @@ mod date_serializer { } impl Claims { - pub fn new(username: String, access_level: AccessLevel) -> Claims { + pub fn new(username: String) -> Claims { let iat = Utc::now(); - let exp = match access_level { - AccessLevel::Admin => iat + chrono::Duration::minutes(30), - _ => iat + chrono::Duration::days(1) - }; + let exp = iat + chrono::Duration::days(1); Claims { username, diff --git a/src/main.rs b/src/main.rs index c529dee..b84c92c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ extern crate rocket_contrib; #[macro_use] extern crate diesel; #[macro_use] +extern crate diesel_migrations; +#[macro_use] extern crate serde; #[macro_use] extern crate slog; @@ -17,16 +19,28 @@ pub mod utils; use slog_term; use slog_async; -use slog::Drain; +use slog::{Drain, Logger}; + +embed_migrations!("migrations"); + +fn run_migrations(logger: &Logger) { + let result = embedded_migrations::run(&utils::db::pg_connection()); + if let Err(e) = result { + error!(logger, "migration error: {}", e.to_string()); + } +} fn main() { + dotenv::dotenv().ok(); + let decorator = slog_term::TermDecorator::new().build(); let drain = slog_term::FullFormat::new(decorator).build().fuse(); let drain = slog_async::Async::new(drain).build().fuse(); - let logger = slog::Logger::root(drain, o!()); + run_migrations(&logger); + let mut rocket = rocket::ignite(); rocket = api::routes::fuel(rocket);