module Main where

import qualified Data.IntMap as IntMap
import Control.Concurrent.STM.TVar
import Network.Wai.Handler.Warp
import System.Random

import Quiz.Config
import Quiz.Types
import Quiz.Web

main :: IO ()
main = do
    -- Get settings from command-line arguments
    (port, preQuizMap) <- getConfig
    -- Initialize quiz index
    quizMap <- newTVarIO preQuizMap
    -- Initialize session state
    sessionMap <- newTVarIO IntMap.empty
    -- Initialize random number generator
    stdGen <- newTVarIO =<< getStdGen
    -- Start web server
    putStrLn $ "Starting web server on port " ++ show port ++ "."
    run port $ quizApplication sessionMap quizMap stdGen
