emptygif
言語: 日本語

すべての言語・フレームワーク

Loco Rust

/pixel.gif へのリクエストに空の GIF を返す、Loco 用の Rust スニペット。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 の他の例