wip: clean up url authoring

This commit is contained in:
Viv Lim 2023-07-04 19:54:36 -07:00
parent 9c58a5235c
commit a3cc13c3ee
5 changed files with 83 additions and 16 deletions

27
Cargo.lock generated
View File

@ -324,6 +324,7 @@ dependencies = [
"axum_static",
"clap",
"config",
"const_format",
"deadpool",
"diesel",
"diesel-async",
@ -532,6 +533,26 @@ dependencies = [
"yaml-rust",
]
[[package]]
name = "const_format"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c990efc7a285731f9a4378d81aff2f0e85a2c8781a05ef0f8baa8dac54d0ff48"
dependencies = [
"const_format_proc_macros",
]
[[package]]
name = "const_format_proc_macros"
version = "0.2.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e026b6ce194a874cb9cf32cd5772d1ef9767cc8fcb5765948d74f37a9d8b2bf6"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
@ -2504,6 +2525,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "untrusted"
version = "0.7.1"

View File

@ -38,4 +38,6 @@ rand = { version = "0.8", features = ["min_const_gen"] }
axum_static = "1.2.1"
axum-option = "0.1.0"
tower-http = { version = "0.4.0", features = ["trace"] }
tower-http = { version = "0.4.0", features = ["trace"] }
const_format = { version = "0.2.31" }

View File

@ -36,6 +36,7 @@ pub mod routes;
pub mod models;
pub mod schema;
pub mod app_error_handling;
pub mod paths;
pub type DbPool = Extension<Pool<AsyncPgConnection>>;
@ -124,16 +125,16 @@ async fn main() {
// build our application with a route
let app = Router::new()
.route("/", get(root))
.route("/boards", get(routes::board_index::board_index))
.route("/login", get(oauth2_login))
.route("/auth/authorized", get(login_authorized))
.route("/protected", get(protected))
.route("/logout", get(logout))
.route("/profile/new", get(routes::profiles::create_form))
.nest("/static", axum_static::static_router("static").with_state(())) // static_router uses different state than with_state
.route(paths::ROOT, get(root))
.route(paths::BOARDS, get(routes::board_index::board_index))
.route(paths::LOGIN, get(oauth2_login))
.route(paths::Auth::AUTHORIZED, get(login_authorized))
.route(paths::PROTECTED, get(protected))
.route(paths::LOGOUT, get(logout))
//.route(paths::Profile::NEW, get(routes::profiles::create_form))
.nest(paths::STATIC, axum_static::static_router("static").with_state(())) // static_router uses different state than with_state
.merge(manage_profiles_router())
.nest("/admin", admin_router())
.nest(paths::ADMIN, admin_router())
.fallback(fallback_handler)
.layer(session_layer)
.layer(TraceLayer::new_for_http())

37
server/src/paths/mod.rs Normal file
View File

@ -0,0 +1,37 @@
pub use const_format::concatcp;
pub const BASE: &'static str = "";
pub const ROOT: &str = concatcp!(BASE, "/");
pub const BOARDS: &str = concatcp!(BASE, "/boards");
pub const LOGIN: &str = concatcp!(BASE, "/login");
pub const LOGOUT: &str = concatcp!(BASE, "/logout");
pub const PROTECTED: &str = concatcp!(BASE, "/protected"); //?
pub const AUTH: &str = concatcp!(BASE, "/auth");
pub mod Auth {
use super::*;
pub const AUTHORIZED: &str = concatcp!(AUTH, "/authorized");
}
pub const PROFILE: &str = concatcp!(BASE, "/profile");
pub mod Profile {
use super::*;
//pub const NEW: &str = concatcp!(PROFILE, "/new");
}
pub const STATIC: &str = concatcp!(BASE, "/static");
pub const MANAGE_PROFILES: &str = concatcp!(BASE, "/my_profiles");
pub mod ManageProfiles {
use super::*;
pub const ADD: &str = concatcp!(MANAGE_PROFILES, "/add");
const SWITCH_TO: &str = concatcp!(MANAGE_PROFILES, "/switch_to/");
pub fn switch_to(id: i32) -> String {
format!("{}{}", SWITCH_TO, id)
}
const EDIT: &str = concatcp!(MANAGE_PROFILES, "/edit/{}");
pub fn edit(id: i32) -> String {
format!("{}{}", EDIT, id)
}
}
pub const ADMIN: &str = concatcp!(BASE, "/admin");
pub mod AdminNested {
}

View File

@ -14,9 +14,9 @@ use super::{header::HeaderTemplateData, generic_forms::{create_modify::{Insertab
pub fn manage_profiles_router() -> Router<AppState, Body> {
Router::new()
.route("/my_profiles", get(GeneratedTableRoutes::<ProfileTableSource, crate::models::Profile, ProfileTableItemActions>::table_route_get))
.route("/my_profiles/add", get(GeneratedForms::<ProfileForm, crate::models::Profile>::form_insert_route_get))
.route("/my_profiles/add", post(GeneratedForms::<ProfileForm, crate::models::Profile>::form_insert_route_post))
.route(crate::paths::MANAGE_PROFILES, get(GeneratedTableRoutes::<ProfileTableSource, crate::models::Profile, ProfileTableItemActions>::table_route_get))
.route(crate::paths::ManageProfiles::ADD, get(GeneratedForms::<ProfileForm, crate::models::Profile>::form_insert_route_get))
.route(crate::paths::ManageProfiles::ADD, post(GeneratedForms::<ProfileForm, crate::models::Profile>::form_insert_route_post))
//.route("/add", post(add_board_post))
}
@ -102,7 +102,7 @@ impl TabularItemSource<crate::models::Profile, ProfileTableItemActions> for Prof
fn table_spec<TContext>(context: TContext) -> GeneratedTableSpec where TContext: ContextBound {
GeneratedTableSpec {
action_buttons: vec![
TableAction { label: "New profile".to_string(), url: "/my_profiles/add".to_string() }
TableAction { label: "New profile".to_string(), url: crate::paths::ManageProfiles::ADD.to_string() }
],
table_help_text: format!("Manage your profiles"),
columns: vec![
@ -139,11 +139,11 @@ impl TabularItemSource<crate::models::Profile, ProfileTableItemActions> for Prof
TableCell::Text(profile.display_name),
TableCell::Link{
label: "Switch to".to_string(),
target: format!("/my_profiles/switch_to/{}", profile.id)
target: crate::paths::ManageProfiles::switch_to(profile.id)
},
TableCell::Link{
label: "Edit".to_string(),
target: format!("/my_profiles/edit/{}", profile.id)
target: crate::paths::ManageProfiles::edit(profile.id)
}
])
}