تخطي إلى المحتوى

بنية المشروع

هذه الصفحة مترجمة من الإنجليزية. اقرأ الصفحة الإنجليزية

لكل تطبيق Zeyn التخطيط نفسه، فيجد الشخص أو الوكيل الأشياء في المكان نفسه في كل مشروع.

config/app.json          defaults; APP_* environment variables override them
locales/<lang>.json      every user facing text; all files have the same keys
src/main.rs              loads configuration and starts the app
src/app.rs               the App: services, features, security settings
src/features/<name>/     one folder per feature
    mod.rs               the Feature: route prefix, guards, screens, APIs, menu
    models.rs            #[entity] and #[model] types
    store.rs             services that read and write data
    actions.rs           #[action] functions
    screens.rs           #[screen] functions
tests/                   tests that drive the app through its router

التطبيق

يبني src/app.rs الكائن App. يسجّل الخدمات والميزات والإعدادات التي تنطبق على كل شاشة:

pub fn app(config: Config) -> App {
    App::new(t!("app.name"))
        .config(config)
        .i18n(i18n())
        .service(NoteStore::default())
        .auth(ForwardAuth::new("x-forwarded-user").groups("x-forwarded-groups"))
        .rate_limit(RateLimit::per_ip(600, Duration::from_secs(60)))
        .home(features::home::home)
        .feature(features::home::feature())
        .feature(features::notes::feature())
}

يفحص App::serve(addr) التطبيق ويبدأ الخادم. ويعيد App::router() موجّه axum، الذي تستخدمه الاختبارات دون فتح منفذ.

الميزة

تجمع الميزة بادئة مسار وحراساً وشاشات وواجهات API وأوامر وقائمة:

pub fn feature() -> Feature {
    Feature::new("notes")
        .route_prefix("/notes")
        .guard(SignedIn)
        .screen(note_list)
        .screen(note_editor)
        .api(search_notes)
        .menu(|m| m.group(t!("menu.notes"), Icon::Edit).item(t!("notes.title"), note_list))
}

الخدمات

الخدمة (service) قيمة رخيصة النسخ تستقبلها الإجراءات والشاشات كوسائط. سجّلها مرة واحدة بـ App::service؛ ويرفض التطبيق البدء عندما تحتاج شاشة إلى خدمة لم يسجلها أحد.

#[derive(Clone, Default)]
pub struct NoteStore(Arc<RwLock<Vec<Note>>>);

impl Service for NoteStore {}

الإعدادات

يقرأ Config::load المصادر بالترتيب؛ وتفوز المصادر اللاحقة. يعيد config.get("key") قيمة، ويعيد config.secret("key") كائن Secret لا يُطبع أبداً.

let config = Config::load(vec![Box::new(JsonFileSource("config/app.json".into())), Box::new(EnvSource("APP"))]).await?;
let addr = config.get("server.addr").unwrap_or("127.0.0.1:3000").to_string();

الاختبارات

تبني الاختبارات الموجّه وتستدعيه في الذاكرة:

#[tokio::test]
async fn notes_open() {
    let router = app(Config::default()).router().unwrap();
    let res = router.oneshot(Request::get("/notes").body(Body::empty()).unwrap()).await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
}