aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: a61140abde4afd2b5a35e9fdd20275f3e5dbb8b2 (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
48
49
50
{-# 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
    >>= addSource (ConfEnv.fromConfig "tracktrain")
    -- 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))
    >>= (\c -> ifM (doesFileExist "./config.yml")
          (addSource (ConfYaml.fromFilePath "./config.yml") c)
          (pure c))
    >>= addSource (ConfAeson.fromFilePath "./config.json")

  settings@ServerConfig{..} <- fetch confconfig

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