Added documentation and refactored

This commit is contained in:
Johannes Randerath
2024-08-30 11:38:40 +02:00
parent d422a4b31c
commit fe04cf539e
10 changed files with 24 additions and 91 deletions

View File

@@ -25,7 +25,7 @@ use rocket::http::{Status, Cookie, CookieJar, SameSite};
use inventur_db;
use rocket_oauth2::{OAuth2, TokenResponse};
use reqwest::Client;
use rocket::serde::{Deserialize, json::Json};
use rocket::serde::Deserialize;
/// OAuth provider
pub struct RanderathIdentity;
@@ -101,7 +101,7 @@ pub async fn redirect_to_login_401() -> Redirect {
/// Unauthorized requests are sent to the oauth provider in order for the user to authenticate.
#[catch(403)]
pub async fn redirect_to_login() -> Redirect {
pub async fn redirect_to_login_403() -> Redirect {
Redirect::to(uri!(oauth_login()))
}
@@ -116,7 +116,6 @@ pub fn oauth_login(oauth2: OAuth2<RanderathIdentity>, cookies: &CookieJar<'_>) -
#[get("/auth")]
pub async fn oauth_callback(conn: Db, token: TokenResponse<RanderathIdentity>, cookies: &CookieJar<'_>) -> Result<Redirect, Status> {
let at = token.access_token().to_string();
let tv = token.as_value();
cookies.add_private(
Cookie::build(("token", at.to_string()))
.same_site(SameSite::Lax)

View File

@@ -30,16 +30,14 @@ use auth::AuthUser;
use rocket::fs::{FileServer, relative};
use rocket_dyn_templates::{Template, context};
use rocket::request;
use rocket::response::Redirect;
use rocket::http::{Status, Cookie, CookieJar, SameSite};
use inventur_db;
use rocket_sync_db_pools::{database, diesel};
use rocket_oauth2::OAuth2;
use std::env;
use dotenvy::dotenv;
use rocket::Config;
use rocket::figment::providers::{Toml, Env, Format};
use rocket::figment::providers::Env;
/// Database connection using diesel and rocket_sync_db_pools
#[database("inventur")]
@@ -84,33 +82,32 @@ async fn favicon() -> Redirect {
Redirect::to(uri!("/img/favicon.ico"))
}
/// Setup app for launch:
/// Load configuration from a file called .env in the project's root.
/// Use tera templates, connect to mysql db, setup oauth
/// Serve everything related to ...
/// ... home page and login under /
/// ... display of a table, and the manipulation of the table object under /table
/// ... manipulation of table rows under /row
/// ... manipulation of columns under /column
/// ... requests not logged in to the oauth provider
/// Setup a fileserver to serve static files from the static directory in the file type's directory
#[launch]
async fn rocket() -> _ {
/// Load configuration from a file called .env in the project's root.
dotenv().ok();
let cfg = Config::figment()
.merge(Env::prefixed("ROCKET_"));
rocket::custom(cfg)
/// Use tera templates
.attach(Template::fairing())
/// Connect to mysql db
.attach(Db::fairing())
/// Set up oauth
.attach(OAuth2::<auth::RanderathIdentity>::fairing("oauth"))
/// Everything related to the home page and login
.mount("/", routes![auth::oauth_login, auth::oauth_callback, home, login_home, favicon])
/// Everything related to the table view and modifying the table as an object (as opposed to its rows and columns).
.mount("/table", routes![table::table, table::table_sec, table::edit_tname, table::create_table, table::import_table, table::delete_table])
/// Modify table rows and their contents
.mount("/row", routes![table::new_entry, table::edit_entry, table::delete_entry])
/// Modify the table's columns, their names and types.
.mount("/column", routes![table::delete_column, table::edit_column])
/// If not logged in, redirect to oauth login
.register("/", catchers![auth::redirect_to_login])
/// Serve static files in the corresponding subdirs of /static.
.mount("/column", routes![table::delete_column, table::edit_column, table::create_column])
.register("/", catchers![auth::redirect_to_login_401, auth::redirect_to_login_403])
.mount("/img", FileServer::from(relative!("static/img")))
.mount("/css", FileServer::from(relative!("static/css")))
.mount("/js", FileServer::from(relative!("static/js")))

View File

@@ -33,5 +33,4 @@ pub use self::table_view::*;
pub use self::table_manipulate_table::*;
pub use self::table_manipulate_entry::*;
pub use self::table_manipulate_column::*;
pub use self::forms::*;

View File

@@ -18,8 +18,7 @@
//! Submodule holding structs relevant to handle form data.
use rocket::form::Form;
use rocket::serde::{Serialize, Deserialize};
use rocket::serde::Deserialize;
#[derive(FromForm)]
pub struct DeleteColumn {

View File

@@ -99,7 +99,7 @@ pub async fn table(conn: Db, tid: i32, sort_dir: Option<u8>, sort_field: Option<
/// View to redirect a post request handled to manipulate a table or its display representation back to the (new) table view.
/// Also uses table() but nulls all optional fields.
#[get("/<tid>", rank=2)]
pub async fn table_sec(conn: Db, tid: i32, user: AuthUser) -> Redirect {
pub async fn table_sec(tid: i32) -> Redirect {
let nus : Option<usize> = None;
let nu8 : Option<u8> = None;
let nvi32 : Option<Vec<i32>> = None;