public
Last active

send and delete a file with Yesod

  • Download Gist
sendTempFile.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
{-# LANGUAGE TemplateHaskell
, QuasiQuotes
, OverloadedStrings
, TypeFamilies
#-}
 
import Yesod
import Yesod.Handler
 
import Data.Maybe
import System.IO
import System.Directory
import System.Process
import Text.Printf
 
import Network.Wai
 
-- http://github.com/softmechanics/wai/commit/2cb5ea47ca458f4b91a73e01a1dd0a29e84e001e
import Network.Wai.Enumerator (fromTempFile)
 
type Handler = GHandler SendTempFile SendTempFile
 
data SendTempFile = SendTempFile
mkYesod "SendTempFile" [$parseRoutes|
/ RootR GET
|]
instance Yesod SendTempFile where approot _ = ""
 
tempFileChooseRep :: ContentType -> FilePath -> ChooseRep
tempFileChooseRep ct fp = const $ return (ct, ResponseEnumerator $ fromTempFile fp)
 
sendTempFile :: ContentType -> FilePath -> GHandler sub master a
sendTempFile ct fp = sendResponse $ tempFileChooseRep ct fp
 
genGraph :: IO FilePath
genGraph = do
(f,h) <- openTempFile "/tmp" "tmp.txt"
hPutStrLn h f
hClose h
return f
 
getRootR :: Handler ()
getRootR = do
liftIO genGraph >>= sendTempFile typePlain
 
main = do
basicHandler 3000 SendTempFile

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.