api_index - OmniCloudOrg/OmniOrchestrator GitHub Wiki
Path: src/api/index.rs
- struct RouteInfo
- struct RoutesResponse
- struct RoutesCollection
- fn new
- fn add_route
- fn get_routes
- fn collect_routes
- fn routes_ui
pub struct RouteInfo {
/// Path of the route
path: String,
/// HTTP methods supported by the route
methods: Vec<String>,
}
Route information structure for API documentation
pub struct RoutesResponse {
/// List of all available routes and their methods
routes: Vec<RouteInfo>,
}
Response structure for routes listing endpoint
pub struct RoutesCollection {
routes: Vec<RouteInfo>,
}
Routes collection that will be populated during startup
pub fn new() -> Self {
Self { routes: Vec::new() }
}
pub fn add_route(&mut self, path: String, method: String) {
// Check if the route already exists
if let Some(route) = self.routes.iter_mut().find(|r| r.path == path) {
// Add method if it doesn't exist
if !route.methods.contains(&method) {
route.methods.push(method);
}
} else {
// Add new route info
self.routes.push(RouteInfo {
path,
methods: vec![method],
});
}
}
pub fn get_routes(&self) -> Vec<RouteInfo> {
// ... function body
}
pub fn add_route(&mut self, path: String, method: String) {
// Check if the route already exists
if let Some(route) = self.routes.iter_mut().find(|r| r.path == path) {
// Add method if it doesn't exist
if !route.methods.contains(&method) {
route.methods.push(method);
}
} else {
// Add new route info
self.routes.push(RouteInfo {
path,
methods: vec![method],
});
}
}
pub fn get_routes(&self) -> Vec<RouteInfo> {
self.routes.clone()
}
}
// ... function definition continues
// ... function body
}
pub fn get_routes(&self) -> Vec<RouteInfo> {
self.routes.clone()
}
}
/// Global singleton instance of the routes collection
/// Stores information about all registered API routes
lazy_static! {
static ref ROUTES_COLLECTION: Arc<Mutex<RoutesCollection>> = Arc::new(Mutex::new(RoutesCollection::new()));
}
/// Decodes any encoded characters in route paths and preserves parameter notation
fn decode_route_path(path: &str) -> String {
let mut result = path.to_string();
// Handle common Unicode escape sequences
let replacements = [
("\\u003C", "<"), ("\\u003E", ">"),
("\\u003c", "<"), ("\\u003e", ">"),
("\\u0026", "&"), ("\\u0027", "'"),
("\\u0022", "\""), ("\\u003D", "="),
// ... function definition continues
// ... function body
}
pub fn collect_routes(rocket: &Rocket<Build>) {
let mut routes_collection = ROUTES_COLLECTION.lock().unwrap();
for route in rocket.routes() {
// Get the path and decode any escaped characters
let path = decode_route_path(&route.uri.to_string());
routes_collection.add_route(
path,
route.method.to_string(),
);
}
}
/// Routes listing endpoint providing HTML representation of routes
#[get("/")]
pub fn routes_ui() -> content::RawHtml<String> {
let routes_collection = ROUTES_COLLECTION.lock().unwrap();
let routes = routes_collection.get_routes();
// Collect unique versions dynamically
// ... function definition continues
// ... function body
}
pub fn routes_ui() -> content::RawHtml<String> {
let routes_collection = ROUTES_COLLECTION.lock().unwrap();
let routes = routes_collection.get_routes();
// Collect unique versions dynamically
let mut versions: Vec<String> = routes
.iter()
.filter_map(|route| {
let path = &route.path;
if let Some(start) = path.find("/api/v") {
let rest = &path[start + 6..];
let end = rest.find('/').unwrap_or(rest.len());
let version = &rest[..end];
if version.chars().all(|c| c.is_numeric()) {
Some(format!("v{}", version))
} else {
None
}
} else {
None
}
// ... function definition continues
// ... function body
}
Routes listing endpoint providing HTML representation of routes