diesel: embed migrations in binary

Signed-off-by: supercmmetry <vishaals2000@gmail.com>
This commit is contained in:
supercmmetry 2020-06-24 15:02:27 +05:30
parent 22cb897088
commit c41b036e07
No known key found for this signature in database
GPG Key ID: 8E60EF28A328E40D
4 changed files with 20 additions and 7 deletions

1
Cargo.lock generated
View File

@ -601,6 +601,7 @@ dependencies = [
name = "katbin" name = "katbin"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"diesel", "diesel",
"diesel_migrations", "diesel_migrations",
"dotenv", "dotenv",

View File

@ -19,3 +19,4 @@ jsonwebtoken = "7.1.2"
slog = "2.5.2" slog = "2.5.2"
slog-term = "2.6.0" slog-term = "2.6.0"
slog-async = "2.5.0" slog-async = "2.5.0"
chrono = "0.4.11"

View File

@ -45,12 +45,9 @@ mod date_serializer {
} }
impl Claims { impl Claims {
pub fn new(username: String, access_level: AccessLevel) -> Claims { pub fn new(username: String) -> Claims {
let iat = Utc::now(); let iat = Utc::now();
let exp = match access_level { let exp = iat + chrono::Duration::days(1);
AccessLevel::Admin => iat + chrono::Duration::minutes(30),
_ => iat + chrono::Duration::days(1)
};
Claims { Claims {
username, username,

View File

@ -7,6 +7,8 @@ extern crate rocket_contrib;
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
#[macro_use] #[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
extern crate slog; extern crate slog;
@ -17,16 +19,28 @@ pub mod utils;
use slog_term; use slog_term;
use slog_async; 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() { fn main() {
dotenv::dotenv().ok();
let decorator = slog_term::TermDecorator::new().build(); let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::FullFormat::new(decorator).build().fuse(); let drain = slog_term::FullFormat::new(decorator).build().fuse();
let drain = slog_async::Async::new(drain).build().fuse(); let drain = slog_async::Async::new(drain).build().fuse();
let logger = slog::Logger::root(drain, o!()); let logger = slog::Logger::root(drain, o!());
run_migrations(&logger);
let mut rocket = rocket::ignite(); let mut rocket = rocket::ignite();
rocket = api::routes::fuel(rocket); rocket = api::routes::fuel(rocket);