aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 204e4d79625bcea31d8c33b30a6cd1829ccc8352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE RecordWildCards #-}

-- | The main module. Does little more than handle some basic ocnfic, then
-- call the server
module Main where

import           Conferer                             (fetch)
import           Conferer.Config                      (addSource, emptyConfig)
import qualified Conferer.Source.Aeson                as ConfAeson
import qualified Conferer.Source.CLIArgs              as ConfCLI
import qualified Conferer.Source.Env                  as ConfEnv
import qualified Conferer.Source.Yaml                 as ConfYaml
import           Control.Monad.Extra                  (ifM)
import           Control.Monad.IO.Class               (MonadIO (liftIO))
import           Control.Monad.Logger                 (runStderrLoggingT)
import           Data.Default.Class                   (def)
import           Database.Persist.Postgresql          (withPostgresqlPool)
import           Network.Wai.Handler.Warp             (runSettings)
import           Network.Wai.Middleware.RequestLogger (OutputFormat (..),
                                                       RequestLoggerSettings (..),
                                                       mkRequestLogger)
import           System.Directory                     (doesFileExist)

import           Config                               (ServerConfig (..))
import           GTFS                                 (loadGtfs)
import           Server                               (application)

main :: IO ()
main = do
  confconfig <- pure emptyConfig
    >>= addSource ConfCLI.fromConfig
    -- for some reason the yaml source fails if the file does not exist, but json works fine
    >>= (\c -> ifM (doesFileExist "./config.yaml")
          (addSource (ConfYaml.fromFilePath "./config.yaml") c)
          (pure c))
    >>= addSource (ConfAeson.fromFilePath "./config.json")
    >>= addSource (ConfEnv.fromConfig "tracktrain")

  ServerConfig{..} <- fetch confconfig

  gtfs <- loadGtfs serverConfigGtfs serverConfigZoneinfoPath
  loggerMiddleware <- mkRequestLogger
    $ def { outputFormat = Detailed True }
  runStderrLoggingT $ withPostgresqlPool serverConfigDbString 10 $ \pool -> liftIO $ do
    app <- application gtfs pool
    putStrLn "starting server …"
    runSettings serverConfigWarp (loggerMiddleware app)