Skip to content

Instantly share code, notes, and snippets.

@softmechanics
Created October 22, 2010 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softmechanics/640902 to your computer and use it in GitHub Desktop.
Save softmechanics/640902 to your computer and use it in GitHub Desktop.
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
-- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment