aboutsummaryrefslogtreecommitdiff
path: root/lib/Config.hs
blob: 363a0686299f9aa7c0727f88536048d5e0a4f074 (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
51
52
53
54
{-# LANGUAGE DeriveGeneric   #-}
{-# LANGUAGE RecordWildCards #-}
-- |

module Config where
import           Conferer                 (DefaultConfig (configDef))
import           Conferer.FromConfig
import           Conferer.FromConfig.Warp ()
import           Data.ByteString          (ByteString)
import           Data.Text                (Text)
import           GHC.Generics             (Generic)
import           Network.Wai.Handler.Warp (Settings)
import           URI.ByteString

data UffdConfig = UffdConfig
  { uffdConfigUrl          :: URIRef Absolute
  , uffdConfigClientSecret :: Text
  , uffdConfigClientName   :: Text
  , uffdConfigEnable       :: Bool
  } deriving (Generic, Show)

data ServerConfig = ServerConfig
  { serverConfigWarp         :: Settings
  , serverConfigDbString     :: ByteString
  , serverConfigGtfs         :: FilePath
  , serverConfigAssets       :: FilePath
  , serverConfigZoneinfoPath :: FilePath
  , serverConfigLogin        :: UffdConfig
  } deriving (Generic)

instance FromConfig ServerConfig

instance DefaultConfig ServerConfig where
  configDef = ServerConfig
    { serverConfigWarp = configDef
    , serverConfigDbString = ""
    , serverConfigGtfs = "./gtfs.zip"
    , serverConfigAssets = "./assets"
    , serverConfigZoneinfoPath = "/etc/zoneinfo/"
    , serverConfigLogin = configDef
    }

instance DefaultConfig UffdConfig where
  configDef = UffdConfig uri "secret" "uffdclient" False
    where Right uri = parseURI strictURIParserOptions "http://www.example.org"

instance FromConfig UffdConfig where
  fromConfig key config = do
    url <- fetchFromConfig (key /. "url") config
    let Right uffdConfigUrl = parseURI strictURIParserOptions url
    uffdConfigClientName <- fetchFromConfig (key /. "clientName") config
    uffdConfigClientSecret <- fetchFromConfig (key /. "clientSecret") config
    uffdConfigEnable <- fetchFromConfig (key /. "enable") config
    pure UffdConfig {..}