forked from Chewing_Bever/rusty-bever
Completely restructured codebase
parent
b45c93cdc9
commit
b13b760e2f
|
@ -6,11 +6,11 @@ edition = "2018"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "rb"
|
name = "rb"
|
||||||
path = "src/rb/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "rbs"
|
name = "rbd"
|
||||||
path = "src/rbs/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|
|
@ -2,4 +2,4 @@
|
||||||
# see diesel.rs/guides/configuring-diesel-cli
|
# see diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
[print_schema]
|
[print_schema]
|
||||||
file = "src/rb/schema.rs"
|
file = "src/schema.rs"
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
use crate::errors::RBError;
|
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::refresh_tokens::dsl as refresh_tokens;
|
||||||
use crate::schema::users::dsl as users;
|
use crate::schema::users::dsl as users;
|
||||||
use argon2::verify_encoded;
|
use argon2::verify_encoded;
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod users;
|
||||||
|
pub mod tokens;
|
|
@ -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,
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -1,10 +1,9 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
|
||||||
|
pub mod db;
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
pub mod admin;
|
|
||||||
pub mod models;
|
|
||||||
pub(crate) mod schema;
|
pub(crate) mod schema;
|
||||||
|
|
||||||
pub use errors::Result;
|
pub use errors::Result;
|
|
@ -10,9 +10,8 @@ extern crate diesel_migrations;
|
||||||
use rocket::{fairing::AdHoc, Build, Rocket};
|
use rocket::{fairing::AdHoc, Build, Rocket};
|
||||||
use rocket_sync_db_pools::{database, diesel};
|
use rocket_sync_db_pools::{database, diesel};
|
||||||
|
|
||||||
mod auth;
|
|
||||||
mod admin;
|
|
||||||
pub(crate) mod guards;
|
pub(crate) mod guards;
|
||||||
|
mod routes;
|
||||||
|
|
||||||
embed_migrations!();
|
embed_migrations!();
|
||||||
|
|
||||||
|
@ -55,6 +54,6 @@ fn rocket() -> _ {
|
||||||
run_db_migrations,
|
run_db_migrations,
|
||||||
))
|
))
|
||||||
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
||||||
.mount("/api/auth", auth::routes())
|
.mount("/api/auth", routes::auth::routes())
|
||||||
.mount("/api/admin", admin::routes())
|
.mount("/api/admin", routes::admin::routes())
|
||||||
}
|
}
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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,
|
|
||||||
}
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::guards::Admin;
|
use crate::guards::Admin;
|
||||||
use crate::RbDbConn;
|
use crate::RbDbConn;
|
||||||
use rb::models::User;
|
use rb::db::users::User;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
pub fn routes() -> Vec<rocket::Route> {
|
pub fn routes() -> Vec<rocket::Route> {
|
||||||
|
@ -9,5 +9,5 @@ pub fn routes() -> Vec<rocket::Route> {
|
||||||
|
|
||||||
#[get("/users")]
|
#[get("/users")]
|
||||||
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> {
|
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?))
|
||||||
}
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod auth;
|
||||||
|
pub mod admin;
|
Loading…
Reference in New Issue