How do you add logic into the following `myApp` haskell function? - haskell

I am still trying to understand how haskell syntax works. So, here's a dead simple wai/warp application.
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types (status200)
import Network.Wai.Handler.Warp (run)
myApp _ respond = respond $
responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
main = run 3000 myApp
If I want to print out some text into stdout with putStrLn before returning the status 200 and "Hello World" plain text, how would I implement it?

myApp has this type:
myApp :: Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
so you can add your own IO action before returning the response like this:
myApp _ respond = do putStrLn "processing request"
respond $ responseLBS status200 [("Content-Type", "text/plain")] "Hello World"

Related

How to find definition of Haskell function in VSCode?

I'm learning Haskell, and some functions like text aren't easy to google. Look at this example:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Spock
import Web.Spock.Config
import Control.Monad.Trans
import Data.IORef
import qualified Data.Text as T
data MySession = EmptySession
data MyAppState = DummyAppState (IORef Int)
main :: IO ()
main =
do ref <- newIORef 0
spockCfg <- defaultSpockCfg EmptySession PCNoDatabase (DummyAppState ref)
runSpock 8080 (spock spockCfg app)
app :: SpockM () MySession MyAppState ()
app =
do get root $ text "Hello World!"
get ("hello" <//> var) $ \name ->
do (DummyAppState ref) <- getState
visitorNumber <- liftIO $ atomicModifyIORef' ref $ \i -> (i+1, i+1)
text ("Hello " <> name <> ", you are visitor number " <> T.pack (show visitorNumber))
do get root $ text "Hello World!"
Normally, on VSCode, with any language, I would click on text with the rigth button, and "go to defintion". How do I find the definition of a function in VSCode?
I installed the haskell.haskell extension, and it didn't add this functionality.

Is there any way to catch all exceptions in scotty without wrapping all of my code in Exception Catching

I would like for my default handler to be able to catch all of the exceptions that my App throws but in order for this to happen I need to manually call raise after manually adding some exception catching around my IO code.
below is an example minimal server:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Lib
( someFunc
) where
import Web.Scotty.Trans
import Control.Monad.Trans
import Control.Monad.Reader
import Control.Monad.Catch
import Control.Monad.Except
import Data.Text.Lazy as TL
data AppEnv = AppEnv
{ appStuff :: String
}
newtype App a = App
{ unApp :: ReaderT AppEnv IO a
} deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppEnv, MonadThrow)
someFunc :: IO ()
someFunc = do
let run a = runReaderT (unApp $ App a) (AppEnv "APPY STUFF")
scottyT 8080 run $ do
defaultHandler $ \(e :: TL.Text) -> do
liftIO $ print "HERE"
liftIO $ print $ showError e
html $ "Something Went Seriously Wrong"
get "/" $ do
(r :: (Either TL.Text String)) <- liftIO $ runExceptT $ do
(uId) <- lift $ readFile "./helloworld.txt"
return $ ("hello")
liftIO $ print r
case r of
Left l -> raise l
Right s -> (html "hello world")
get "/catch-this" $ do
error "Catch Me"
(html "hello world")
notFound $ do
html "That is not a valid route"
I would like to be able to catch all of my uncaught exceptions in my default handler however this is not the default behavior of scotty that only happens if you call raise. I could wrap all of my ActionM code blocks in ExceptT however this seems like a messy/mechanical way of solving this problem. I mostly want to do this for logging purposes so I can report out to Sentry or Log to a file and this would make it much more convenient.
I figured I'd throw this in there as I recently was looking for this same solution again. It's unfortunate but I was never able to get the behavior that I wanted out of scotty.
Fortunately since scotty is just a nice library to create WAI application's you can get a nice work around using the Settings type from warp and the Options type from scotty.
Below is an example of how you can approach this:
{-# LANGUAGE OverloadedStrings #-}
module Lib
( someFunc
) where
import Web.Scotty.Trans
import Data.Text
import qualified Data.Text.Lazy as TL
import Control.Monad.IO.Class
import Control.Exception
import Network.HTTP.Types
import System.IO.Error
import Network.Wai.Handler.Warp
import Network.Wai
myOpts :: Options
myOpts = Options 1 mySettings
mySettings :: Settings
mySettings = setOnExceptionResponse myHandler $ setPort 3002 $ defaultSettings
myHandler :: SomeException -> Response
myHandler se = responseLBS status500 [] "HERE WE ARE"
someFunc :: IO ()
someFunc = do
scottyOptsT myOpts id routes
myExceptions :: (MonadIO m) => TL.Text -> ActionT TL.Text m ()
myExceptions t = do
liftIO $ print t
html "error"
routes :: (MonadIO m) => ScottyT TL.Text m ()
routes = do
defaultHandler $ \str -> do
liftAndCatchIO $ print str
status status500
json ("welp you thought"::Text)
get "/:here" $ do
liftIO $ ioError $ userError "Hahah"
text "here"
You can then tap further into the Settings type provided by warp so that you could maybe log all the error messages or perform some custom action using the following methods setOnException setOnExceptionResponse.

How do I use wai-handler-devel with a simple wai application

I have the basic "hello world" application setup using wai, and would like to use wai-handler-devel, but am unsure how to go about it and can't find any examples of it in usage on a wai project.
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)
import Data.ByteString.Lazy.Char8 () -- Just for an orphan instance
app :: Application
app _ = return $ responseLBS
status200
[("Content-Type", "text/plain")]
"Hello, World!"
main :: IO ()
main = do
putStrLn $ "http://localhost:8080/"
run 8080 app
What do I need to do to get wai-handler-devel working with a basic wai app?
Note:
There is a fix here ( https://gist.github.com/1499226) i f you run into issues with "wai-handler-devel: command not found"
wai-handler-devel's Hackage page says that it should be invoked from the command-line like so:
$ wai-handler-devel <port> My.App.Module myApp
and that your application's type must look like this:
myApp :: (Application -> IO ()) -> IO ()
In this case, you should define myApp as follows:
myApp :: (Application -> IO ()) -> IO ()
myApp handler = handler app
although you may want to inline app entirely:
myApp :: (Application -> IO ()) -> IO ()
myApp handler = handler $ \_ -> return $ responseLBS
status200
[("Content-Type", "text/plain")]
"Hello, World!"
The type is like this so that you can do initialisation on start-up and the like in IO. I suggest reading the SmallApp and FullApp examples from wai-handler-devel's git repository; the latter is especially helpful, as it has debug output showing the flow of the code during a reload, and shows how to integrate a long-running database connection.
The run script for the FullApp example also shows how to use wai-handler-devel programmatically, including manually specifying Hamlet template dependencies (which the wai-handler-devel command-line tool determines automatically).
You should then be able to rewrite your main as follows:
main :: IO ()
main = do
putStrLn $ "http://localhost:8080/"
myApp (run 8080)
Of course, you could just as easily pass the run function from wai-handler-fastcgi, wai-handler-scgi or even wai-handler-webkit.

How do I perform IO inside a WAI (Warp) Application

I have a simple WAI application (Warp in this case) that responds to all web requests with "Hi". I also want it to display "Said hi" on the server each time a request is processed. How do I perform IO inside my WAI response handler? Here's my application:
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types (status200)
import Network.Wai.Handler.Warp (run)
main :: IO ()
main = do
putStrLn "http://localhost:3000/"
run 3000 app
app :: Application
app _ = return hello
hello = responseLBS status200 [("Content-Type", "text/plain")] "Hi"
The type of a WAI application is:
type Application = Request -> Iteratee ByteString IO Response
This means that a WAI application runs in an Iteratee monad transformer over IO, so you'll have to use liftIO to perform regular IO actions.
import Control.Monad.Trans
app _ = do
liftIO $ putStrLn "Said hi"
return hello

Haskell. Problem with WAI simple web app (unpacking bytestrings)

Did lot of packing, unpacking and etc playing with Strings and Texts and still stuck,
the goal is simple as hello world with extra request info concatenated:
{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.Wai.Handler.Warp (run)
import Network.HTTP.Types (statusOK)
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Char8 (unpack)
import qualified Data.Text.Lazy as T
application req = do
return $ responseLBS statusOK [("Content-Type", "text/plain")]
$ L.pack $ "Hello World" ++ (unpack $ rawPathInfo req)
main = run 3000 application
produces:
Couldn't match expected type `GHC.Word.Word8'
against inferred type `Char'
Expected type: [GHC.Word.Word8]
Inferred type: [Char]
In the second argument of `(++)', namely
`(unpack $ rawPathInfo req)'
In the second argument of `($)', namely
`"Hello World" ++ (unpack $ rawPathInfo req)
Eagerly need hint how to make it work.
Use Data.ByteString.Lazy.Char8.pack instead of L.pack.
That's a lot of packing and unpacking. I don't have wai installed, so I can't test, but something like this should be simpler:
application req = do
return $ responseLBS statusOK [("Content-Type", "text/plain")]
$ L.append "Hello World" $ rawPathInfo req
i.e. simply use ByteString's append rather than String's (++).

Resources