gioser

tawasuyu/brahman

Rust 326TOML 123JSON 14Markdown 114554 Sutichaykuna

⎇ 3f8a3ea4b6d3de529e42ae418d486bc81cca308e crates/modules/matilda/matilda-config/src/lib.rs Rust · 63 siq'ikuna · 2.0 KB Ñawpa willay Pi imata llamkarqan ch'uya

feat(matilda): administración de servidores — core + config + plan 3f8a3ea4b · sergio ·

1//! `matilda-config` — del modelo declarativo a archivos de configuración.
2//!
3//! Funciones puras: toman un tipo de `matilda-core` y devuelven el texto
4//! de configuración listo para escribir en el servidor. No tocan disco
5//! ni Docker — sólo construyen strings, así que cada salida es testeable
6//! y determinista.
7//!
8//! - [`docker`] — `Container` → `docker run` / servicio docker-compose.
9//! - [`nginx`] — `VHost` → bloque `server` de nginx.
10
11#![forbid(unsafe_code)]
12
13pub mod docker;
14pub mod nginx;
15
16pub use docker::{compose_service, docker_run_command};
17pub use nginx::nginx_server_block;
18
19use matilda_core::Inventory;
20
21/// Renderiza el `docker-compose.yml` completo de un inventario.
22pub fn compose_file(inv: &Inventory) -> String {
23    let mut out = String::from("services:\n");
24    for c in inv.containers() {
25        out.push_str(&compose_service(c));
26    }
27    out
28}
29
30/// Renderiza el archivo de sites de nginx — un bloque `server` por
31/// vhost, separados por una línea en blanco.
32pub fn nginx_sites(inv: &Inventory) -> String {
33    inv.vhosts()
34        .map(nginx_server_block)
35        .collect::<Vec<_>>()
36        .join("\n")
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use matilda_core::{Container, VHost};
43
44    #[test]
45    fn compose_file_lists_every_container() {
46        let mut inv = Inventory::new();
47        inv.add_container(Container::new("web", "nginx"));
48        inv.add_container(Container::new("db", "postgres:16"));
49        let yaml = compose_file(&inv);
50        assert!(yaml.starts_with("services:\n"));
51        assert!(yaml.contains("  web:\n") && yaml.contains("  db:\n"));
52    }
53
54    #[test]
55    fn nginx_sites_renders_every_vhost() {
56        let mut inv = Inventory::new();
57        inv.add_vhost(VHost::to_container("a.com", "web", 80));
58        inv.add_vhost(VHost::to_container("b.com", "web", 80));
59        let conf = nginx_sites(&inv);
60        assert!(conf.contains("server_name a.com;"));
61        assert!(conf.contains("server_name b.com;"));
62    }
63}