Completely restructured codebase

develop
Jef Roosens 2021-08-22 16:24:59 +02:00
parent b45c93cdc9
commit b13b760e2f
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
16 changed files with 67 additions and 60 deletions

View File

@ -6,11 +6,11 @@ edition = "2018"
[lib]
name = "rb"
path = "src/rb/lib.rs"
path = "src/lib.rs"
[[bin]]
name = "rbs"
path = "src/rbs/main.rs"
name = "rbd"
path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -2,4 +2,4 @@
# see diesel.rs/guides/configuring-diesel-cli
[print_schema]
file = "src/rb/schema.rs"
file = "src/schema.rs"

View File

@ -1,5 +1,8 @@
use crate::errors::RBError;
use crate::models::{NewRefreshToken, NewUser, RefreshToken, User};
use crate::db::{
users::{User, NewUser},
tokens::{RefreshToken, NewRefreshToken}
};
use crate::schema::refresh_tokens::dsl as refresh_tokens;
use crate::schema::users::dsl as users;
use argon2::verify_encoded;

2
src/db/mod.rs 100644
View File

@ -0,0 +1,2 @@
pub mod users;
pub mod tokens;

20
src/db/tokens.rs 100644
View File

@ -0,0 +1,20 @@
use uuid::Uuid;
use diesel::{Queryable, Insertable};
use crate::schema::refresh_tokens;
#[derive(Queryable)]
pub struct RefreshToken {
pub token: Vec<u8>,
pub user_id: Uuid,
pub expires_at: chrono::NaiveDateTime,
pub last_used_at: Option<chrono::NaiveDateTime>,
}
#[derive(Insertable)]
#[table_name = "refresh_tokens"]
pub struct NewRefreshToken {
pub token: Vec<u8>,
pub user_id: Uuid,
pub expires_at: chrono::NaiveDateTime,
}

29
src/db/users.rs 100644
View File

@ -0,0 +1,29 @@
use crate::schema::users;
use diesel::{AsChangeset, Insertable, Queryable, prelude::*};
use serde::Serialize;
use uuid::Uuid;
use crate::schema::users::dsl::*;
use crate::errors::RBError;
#[derive(Queryable, Serialize)]
pub struct User {
pub id: Uuid,
pub username: String,
#[serde(skip_serializing)]
pub password: String,
#[serde(skip_serializing)]
pub blocked: bool,
pub admin: bool,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "users"]
pub struct NewUser {
pub username: String,
pub password: String,
pub admin: bool,
}
pub fn all(conn: &PgConnection) -> crate::Result<Vec<User>> {
users.load::<User>(conn).map_err(|_| RBError::DBError)
}

View File

@ -1,10 +1,9 @@
#[macro_use]
extern crate diesel;
pub mod db;
pub mod auth;
pub mod errors;
pub mod admin;
pub mod models;
pub(crate) mod schema;
pub use errors::Result;

View File

@ -10,9 +10,8 @@ extern crate diesel_migrations;
use rocket::{fairing::AdHoc, Build, Rocket};
use rocket_sync_db_pools::{database, diesel};
mod auth;
mod admin;
pub(crate) mod guards;
mod routes;
embed_migrations!();
@ -55,6 +54,6 @@ fn rocket() -> _ {
run_db_migrations,
))
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
.mount("/api/auth", auth::routes())
.mount("/api/admin", admin::routes())
.mount("/api/auth", routes::auth::routes())
.mount("/api/admin", routes::admin::routes())
}

View File

@ -1,8 +0,0 @@
use crate::errors::RBError;
use crate::models::User;
use diesel::prelude::*;
use crate::schema::users::dsl as users;
pub fn get_users(conn: &PgConnection) -> crate::Result<Vec<User>> {
users::users.load::<User>(conn).map_err(|_| RBError::DBError)
}

View File

@ -1,39 +0,0 @@
use crate::schema::{refresh_tokens, users};
use diesel::{AsChangeset, Insertable, Queryable};
use serde::Serialize;
use uuid::Uuid;
#[derive(Queryable, Serialize)]
pub struct User {
pub id: Uuid,
pub username: String,
#[serde(skip_serializing)]
pub password: String,
#[serde(skip_serializing)]
pub blocked: bool,
pub admin: bool,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "users"]
pub struct NewUser {
pub username: String,
pub password: String,
pub admin: bool,
}
#[derive(Queryable)]
pub struct RefreshToken {
pub token: Vec<u8>,
pub user_id: Uuid,
pub expires_at: chrono::NaiveDateTime,
pub last_used_at: Option<chrono::NaiveDateTime>,
}
#[derive(Insertable)]
#[table_name = "refresh_tokens"]
pub struct NewRefreshToken {
pub token: Vec<u8>,
pub user_id: Uuid,
pub expires_at: chrono::NaiveDateTime,
}

View File

@ -1,6 +1,6 @@
use crate::guards::Admin;
use crate::RbDbConn;
use rb::models::User;
use rb::db::users::User;
use rocket::serde::json::Json;
pub fn routes() -> Vec<rocket::Route> {
@ -9,5 +9,5 @@ pub fn routes() -> Vec<rocket::Route> {
#[get("/users")]
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> {
Ok(Json(conn.run(|c| rb::admin::get_users(c)).await?))
Ok(Json(conn.run(|c| rb::db::users::all(c)).await?))
}

View File

@ -0,0 +1,2 @@
pub mod auth;
pub mod admin;