emptygif
اللغة: العربية

كل اللغات وأطر العمل

Loco Rust

مقتطف بلغة Rust لـ Loco يستجيب لطلبات /pixel.gif بملف GIF الفارغ، الذي يُفك ترميزه مرة واحدة عند بدء التشغيل.

Loco Rust

التثبيت
cargo install loco && loco new
الملف
src/controllers/pixel.rs
التشغيل
cargo loco start
// src/controllers/pixel.rs - register it in src/app.rs:
//   AppRoutes::with_default_routes().add_route(controllers::pixel::routes())
use axum::http::header;
use loco_rs::prelude::*;

const GIF: &[u8] = &[0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b];

async fn pixel() -> impl IntoResponse {
    (
        [
            (header::CONTENT_TYPE, "image/gif"),
            (header::CACHE_CONTROL, "no-store"),
        ],
        GIF,
    )
}

pub fn routes() -> Routes {
    Routes::new().add("/pixel.gif", get(pixel))
}

ترجمنا هذا المقتطف أو تحققنا من أنواعه.

التحقق

curl -sI http://localhost:8080/pixel.gif

الموقع الرسمي: Loco

المزيد بلغة Rust