Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active December 20, 2015 01:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simonmichael/6050387 to your computer and use it in GitHub Desktop.
setting Expires header in snap
-- | Set an Expires reponse header to this number of days from now.
expiresInDays :: Integer -> Snap ()
expiresInDays n = do
now <- liftIO getCurrentTime
let later = addUTCTime (fromInteger $ n * 60 * 60 * 24) now
getResponse >>= return . addHeader "Expires" (formatRFC1123 later) >>= putResponse
formatRFC1123 :: UTCTime -> BS.ByteString
formatRFC1123 = BS8.pack . formatTime defaultTimeLocale "%a, %d %b %Y %X %Z"
Example:
route
[ ("public/images", expiresInDays 7 >> (serveDirectory $ cwd </> "public"))
, ("public", serveDirectory $ cwd </> "public")
...
@gregorycollins
Copy link

I would suggest the more general

expiresOn :: UTCTime -> Snap ()
expiresIn :: NominalDiffTime -> Snap ()
expiresInDays :: Int -> Snap ()

where expiresIn (operating in seconds, this has a proper Num instance also) and expiresInDays are expressed in terms of expiresOn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment