Read env variables using envy library

Using my dear friend's, dmjio's, excellent library, envy -- to read and parse
variables from the system environment.

I added and git-ignored the .envrc file that contains API secrets. I'm using
Envy to read these values, so that I don't hard-code these values into the
source code.
This commit is contained in:
William Carroll 2020-07-30 13:58:50 +01:00
parent 385164c6af
commit b6e8389edd
5 changed files with 33 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
.envrc
*.db *.db
*.sqlite3 *.sqlite3
!populate.sqlite3 !populate.sqlite3

View file

@ -10,6 +10,7 @@ in pkgs.mkShell {
hpkgs.warp hpkgs.warp
hpkgs.cryptonite hpkgs.cryptonite
hpkgs.uuid hpkgs.uuid
hpkgs.envy
])) ]))
]; ];
} }

View file

@ -37,8 +37,8 @@ err429 = ServerError
, errHeaders = [] , errHeaders = []
} }
server :: FilePath -> Server API server :: T.Config -> Server API
server dbFile = createAccount server T.Config{..} = createAccount
:<|> deleteAccount :<|> deleteAccount
:<|> listAccounts :<|> listAccounts
:<|> createTrip :<|> createTrip
@ -124,6 +124,6 @@ server dbFile = createAccount
liftIO $ Sessions.delete dbFile uuid liftIO $ Sessions.delete dbFile uuid
pure $ addHeader Auth.emptyCookie NoContent pure $ addHeader Auth.emptyCookie NoContent
run :: FilePath -> IO () run :: T.Config -> IO ()
run dbFile = run config =
Warp.run 3000 (serve (Proxy @ API) $ server dbFile) Warp.run 3000 (serve (Proxy @ API) $ server config)

View file

@ -1,7 +1,13 @@
--------------------------------------------------------------------------------
module Main where module Main where
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
import qualified App import qualified App
import qualified System.Envy as Envy
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
main :: IO () main :: IO ()
main = App.run "../db.sqlite3" main = do
mEnv <- Envy.decodeEnv
case mEnv of
Left err -> putStrLn err
Right env -> App.run env

View file

@ -16,6 +16,7 @@ import Database.SQLite.Simple.ToField
import GHC.Generics import GHC.Generics
import Web.Cookie import Web.Cookie
import Servant.API import Servant.API
import System.Envy (FromEnv, fromEnv, env)
import Crypto.Random.Types (MonadRandom) import Crypto.Random.Types (MonadRandom)
import qualified Crypto.KDF.BCrypt as BC import qualified Crypto.KDF.BCrypt as BC
@ -26,6 +27,17 @@ import qualified Data.Text.Encoding as TE
import qualified Data.UUID as UUID import qualified Data.UUID as UUID
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- | Top-level application configuration.
data Config = Config
{ mailgunAPIKey :: Text
, dbFile :: FilePath
} deriving (Eq, Show)
instance FromEnv Config where
fromEnv _ =
Config <$> env "MAILGUN_API_KEY"
<*> env "DB_FILE"
-- TODO(wpcarro): Properly handle NULL for columns like profilePicture. -- TODO(wpcarro): Properly handle NULL for columns like profilePicture.
forNewtype :: (Typeable b) => (Text -> b) -> FieldParser b forNewtype :: (Typeable b) => (Text -> b) -> FieldParser b
forNewtype wrapper field = forNewtype wrapper field =