I have written my medium-sized Haskell app with hard-coded config variables (like Google OAuth ClientId & ClientSecret). Now that I'm prepping the app for a production deployment, I need to move all these config variable out of the source, to either: (a) environment variables, or (b) a plain-text config file.
Here's what the code, currently, look likes:
googleClientId :: T.Text
googleClientId = "redacted"
googleClientSecret :: T.Text
googleClientSecret = "redacted"
generateOAuthUserCode :: IO (OAuthCodeResponse)
generateOAuthUserCode = do
r <- asJSON =<< post "https://accounts.google.com/o/oauth2/device/code" ["client_id" := googleClientId, "scope" := ("email profile" :: T.Text)]
return $ r ^. responseBody
What's the fastest/easiest way to get googleClientId
and googleClientSecret
from an environment variable (or config file)? I tried the following:
googleClientId :: T.Text
googleClientId = undefined
googleClientSecret :: T.Text
googleClientSecret = undefined
main :: IO()
main = do
googleClientId <- getEnv "GOOGLE_CLIENT_ID"
googleClientSecret <- getENV "GOOGLE_CLIENT_SECRET"
-- Start the main app, which internally will call generateOAuthUserCode at some point.
The expectation was that the global googleClientId
and googleClientSecret
will be re-bound, but my editor immediately started showing a warning that "the binding shadows an existing binding", indicating that Haskell is creating a new binding, instead of changing the existing one.
So, two questions here:
- First, the pragmatic one. How to solve the problem at hand, without getting into the Reader monad, which might involve changing a lot of function signatures across my app.
- Second, the one oriented to learning. Haskell has immutable values, which is understood and appreciated. Does it even have immutable variable binding? Is it not possible to get dynamic variable bindings, like in Common Lisp?
Edit: What about the following approach?
what about the following approach?
outerFunc :: String -> String -> IO ()
outerFunc googleClientId googleClientSecret = do
-- more code comes here
where
generateOAuthUserCode :: IO (OAuthCodeResponse)
generateOAuthUserCode = do
r <- asJSON =<< post "https://accounts.google.com/o/oauth2/device/code" ["client_id" := googleClientId, "scope" := ("email profile" :: T.Text)]
return $ r ^. responseBody
-- more functions depending upon the config variables
googleClientId :: ...
andgoogleClientSecret :: ...
(both definitions) completely, then look where the compiler is complaining about the missing items, move the identifiers into arguments in the so found functions and then provide the arguments where you have them from either arguments you already introduced or from the local values in yourmain
... TL;DR: make the configuration into arguments instead of global valuesgenerateOAuthUserCode
is already anIO
action, you could just move thegetEnv
into thedo
block there, I think. Or is it just an example for lots of other global variables throughout your codebase? Then read my answer.