Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
send and delete a file with Yesod
{-# 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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment