/
mcmare
/
RustAPI
Обзор
Документация
Войти
/
mcmare
/
RustAPI
Код
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
v0.2.0
engine/src/bootstrap.rs
35 строк
2 KB
mcmare
Add WebUI: zero-config bootstrap, admin login, browser-based settings/endpoints/scripts management
18 июл 2026, 18:34
18 июл 2026, 18:34
e401c22
Код
Авторство
О чём код?
use anyhow::{Context, Result}; use std::path::Path; use uuid::Uuid; /// Creates `config/`, `config/endpoints/`, `config/scripts/` and `data/` if they don't /// exist yet, so a fresh checkout with nothing prepared still has somewhere for the /// hot-reload watcher to attach and for the WebUI to write its first files into. pub fn ensure_dirs(config_dir: &Path, data_dir: &Path) -> Result<()> { std::fs::create_dir_all(config_dir.join("endpoints")) .with_context(|| format!("failed to create {}", config_dir.join("endpoints").display()))?; std::fs::create_dir_all(config_dir.join("scripts")) .with_context(|| format!("failed to create {}", config_dir.join("scripts").display()))?; std::fs::create_dir_all(data_dir).with_context(|| format!("failed to create {}", data_dir.display()))?; Ok(()) } /// `JWT_SECRET` env var wins if set. Otherwise a secret is generated once and persisted /// to `data/jwt_secret` so it survives restarts — this is what makes "just run the /// binary, no setup" possible instead of requiring an env var up front. pub fn resolve_jwt_secret(data_dir: &Path) -> Result<String> { if let Ok(secret) = std::env::var("JWT_SECRET") { if !secret.is_empty() { return Ok(secret); } } let path = data_dir.join("jwt_secret"); if path.exists() { let secret = std::fs::read_to_string(&path) .with_context(|| format!("failed to read {}", path.display()))?; return Ok(secret.trim().to_string()); } let secret = format!("{}{}", Uuid::new_v4().simple(), Uuid::new_v4().simple()); std::fs::write(&path, &secret).with_context(|| format!("failed to write {}", path.display()))?; Ok(secret) }