add configuration options

This commit is contained in:
nek0 2019-10-27 22:45:07 +01:00
parent f1c9d4282e
commit 0e32c7ae98
5 changed files with 121 additions and 18 deletions

5
app/AppTypes.hs Normal file
View file

@ -0,0 +1,5 @@
module AppTypes
( module T
) where
import AppTypes.Configuration as T

View file

@ -0,0 +1,45 @@
{-# LANGUAGE OverloadedStrings #-}
module AppTypes.Configuration where
import qualified Data.Text as T
import Data.YAML as Y
import Options.Applicative as O
data ServerConfig = ServerConfig
{ configDbHost :: T.Text
, configDbPort :: Word
, configDbName :: T.Text
, configDbUser :: T.Text
, configDbPasswd :: T.Text
, configCurrencySymbol :: T.Text
, configListenPort :: Word
, configListenHost :: T.Text
}
deriving (Show)
instance FromYAML ServerConfig where
parseYAML = withMap "Configuration" $ \m -> ServerConfig
<$> m .: "db_host"
<*> m .: "db_port"
<*> m .: "db_name"
<*> m .: "db_user"
<*> m .: "db_passwd"
<*> m .: "currency"
<*> m .: "listen_port"
<*> m .:? "listen_host" .!= "127.0.0.1"
newtype Options = Options
{ optConfigLocation :: T.Text
}
deriving (Show)
options :: O.Parser Options
options = Options
<$> strOption
( long "configuration"
<> short 'c'
<> metavar "FILEPATH"
<> help "Location of the configuration YAML-file"
)

View file

@ -8,7 +8,11 @@ module Main where
import Servant import Servant
import Servant.Server.Experimental.Auth import Servant.Server.Experimental.Auth
import Data.Set (empty) import Data.Set as S (empty)
import Data.ByteString.Lazy as BL hiding (putStrLn)
import qualified Data.Text as T
import Data.String
import Data.YAML
import Database.PostgreSQL.Simple import Database.PostgreSQL.Simple
@ -20,33 +24,68 @@ import Control.Monad.Reader
import Control.Concurrent.STM.TVar import Control.Concurrent.STM.TVar
import Options.Applicative
-- internal imports -- internal imports
import API import API
import Model as M import Model as M
import AppTypes
import Types import Types
import Control import Control
main :: IO () main :: IO ()
main = do main = do
conn <- connectPostgreSQL (Options confLoc) <- execParser opts
"host='localhost' port=5432 dbname='mateamt' user='mateamt' password='mateamt'" raw <- BL.readFile (T.unpack confLoc)
store <- newTVarIO empty case decode1 raw of
void $ execute_ conn initAvatar Left (loc, msg) ->
void $ execute_ conn initUser error (T.unpack $ confLoc <> ":" <>
void $ execute_ conn initProduct fromString (prettyPosWithSource loc raw " error") <>
void $ execute_ conn initToken fromString msg
void $ execute_ conn initAuthData )
void $ execute_ conn initAmount Right
void $ execute_ conn initJournal (ServerConfig
withStdoutLogger $ \ilog -> do db_host
let settings = setPort 8000 $ setLogger ilog defaultSettings db_port
initState = ReadState db_name
{ rsConnection = conn db_user
, rsTicketStore = store db_passwd
} sym
runSettings settings (app initState) lport
lhost
) -> do
conn <- connectPostgreSQL (
"host='" <> fromString (T.unpack db_host) <> "' " <>
"port=" <> fromString (show db_port) <> " " <>
"dbname='" <> fromString (T.unpack db_name) <> "' " <>
"user='" <> fromString (T.unpack db_user) <> "' " <>
"password='" <> fromString (T.unpack db_passwd) <> "'"
)
store <- newTVarIO S.empty
void $ execute_ conn initAvatar
void $ execute_ conn initUser
void $ execute_ conn initProduct
void $ execute_ conn initToken
void $ execute_ conn initAuthData
void $ execute_ conn initAmount
void $ execute_ conn initJournal
withStdoutLogger $ \ilog -> do
let settings = setPort (fromIntegral lport) $
setHost (fromString $ T.unpack lhost) $
setLogger ilog defaultSettings
initState = ReadState
{ rsConnection = conn
, rsTicketStore = store
}
runSettings settings (app initState)
where
opts = info (options <**> helper)
( fullDesc
<> progDesc "Run the \"mateamt\" API-Server."
<> header "mateamt - Your friendly mate distribution office"
)
app :: ReadState -> Application app :: ReadState -> Application
-- app conn = serveWithContext userApi genAuthServerContext (users conn) -- app conn = serveWithContext userApi genAuthServerContext (users conn)

8
config.yaml Normal file
View file

@ -0,0 +1,8 @@
db_host: "localhost"
db_port: 5432
db_name: "mateamt"
db_user: "mateamt"
db_passwd: "mateamt"
listen_port: 8000
#listen_host: "127.0.0.1"
currency: "€"

View file

@ -22,9 +22,13 @@ flag develop
executable mateamt executable mateamt
main-is: Main.hs main-is: Main.hs
other-modules: AppTypes
, AppTypes.Configuration
-- other-extensions: -- other-extensions:
build-depends: base ^>=4.12.0.0 build-depends: base ^>=4.12.0.0
, mateamt , mateamt
, text
, bytestring
, base16-bytestring , base16-bytestring
, containers , containers
, mtl , mtl
@ -37,6 +41,8 @@ executable mateamt
, warp , warp
, wai , wai
, wai-logger , wai-logger
, HsYAML
, optparse-applicative
hs-source-dirs: app hs-source-dirs: app
ghc-options: -Wall ghc-options: -Wall
default-language: Haskell2010 default-language: Haskell2010