emptygif
言語: 日本語

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

Scotty Haskell

/pixel.gif へのリクエストに空の GIF を返す、Scotty 用の Haskell スニペット。GIF は起動時に一度だけデコードします。

Scotty Haskell

インストール
build-depends: base, bytestring, base64-bytestring, text, scotty; ghc-options: -threaded
ファイル
Main.hs
実行
cabal run
{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text.Lazy as TL
import Web.Scotty

-- A top-level constant: decoded once, on first use.
gif :: BL.ByteString
gif = BL.fromStrict (Base64.decodeLenient "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")

main :: IO ()
main = scotty 8080 $
  get "/pixel.gif" $ do
    setHeader "Content-Type" "image/gif"
    setHeader "Content-Length" (TL.pack (show (BL.length gif)))
    setHeader "Cache-Control" "no-store"
    raw gif

このスニペットは実行し、返されたバイト列を照合しました。

確認

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

公式サイト: Scotty

Haskell の他の例