content: {-# LANGUAGE OverloadedStrings #-}
  
  import Web.Scotty
  import Network.Wai.Middleware.RequestLogger (logStdoutDev)
  import System.Random (randomRIO)
  import Control.Monad.IO.Class (liftIO)
  import Data.Text.Lazy (Text, pack, unpack)
  
  -- List of random advices
  -- From https://api.adviceslip.com/advice
  advices :: [Text]
  advices =
      [ "It always seems impossible, until it's done."
      , "To cleanly remove the seed from an Avocado, lay a knife firmly across it, and twist."
      , "Fail. Fail again. Fail better."
      , "Play is the true mother of invention."
      , "Remedy tickly coughs with a drink of honey, lemon and water as hot as you can take."
      ]
  
  -- Function to get a random advice
  getRandomAdvice :: IO Text
  getRandomAdvice = do
      index <- randomRIO (0, length advices - 1)
      -- Illustrate conversions: Text <-> String
      -- For a better solution, see https://hackage.haskell.org/package/json
      let advice = advices !! index
          responseString = "{\"advice\": \"" ++ unpack advice ++ "\"}"
          responseText = pack responseString
      return responseText
      -- Shorter version, without JSON formatting:
      -- return $ advices !! index
  
  main :: IO ()
  main = scotty 3000 $ do
      middleware logStdoutDev
      get "/advice" $ do
          randomAdvice <- liftIO getRandomAdvice
          setHeader "Content-Type" "application/json"
          text randomAdvice
  
  