{-# LANGUAGE OverloadedLists #-}

module BB8 where

import qualified Control.Concurrent as Concurrent
import qualified Control.Concurrent.STM as STM
import qualified Database.SQLite.Simple as SQL
import qualified Web.Scotty as Web

import qualified BB8.Common as BB8
import qualified BB8.Types as Types
import qualified BB8.Config as Config
import qualified BB8.Server as Server


startup :: String
startup = "\n\
          \   |_)|_)(_) is now\n\
          \   |_)|_)(_) online!"

main :: IO ()
main = do
  BB8.debug startup
  conf <- Config.getConfig
  trace <- STM.atomically (STM.newTVar mempty)
  SQL.withConnection (Types.configDBPath conf) $ \db -> do
    jobState <- STM.atomically (STM.newTVar Nothing)
    let state = Server.AppState
          { Server.appJobState = jobState
          , Server.appJobTrace = trace
          , Server.appDBHandle = db
          , Server.appConfig   = conf
          }
    _ <- Concurrent.forkIO (Server.jobRunnerLoop state)
    Web.scotty 8080 (Server.app state)
