Created
January 29, 2017 07:11
-
-
Save naoto-ogawa/793b516ef32c25b48332818a17fb24aa to your computer and use it in GitHub Desktop.
Capturing in Servant
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 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