From 2a1034c70f3e58701a6a6535f504453dafa0788f Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Wed, 24 Jun 2020 21:53:48 +0530 Subject: [PATCH] diesel: Set up initial migrations Signed-off-by: ATechnoHazard --- .gitignore | 1 + .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ src/api/guards/auth.rs | 9 ++--- src/api/routes/health.rs | 4 +-- 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 migrations/00000000000000_diesel_initial_setup/up.sql diff --git a/.gitignore b/.gitignore index 3a8cabc..43795be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .idea +.env \ No newline at end of file diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -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(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -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; diff --git a/src/api/guards/auth.rs b/src/api/guards/auth.rs index af708c4..6a0f1f7 100644 --- a/src/api/guards/auth.rs +++ b/src/api/guards/auth.rs @@ -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, #[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 to a Unix timestamp (milliseconds since 1970/1/1T00:00:00T) pub fn serialize(date: &DateTime, serializer: S) -> Result @@ -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) } diff --git a/src/api/routes/health.rs b/src/api/routes/health.rs index 0201b31..c264b98 100644 --- a/src/api/routes/health.rs +++ b/src/api/routes/health.rs @@ -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]) } \ No newline at end of file