aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 3856a6772779091ed7a9dfa6c7c982cfd2aed5f9 (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 OverloadedLists #-}
{-# LANGUAGE QuasiQuotes     #-}
{-# LANGUAGE RecordWildCards #-}

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

import           Conftrack
import           Conftrack.Pretty
import           Conftrack.Source.Env                 (mkEnvSource)
import           Conftrack.Source.Yaml                (mkYamlFileSource)
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           System.OsPath                        (osp)

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

main :: IO ()
main = do

  Right ymlsource <- mkYamlFileSource [osp|./config.yaml|]

  Right (settings@ServerConfig{..}, origins, warnings) <-
    runFetchConfig [mkEnvSource "tracktrain", ymlsource]

  putStrLn "reading configs .."
  printConfigOrigins origins
  printConfigWarnings warnings

  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)