feat(api): add Leptos server functions bridging client and server
- Add src/api/ module with server functions for networks, hosts, applications - Each server function retrieves the pool via use_context::<AnyPool>() - Pool is injected via provide_context in two places in main.rs: * leptos_routes_with_context: for SSR renders and inline server fn calls * handle_server_fns_with_context on /api/*fn_name: for WASM client calls - create_host validates IP against network CIDR before inserting - create_network validates CIDR format before inserting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
67
src/api/networks.rs
Normal file
67
src/api/networks.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
// api/networks.rs — Server functions for networks
|
||||
|
||||
use leptos::prelude::*;
|
||||
|
||||
use crate::models::Network;
|
||||
|
||||
// ─── Queries ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Returns all networks from the database.
|
||||
///
|
||||
/// Called by the Networks page to populate the list.
|
||||
#[server]
|
||||
pub async fn get_networks() -> Result<Vec<Network>, ServerFnError> {
|
||||
use sqlx::AnyPool;
|
||||
use crate::server::repository::networks as repo;
|
||||
|
||||
// `use_context` retrieves a value previously registered with `provide_context`.
|
||||
// The pool was injected in main.rs before every request.
|
||||
// `ok_or_else` converts `None` into an error (defensive: should never happen).
|
||||
let pool = use_context::<AnyPool>()
|
||||
.ok_or_else(|| ServerFnError::new("Database pool not found in context"))?;
|
||||
|
||||
// Propagate any DB error as a ServerFnError so the client sees a clean message.
|
||||
repo::list_networks(&pool)
|
||||
.await
|
||||
.map_err(|e| ServerFnError::new(e.to_string()))
|
||||
}
|
||||
|
||||
// ─── Mutations ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Creates a new network with the given CIDR block.
|
||||
///
|
||||
/// Returns the created record (with its auto-generated id).
|
||||
/// Returns an error if the CIDR is malformed or already exists.
|
||||
#[server]
|
||||
pub async fn create_network(cidr: String) -> Result<Network, ServerFnError> {
|
||||
use sqlx::AnyPool;
|
||||
use crate::server::{repository::networks as repo, validation::validate_cidr};
|
||||
|
||||
let pool = use_context::<AnyPool>()
|
||||
.ok_or_else(|| ServerFnError::new("Database pool not found in context"))?;
|
||||
|
||||
// Validate the CIDR before touching the database.
|
||||
// Example of a valid CIDR: "192.168.1.0/24"
|
||||
validate_cidr(&cidr).map_err(|e| ServerFnError::new(e.to_string()))?;
|
||||
|
||||
repo::create_network(&pool, &cidr)
|
||||
.await
|
||||
.map_err(|e| ServerFnError::new(e.to_string()))
|
||||
}
|
||||
|
||||
/// Deletes a network by id.
|
||||
///
|
||||
/// Also deletes all hosts in that network (via `ON DELETE CASCADE`).
|
||||
/// Returns `true` if the network existed and was deleted.
|
||||
#[server]
|
||||
pub async fn delete_network(id: i64) -> Result<bool, ServerFnError> {
|
||||
use sqlx::AnyPool;
|
||||
use crate::server::repository::networks as repo;
|
||||
|
||||
let pool = use_context::<AnyPool>()
|
||||
.ok_or_else(|| ServerFnError::new("Database pool not found in context"))?;
|
||||
|
||||
repo::delete_network(&pool, id)
|
||||
.await
|
||||
.map_err(|e| ServerFnError::new(e.to_string()))
|
||||
}
|
||||
Reference in New Issue
Block a user