diff --git a/.gitignore b/.gitignore index 849ddff..73012b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ dist/ +config/config.dhall diff --git a/config/config.dhall.sample b/config/config.dhall.sample new file mode 100644 index 0000000..860aedd --- /dev/null +++ b/config/config.dhall.sample @@ -0,0 +1,8 @@ +{ + database = { + username = "username" + , password = "password" + , host = "hostname" + , database = "ebook" + } +} diff --git a/config/devel.dhall b/config/devel.dhall new file mode 100644 index 0000000..83d0368 --- /dev/null +++ b/config/devel.dhall @@ -0,0 +1,10 @@ +-- Devel configuration file, with defaults for local testing environment +-- pre-filled +{ + database = { + username = "postgres" + , password = "devel" + , host = "localhost" + , database = "postgres" + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b32df32 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '2' + +services: + postgres: + image: postgres:9.6.1 + environment: + - POSTGRES_PASSWORD=devel + ports: + - "5432:5432" diff --git a/ebook-manager.cabal b/ebook-manager.cabal index 82d261d..5b09cf9 100644 --- a/ebook-manager.cabal +++ b/ebook-manager.cabal @@ -18,8 +18,9 @@ cabal-version: >=1.10 executable ebook-manager main-is: Main.hs other-modules: Devel.Main - , Server , API + , Configuration + , Server , Types -- other-extensions: build-depends: base >=4.10 && <4.11 @@ -45,5 +46,7 @@ executable ebook-manager , dhall , lucid , servant-lucid + , lens + , generic-lens hs-source-dirs: src default-language: Haskell2010 diff --git a/src/Configuration.hs b/src/Configuration.hs new file mode 100644 index 0000000..dcd6f96 --- /dev/null +++ b/src/Configuration.hs @@ -0,0 +1,19 @@ +{-# Language NoImplicitPrelude #-} +{-# Language DeriveGeneric #-} +{-# Language DuplicateRecordFields #-} +module Configuration where + +import ClassyPrelude +import Dhall (Interpret) + +data Pg = Pg { username :: Text + , password :: Text + , host :: Text + , database :: Text } + deriving (Show, Generic) + + +newtype Config = Config { database :: Pg } deriving (Show, Generic) + +instance Interpret Pg +instance Interpret Config diff --git a/src/Devel/Main.hs b/src/Devel/Main.hs index 8df1b6b..84e8726 100644 --- a/src/Devel/Main.hs +++ b/src/Devel/Main.hs @@ -1,3 +1,4 @@ +{-# Language OverloadedStrings #-} module Devel.Main where import Main (defaultMain) @@ -6,6 +7,7 @@ import Control.Monad (void) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Foreign.Store (Store(..), lookupStore, readStore, storeAction, withStore) import GHC.Word (Word32) +import Dhall (input, auto) update :: IO () update = do @@ -23,7 +25,7 @@ update = do withStore doneStore takeMVar readStore doneStore >>= start start :: MVar () -> IO ThreadId - start done = forkFinally defaultMain (\_ -> putMVar done ()) + start done = forkFinally (input auto "./config/devel.dhall" >>= defaultMain) (\_ -> putMVar done ()) modifyStoredIORef :: Store (IORef a) -> (a -> IO a) -> IO () modifyStoredIORef store f = withStore store $ \ref -> do diff --git a/src/Main.hs b/src/Main.hs index c75a7c4..68be11e 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,11 +1,16 @@ +{-# Language OverloadedStrings #-} module Main where import Server (server) import Network.Wai.Handler.Warp (run) import Types +import Configuration (Config) +import Dhall (input, auto) -defaultMain :: IO () -defaultMain = run 8080 (server App) +defaultMain :: Config -> IO () +defaultMain c = run 8080 (server (App c)) main :: IO () -main = defaultMain +main = do + c <- input auto "./config/config.dhall" + defaultMain c diff --git a/src/Types.hs b/src/Types.hs index af201f3..4b6ce68 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -3,7 +3,8 @@ module Types where import ClassyPrelude import Control.Monad.Logger +import Configuration -data App = App +newtype App = App { config :: Config } type AppM = LoggingT (ReaderT App IO)