Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created January 29, 2017 07:11
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 naoto-ogawa/793b516ef32c25b48332818a17fb24aa to your computer and use it in GitHub Desktop.
Save naoto-ogawa/793b516ef32c25b48332818a17fb24aa to your computer and use it in GitHub Desktop.
Capturing in Servant
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Exception as E
import Servant
import Control.Monad.IO.Class -- liftIO
import Network.Wai.Handler.Warp -- run
type FileRead04 = "file" :> Capture "filename" String :> Get '[PlainText] String
myAPI04 :: Proxy FileRead04
myAPI04 = Proxy
app04 :: Application
app04 = serve myAPI04 server04
server04 :: Server FileRead04
server04 = \x -> do
-- liftIO (putStrLn x)
ret <- liftIO (readF x)
case ret of
Left e -> throwError err404 {errBody="file error"}
Right s -> return s
readF :: String -> IO (Either SomeException String)
readF x = E.try (readFile x)
main04 :: IO ()
main04 = run 8080 app04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment