code wiki / _hdl_build / nx_domain_forge.nx

nx_domain_forge.nx source

↩ module page · 116 lines · 6573 B

1// nx_domain_forge.nx -- APPEND-DRIVEN, DATA-DRIVEN domain/site AUTO-BUILDER (operator 2026-06-29: "we should be 2// able to append new domains and sites and have the site builders EMIT them using the auto-building capabilities 3// to put together PIECES like LEGOS"). Reads a domain REGISTRY (domains.conf, one row per domain) and for EACH 4// domain COMPOSES the site from reusable Lego pieces (the nx_hub_template block emitter: header/blog/video/ 5// calendar/friends) AND emits the standalone-domain wiring: the docroot index.html + the sites.conf host->docroot 6// VHOST line + a per-domain CERT-REGISTRY row (host -> wildcard chain/key). APPEND A DOMAIN = add ONE registry row 7// + re-run (IDEMPOTENT: the emitted configs are rewritten from the registry every run -> the registry is the SSOT, 8// no hand-editing). Composes nx_hub_template. domains.conf row (space-delim): <domain> <docroot_dir> <hub_config>. 9// license_tier: ORIGINAL 10import "nx_hub_template.nx" 11import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 12const K_MAGIC_1048576: i64 = 1048576 13const K_MAGIC_2048: i64 = 2048 14const K_MAGIC_4096: i64 = 4096 15const K_MAGIC_1024: i64 = 1024 16 17func df_cat(dst: *u8, o: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o } 18func df_catb(dst: *u8, o: i64, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n {dst[o]=b[i];o=o+1;i=i+1} return o } 19 20func df_eol(b: *u8, n: i64, st: i64) -> i64 { 21 var i: i64=st; var f: i64=0 22 while f==0 { if i>=n {f=1} else { if (b[i] as i64)==10 {f=1} else {i=i+1} } } 23 return i 24} 25func df_split(b: *u8, ls: i64, le: i64, offs: *i64, lens: *i64, maxf: i64) -> i64 { 26 var nf: i64=0; var i: i64=ls 27 while i<le { 28 var sk: i64=1 29 while sk==1 { if i>=le {sk=0} else { if (b[i] as i64)==32 {i=i+1} else {sk=0} } } 30 if i<le { 31 let st: i64=i; var sc: i64=1 32 while sc==1 { if i>=le {sc=0} else { if (b[i] as i64)==32 {sc=0} else {i=i+1} } } 33 if nf<maxf { offs[nf]=st; lens[nf]=i-st; nf=nf+1 } 34 } 35 } 36 return nf 37} 38func df_copyz(dst: *u8, src: *u8, off: i64, len: i64) -> i64 { var i: i64=0; while i<len { dst[i]=src[off+i]; i=i+1 } dst[len]=0 as u8; return len } 39 40// emit ONE domain: compose its hub (Lego pieces) -> <docroot>/index.html; append vhost + cert-registry rows. 41func df_emit(domain: *u8, dl: i64, docroot: *u8, drl: i64, hubcfg: *u8, scfd: i64, crfd: i64) -> i64 { 42 let szb: *i64 = sys_mmap(16) as *i64; szb[0]=0 43 let cfg: *u8 = sys_read_file(hubcfg, szb) 44 if (cfg as i64)==0 { return 0-1 } 45 let html: *u8 = sys_mmap(K_MAGIC_1048576) 46 let hn: i64 = ht_emit_hub(cfg, szb[0], html) 47 sys_mkdir(docroot, 0x1ed) 48 let idx: *u8 = sys_mmap(K_MAGIC_2048); var io: i64 = df_catb(idx, 0, docroot, drl); io = df_cat(idx, io, "/index.html" as *u8); idx[io]=0 as u8 49 let fd: i64 = sys_openat_wr(idx, 0x1a4); if fd<0 { return 0-2 } sys_write(fd, html, hn); sys_close(fd) 50 // vhost row: <domain> \t <docroot>/index.html 51 let vr: *u8 = sys_mmap(K_MAGIC_4096); var vo: i64 = df_catb(vr, 0, domain, dl); vr[vo]=9 as u8; vo=vo+1; vo=df_catb(vr, vo, idx, io); vr[vo]=10 as u8; vo=vo+1 52 sys_write(scfd, vr, vo) 53 // cert-registry row: <domain> \t certs/le_<domain>_fullchain.der \t certs/le_<domain>_ecdsa_key.bin 54 let cr: *u8 = sys_mmap(K_MAGIC_1024); var co: i64 = df_catb(cr, 0, domain, dl) 55 co = df_cat(cr, co, "\tcerts/le_" as *u8); co = df_catb(cr, co, domain, dl) 56 co = df_cat(cr, co, "_fullchain.der\tcerts/le_" as *u8); co = df_catb(cr, co, domain, dl) 57 co = df_cat(cr, co, "_ecdsa_key.bin\n" as *u8) 58 sys_write(crfd, cr, co) 59 return 0 60} 61 62// FORGE: registry -> emit every domain. Idempotent (truncates + rewrites the outputs from the SSOT registry). 63// returns the number of domains emitted (>=0), or negative on a registry/io error. 64func df_forge(reg: *u8, sitesconf: *u8, certreg: *u8) -> i64 { 65 let szb: *i64 = sys_mmap(16) as *i64; szb[0]=0 66 let rb: *u8 = sys_read_file(reg, szb) 67 if (rb as i64)==0 { return 0-1 } 68 let n: i64 = szb[0] 69 let scfd: i64 = sys_openat_wr(sitesconf, 0x1a4); if scfd<0 { return 0-2 } 70 let crfd: i64 = sys_openat_wr(certreg, 0x1a4); if crfd<0 { sys_close(scfd); return 0-3 } 71 let hdr: *u8 = "# AUTO-GENERATED by nx_domain_forge from domains.conf -- the registry is the SSOT; append there, never hand-edit here.\n" as *u8 72 var hl: i64 = 0; while hdr[hl]!=(0 as u8){hl=hl+1} 73 sys_write(scfd, hdr, hl); sys_write(crfd, hdr, hl) 74 let offs: *i64 = sys_mmap(64) as *i64 75 let lens: *i64 = sys_mmap(64) as *i64 76 let dom: *u8 = sys_mmap(256); let dr: *u8 = sys_mmap(K_MAGIC_1024); let hc: *u8 = sys_mmap(K_MAGIC_1024) 77 var cur: i64=0; var count: i64=0 78 while cur<n { 79 let le: i64 = df_eol(rb, n, cur) 80 var ok_line: i64=1 81 if le<=cur {ok_line=0} 82 if ok_line==1 { if (rb[cur] as i64)==35 {ok_line=0} } 83 if ok_line==1 { 84 let nf: i64 = df_split(rb, cur, le, offs, lens, 8) 85 if nf>=3 { 86 let dl: i64 = df_copyz(dom, rb, offs[0], lens[0]) 87 let drl: i64 = df_copyz(dr, rb, offs[1], lens[1]) 88 df_copyz(hc, rb, offs[2], lens[2]) 89 if df_emit(dom, dl, dr, drl, hc, scfd, crfd) == 0 { count = count + 1 } 90 } 91 } 92 cur = le + 1 93 } 94 sys_close(scfd); sys_close(crfd) 95 return count 96} 97 98func df_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 99// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 100// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 101// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 102// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 103func df_wn(v: i64) -> i64 { nxi_out(v); return 0 } 104 105func main(argc: i64, argv: *i64) -> i64 { 106 var reg: *u8 = "knowledge/hosting/domains.conf" as *u8 107 var sc: *u8 = "knowledge/hosting/sites_generated.conf" as *u8 108 var cr: *u8 = "knowledge/hosting/certs_generated.conf" as *u8 109 if argc >= 2 { reg = argv[1] as *u8 } 110 if argc >= 3 { sc = argv[2] as *u8 } 111 if argc >= 4 { cr = argv[3] as *u8 } 112 let r: i64 = df_forge(reg, sc, cr) 113 if r < 0 { df_w("domain-forge: registry/io error rc=" as *u8); df_wn(r); df_w("\n" as *u8); return 1 } 114 df_w("DOMAIN-FORGE: emitted " as *u8); df_wn(r); df_w(" domains from " as *u8); df_w(reg); df_w(" (docroots + vhost + cert rows; idempotent SSOT)\n" as *u8) 115 return 0 116}