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:
parent
385164c6af
commit
b6e8389edd
5 changed files with 33 additions and 13 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
|
.envrc
|
||||||
*.db
|
*.db
|
||||||
*.sqlite3
|
*.sqlite3
|
||||||
!populate.sqlite3
|
!populate.sqlite3
|
||||||
|
|
|
@ -10,6 +10,7 @@ in pkgs.mkShell {
|
||||||
hpkgs.warp
|
hpkgs.warp
|
||||||
hpkgs.cryptonite
|
hpkgs.cryptonite
|
||||||
hpkgs.uuid
|
hpkgs.uuid
|
||||||
|
hpkgs.envy
|
||||||
]))
|
]))
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
24
src/App.hs
24
src/App.hs
|
@ -37,15 +37,15 @@ 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
|
||||||
:<|> deleteTrip
|
:<|> deleteTrip
|
||||||
:<|> listTrips
|
:<|> listTrips
|
||||||
:<|> login
|
:<|> login
|
||||||
:<|> logout
|
:<|> logout
|
||||||
where
|
where
|
||||||
-- Admit Admins + whatever the predicate `p` passes.
|
-- Admit Admins + whatever the predicate `p` passes.
|
||||||
adminsAnd cookie p = Auth.assert dbFile cookie (\acct@T.Account{..} -> accountRole == T.Admin || p acct)
|
adminsAnd cookie p = Auth.assert dbFile cookie (\acct@T.Account{..} -> accountRole == T.Admin || p acct)
|
||||||
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
12
src/Types.hs
12
src/Types.hs
|
@ -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 =
|
||||||
|
|
Loading…
Reference in a new issue