Search code examples
haskellscottyhaskell-warphaskell-wai

Simple questions about the scotty Haskell web framework


Consider the simplest scotty app:

{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty

import Data.Monoid (mconcat)

main = scotty 3000 $ do
    get "/:word" $ do
        beam <- param "word"
        html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]

I put this code into app.hs and compile it with GHC. I run it with ./app. Simple.

  1. What really happens when people visit the site? It's only one ./app that's running. Does a new thread get created within this same app whenever each user triggers a get "/:word" $ do line? How many such threads can exist? Thousand? Ten thousand?

  2. After running ./app it shows the message Setting phasers to stun... (port 3000) (ctrl-c to quit). But it shows nothing more. It doesn't output the incoming web requests. How can I make it do that? This would be useful for logging purposes.


Solution

  • Assuming you are using GHC, each request to the scotty server essentially creates a "green thread" that is scheduled by the GHC runtime. You can easily have thousands of those running at a time.

    Scotty itself doesn't do any request logging but since it is built on top of WAI, you can use any middleware component that exists for it, for example RequestLogger.

    {-# LANGUAGE OverloadedStrings #-}
    import Web.Scotty
    import Network.Wai.Middleware.RequestLogger
    
    import Data.Monoid (mconcat)
    
    main = scotty 3000 $ do
        middleware logStdoutDev
    
        get "/:word" $ do
            beam <- param "word"
            html $ mconcat ["<h1>Scotty, ", beam, " me up!</h1>"]