summaryrefslogtreecommitdiff
path: root/fig-frontend/src/Fig/Frontend/Utils.hs
blob: 1ba1d5f100474705cfee18c6a4b6256bcccf04ef (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
{-# Language RecordWildCards #-}
{-# Language ApplicativeDo #-}

module Fig.Frontend.Utils
  ( FigFrontendException(..)
  , loadConfig
  , Config(..)
  , module Network.HTTP.Types.Status
  ) where

import Fig.Prelude

import Network.HTTP.Types.Status

import qualified Toml

newtype FigFrontendException = FigFrontendException Text
  deriving (Show, Eq, Ord)
instance Exception FigFrontendException

data Config = Config
  { port :: Int
  , assetPath :: FilePath
  , clientId :: Text
  } deriving (Show, Eq, Ord)

configCodec :: Toml.TomlCodec Config
configCodec = do
  port <- Toml.int "port" Toml..= (\a -> a.port)
  assetPath <- Toml.string "asset_path" Toml..= (\a -> a.assetPath)
  clientId <- Toml.text "client_id" Toml..= (\a -> a.clientId)
  pure $ Config{..}

loadConfig :: FilePath -> IO Config
loadConfig path = Toml.decodeFileEither configCodec path >>= \case
  Left err -> throwM . FigFrontendException $ tshow err
  Right config -> pure config