Adding diesel dep.

Need somewhere to store sessions and tokens.
This commit is contained in:
Alex Wright 2020-03-01 16:55:39 +01:00
parent dd21d419bd
commit 6cb7b5affa
10 changed files with 121 additions and 1 deletions

View File

@ -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"

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

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;

View File

@ -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
);

View File

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE tokens;

View File

@ -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
);

View File

@ -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<rocket::Route> {
]
}
#[database("xeenauth")]
struct AuthDb(diesel::PgConnection);
fn main() {
env_logger::init();
rocket::ignite()
.attach(AuthDb::fairing())
.attach(Template::fairing())
.mount("/", routes())
.launch();

19
src/models.rs Normal file
View File

@ -0,0 +1,19 @@
use chrono::{
NaiveDateTime,
};
#[derive(Queryable, Debug)]
pub struct User {
pub id: Option<i32>,
pub username: String,
pub is_active: bool,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
#[derive(Queryable)]
pub struct Token {
pub id: Option<i32>,
pub user_id: i32,
pub token: String,
}

26
src/schema.rs Normal file
View File

@ -0,0 +1,26 @@
table! {
tokens (id) {
id -> Int4,
user_id -> Nullable<Int4>,
token -> Nullable<Varchar>,
created_at -> Nullable<Timestamptz>,
updated_at -> Nullable<Timestamptz>,
}
}
table! {
users (id) {
id -> Int4,
username -> Nullable<Varchar>,
is_active -> Nullable<Bool>,
created_at -> Nullable<Timestamptz>,
updated_at -> Nullable<Timestamptz>,
}
}
joinable!(tokens -> users (user_id));
allow_tables_to_appear_in_same_query!(
tokens,
users,
);