use actix_web::{web, Scope};
use tracing::{info, warn};
use crate::registry::all_resources;
use crate::controllers::{
resource_controller::{
register_admix_resource_routes
}
};
use crate::controllers::auth_controller::{
login_form,
login_action,
logout_action,
dashboard_view,
profile_view,
api_login_action,
check_auth_status
};
use crate::utils::{
structs::{
RoleGuard
},
};
pub fn register_all_admix_routes() -> Scope {
info!("🔧 Starting AdminX route registration...");
let mut scope = web::scope("/adminx")
.route("/login", web::get().to(login_form))
.route("/login", web::post().to(login_action))
.route("/logout", web::get().to(logout_action)) .route("/logout", web::post().to(logout_action))
.route("", web::get().to(dashboard_view)) .route("/", web::get().to(dashboard_view)) .route("/dashboard", web::get().to(dashboard_view))
.route("/profile", web::get().to(profile_view))
.route("/api/login", web::post().to(api_login_action))
.route("/api/auth/status", web::get().to(check_auth_status));
let resources = all_resources();
info!("📋 Found {} resources to register", resources.len());
if resources.is_empty() {
warn!("⚠️ No resources found! Make sure you've called register_resource() before starting the server.");
return scope;
}
for resource in resources {
let resource_name = resource.resource_name();
let base_path = resource.base_path();
let allowed_roles = resource.allowed_roles();
info!("📝 Registering resource: '{}' at path: '{}'", resource_name, base_path);
info!("🔐 Allowed roles for {}: {:?}", resource_name, allowed_roles);
let resource_scope = web::scope(&format!("/{}", base_path))
.service(register_admix_resource_routes(resource))
.wrap(RoleGuard { allowed_roles });
scope = scope.service(resource_scope);
info!("✅ Successfully registered resource: '{}'", resource_name);
info!("🌐 Available URLs:");
info!(" - GET /adminx/{}/list", base_path);
info!(" - GET /adminx/{}/new", base_path);
info!(" - GET /adminx/{}/view/{{id}}", base_path);
info!(" - GET /adminx/{}/edit/{{id}}", base_path);
info!(" - GET /adminx/{} (API list)", base_path);
info!(" - POST /adminx/{} (API create)", base_path);
info!(" - GET /adminx/{}/{{id}} (API get)", base_path);
info!(" - PUT /adminx/{}/{{id}} (API update)", base_path);
info!(" - DELETE /adminx/{}/{{id}} (API delete)", base_path);
}
info!("🎉 AdminX route registration completed!");
scope
}
pub fn register_all_admix_routes_debug() -> Scope {
info!("🔧 Starting AdminX route registration (DEBUG MODE - NO AUTH)...");
let mut scope = web::scope("/adminx")
.route("/login", web::get().to(login_form))
.route("/login", web::post().to(login_action))
.route("/logout", web::get().to(logout_action)) .route("/logout", web::post().to(logout_action))
.route("", web::get().to(dashboard_view)) .route("/", web::get().to(dashboard_view)) .route("/dashboard", web::get().to(dashboard_view))
.route("/profile", web::get().to(profile_view))
.route("/api/login", web::post().to(api_login_action))
.route("/api/auth/status", web::get().to(check_auth_status));
let resources = all_resources();
info!("📋 Found {} resources to register", resources.len());
if resources.is_empty() {
warn!("⚠️ No resources found! Make sure you've called register_resource() before starting the server.");
return scope;
}
for resource in resources {
let resource_name = resource.resource_name();
let base_path = resource.base_path();
info!("📝 Registering resource: '{}' at path: '{}'", resource_name, base_path);
let resource_scope = web::scope(&format!("/{}", base_path))
.service(register_admix_resource_routes(resource));
scope = scope.service(resource_scope);
info!("✅ Successfully registered resource: '{}'", resource_name);
}
info!("🎉 AdminX route registration completed (DEBUG MODE)!");
scope
}
pub fn register_auth_routes_only() -> Scope {
web::scope("/adminx")
.route("/login", web::get().to(login_form))
.route("/login", web::post().to(login_action))
.route("/logout", web::get().to(logout_action))
.route("/logout", web::post().to(logout_action))
.route("", web::get().to(dashboard_view))
.route("/", web::get().to(dashboard_view))
.route("/dashboard", web::get().to(dashboard_view))
.route("/profile", web::get().to(profile_view))
.route("/api/login", web::post().to(api_login_action))
.route("/api/auth/status", web::get().to(check_auth_status))
}
pub fn register_resource_routes_only() -> Scope {
info!("🔧 Starting AdminX resource-only route registration...");
let mut scope = web::scope("/adminx");
let resources = all_resources();
info!("📋 Found {} resources to register", resources.len());
for resource in resources {
let resource_name = resource.resource_name();
let base_path = resource.base_path();
let allowed_roles = resource.allowed_roles();
info!("📝 Registering resource: '{}' at path: '{}'", resource_name, base_path);
let resource_scope = web::scope(&format!("/{}", base_path))
.service(register_admix_resource_routes(resource))
.wrap(RoleGuard { allowed_roles });
scope = scope.service(resource_scope);
info!("✅ Successfully registered resource: '{}'", resource_name);
}
info!("🎉 AdminX resource route registration completed!");
scope
}
pub fn register_all_admix_routes_enhanced() -> Scope {
info!("🔧 Starting Enhanced AdminX route registration...");
let mut scope = web::scope("/adminx")
.service(
web::scope("/auth")
.route("/login", web::get().to(login_form))
.route("/login", web::post().to(login_action))
.route("/logout", web::get().to(logout_action))
.route("/logout", web::post().to(logout_action))
.route("/status", web::get().to(check_auth_status))
)
.route("", web::get().to(dashboard_view))
.route("/", web::get().to(dashboard_view))
.route("/dashboard", web::get().to(dashboard_view))
.route("/profile", web::get().to(profile_view))
.route("/login", web::get().to(login_form))
.route("/login", web::post().to(login_action))
.route("/logout", web::get().to(logout_action))
.route("/logout", web::post().to(logout_action))
.service(
web::scope("/api")
.route("/login", web::post().to(api_login_action))
.route("/auth/status", web::get().to(check_auth_status))
);
let resources = all_resources();
info!("📋 Found {} resources to register", resources.len());
if resources.is_empty() {
warn!("⚠️ No resources found!");
return scope;
}
for resource in resources {
let resource_name = resource.resource_name();
let base_path = resource.base_path();
let allowed_roles = resource.allowed_roles();
info!("📝 Registering resource: '{}' at path: '{}' with roles: {:?}",
resource_name, base_path, allowed_roles);
let resource_scope = web::scope(&format!("/{}", base_path))
.service(register_admix_resource_routes(resource))
.wrap(RoleGuard { allowed_roles });
scope = scope.service(resource_scope);
}
info!("🎉 Enhanced AdminX route registration completed!");
scope
}