From 6cb7b5affab6b347ecbd716d5281df55d9c83aaf Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 1 Mar 2020 16:55:39 +0100 Subject: [PATCH] Adding diesel dep. Need somewhere to store sessions and tokens. --- Cargo.toml | 6 +++- .../down.sql | 6 ++++ .../up.sql | 36 +++++++++++++++++++ .../2020-03-01-120634_create_users/down.sql | 2 ++ .../2020-03-01-120634_create_users/up.sql | 8 +++++ .../2020-03-01-122646_create_tokens/down.sql | 2 ++ .../2020-03-01-122646_create_tokens/up.sql | 8 +++++ src/main.rs | 9 +++++ src/models.rs | 19 ++++++++++ src/schema.rs | 26 ++++++++++++++ 10 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 migrations/2020-03-01-120634_create_users/down.sql create mode 100644 migrations/2020-03-01-120634_create_users/up.sql create mode 100644 migrations/2020-03-01-122646_create_tokens/down.sql create mode 100644 migrations/2020-03-01-122646_create_tokens/up.sql create mode 100644 src/models.rs create mode 100644 src/schema.rs diff --git a/Cargo.toml b/Cargo.toml index 415b103..90cf9db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,11 @@ edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch="master" } -rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket.git", branch="master", features = ["tera_templates"] } +rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket.git", branch="master", features = ["tera_templates", "diesel_postgres_pool"] } +diesel = { version = "1.4.3", features = ["postgres", "chrono"] } +chrono = { version = "0.4.10", features = ["serde"] } +r2d2 = "0.8.8" +r2d2-diesel = "1.0.0" futures = "0.1.21" ldap3 = "0.6" tokio = "0.1.0" 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/migrations/2020-03-01-120634_create_users/down.sql b/migrations/2020-03-01-120634_create_users/down.sql new file mode 100644 index 0000000..dc3714b --- /dev/null +++ b/migrations/2020-03-01-120634_create_users/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE users; diff --git a/migrations/2020-03-01-120634_create_users/up.sql b/migrations/2020-03-01-120634_create_users/up.sql new file mode 100644 index 0000000..33db209 --- /dev/null +++ b/migrations/2020-03-01-120634_create_users/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(32), + is_active BOOLEAN DEFAULT TRUE, + created_at TIMESTAMPTZ DEFAULT now(), + updated_at TIMESTAMPTZ DEFAULT NULL +); diff --git a/migrations/2020-03-01-122646_create_tokens/down.sql b/migrations/2020-03-01-122646_create_tokens/down.sql new file mode 100644 index 0000000..1fdb181 --- /dev/null +++ b/migrations/2020-03-01-122646_create_tokens/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE tokens; diff --git a/migrations/2020-03-01-122646_create_tokens/up.sql b/migrations/2020-03-01-122646_create_tokens/up.sql new file mode 100644 index 0000000..0bd5464 --- /dev/null +++ b/migrations/2020-03-01-122646_create_tokens/up.sql @@ -0,0 +1,8 @@ +-- Your SQL goes here +CREATE TABLE tokens ( + id SERIAL PRIMARY KEY, + user_id INTEGER REFERENCES users (id), + token VARCHAR(255) UNIQUE, + created_at TIMESTAMPTZ DEFAULT now(), + updated_at TIMESTAMPTZ DEFAULT NULL +); diff --git a/src/main.rs b/src/main.rs index c456560..74c98f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #![deny(warnings)] #![feature(proc_macro_hygiene)] +#[macro_use] extern crate diesel; #[macro_use] extern crate rocket; +#[macro_use] extern crate rocket_contrib; use log::info; use serde_derive::Serialize; @@ -37,6 +39,9 @@ use rocket::response::{ use rocket_contrib::json::Json; use rocket_contrib::templates::Template; +mod schema; +mod models; + #[derive(Debug)] struct BasicAuthentication { pub username: String, @@ -215,10 +220,14 @@ fn routes() -> Vec { ] } +#[database("xeenauth")] +struct AuthDb(diesel::PgConnection); + fn main() { env_logger::init(); rocket::ignite() + .attach(AuthDb::fairing()) .attach(Template::fairing()) .mount("/", routes()) .launch(); diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..cd8d5cf --- /dev/null +++ b/src/models.rs @@ -0,0 +1,19 @@ +use chrono::{ + NaiveDateTime, +}; + +#[derive(Queryable, Debug)] +pub struct User { + pub id: Option, + pub username: String, + pub is_active: bool, + pub created_at: Option, + pub updated_at: Option, +} + +#[derive(Queryable)] +pub struct Token { + pub id: Option, + pub user_id: i32, + pub token: String, +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..b789b3b --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,26 @@ +table! { + tokens (id) { + id -> Int4, + user_id -> Nullable, + token -> Nullable, + created_at -> Nullable, + updated_at -> Nullable, + } +} + +table! { + users (id) { + id -> Int4, + username -> Nullable, + is_active -> Nullable, + created_at -> Nullable, + updated_at -> Nullable, + } +} + +joinable!(tokens -> users (user_id)); + +allow_tables_to_appear_in_same_query!( + tokens, + users, +);