diesel: Set up initial migrations

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-06-24 21:53:48 +05:30
parent c41b036e07
commit 2a1034c70f
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C
5 changed files with 50 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
.idea
.env

View File

@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();

View File

@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.
-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

View File

@ -13,7 +13,7 @@ use std::io::Cursor;
#[derive(Serialize, Deserialize, Clone)]
pub struct Claims {
pub username: String,
pub user_id: String,
#[serde(with = "date_serializer")]
iat: DateTime<Utc>,
#[serde(with = "date_serializer")]
@ -23,6 +23,7 @@ pub struct Claims {
mod date_serializer {
use chrono::{DateTime, TimeZone, Utc};
use serde::{self, Deserialize, Deserializer, Serializer};
use serde::de::Error;
/// Serializes a DateTime<Utc> to a Unix timestamp (milliseconds since 1970/1/1T00:00:00T)
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
@ -40,17 +41,17 @@ mod date_serializer {
{
Utc.timestamp_opt(i64::deserialize(deserializer)?, 0)
.single() // If there are multiple or no valid DateTimes from timestamp, return None
.ok_or_else(|| serde::de::Error::custom("invalid Unix timestamp value"))
.ok_or_else(|| Error::custom("invalid Unix timestamp value"))
}
}
impl Claims {
pub fn new(username: String) -> Claims {
pub fn new(user_id: String) -> Claims {
let iat = Utc::now();
let exp = iat + chrono::Duration::days(1);
Claims {
username,
user_id,
iat: iat.date().and_hms_milli(iat.hour(), iat.minute(), iat.second(), 0),
exp: exp.date().and_hms_milli(exp.hour(), exp.minute(), exp.second(), 0)
}

View File

@ -1,11 +1,11 @@
use rocket::Rocket;
#[get("/check")]
#[get("/health")]
fn check() -> &'static str {
"OK"
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/health", routes![check])
rocket.mount("/api", routes![check])
}