Created
October 22, 2010 16:33
-
-
Save softmechanics/640902 to your computer and use it in GitHub Desktop.
send and delete a file with Yesod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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