about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-31T17·26+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-31T17·28+0100
commitc8ed6e51fea30ea2f79cca058c4f161625ab6a85 (patch)
treed49ee9bd7b1b5b5754c84b80f474f69891549f2f
parent1d7c77f51d287c9d636630142791952890d17622 (diff)
Read CLIENT and SERVER endpoints from .envrc
In the spirit of DRY.
-rw-r--r--src/App.hs39
-rw-r--r--src/Types.hs4
2 files changed, 24 insertions, 19 deletions
diff --git a/src/App.hs b/src/App.hs
index e3806610aa5b..ff292ff2cd16 100644
--- a/src/App.hs
+++ b/src/App.hs
@@ -41,12 +41,13 @@ err429 = ServerError
   }
 
 -- | Send an email to recipient, `to`, with a secret code.
-sendVerifyEmail :: Text
-             -> T.Username
-             -> T.Email
-             -> T.RegistrationSecret
-             -> IO (Either Email.SendError Email.SendSuccess)
-sendVerifyEmail apiKey (T.Username username) email@(T.Email to) (T.RegistrationSecret secretUUID) = do
+sendVerifyEmail :: T.Config
+                -> Text
+                -> T.Username
+                -> T.Email
+                -> T.RegistrationSecret
+                -> IO (Either Email.SendError Email.SendSuccess)
+sendVerifyEmail T.Config{..} apiKey (T.Username username) email@(T.Email to) (T.RegistrationSecret secretUUID) = do
   Email.send apiKey subject (cs body) email
   where
     subject = "Please confirm your account"
@@ -54,20 +55,20 @@ sendVerifyEmail apiKey (T.Username username) email@(T.Email to) (T.RegistrationS
     -- TODO(wpcarro): Use a dynamic domain and port number
     body =
       let secret = secretUUID |> UUID.toString in
-        "http://localhost:3000/verify?username=" ++ cs username ++ "&secret=" ++ secret
+        cs configServer ++ cs username ++ "&secret=" ++ secret
 
 server :: T.Config -> Server API
-server T.Config{..} = createAccount
-                 :<|> verifyAccount
-                 :<|> deleteAccount
-                 :<|> listAccounts
-                 :<|> createTrip
-                 :<|> updateTrip
-                 :<|> deleteTrip
-                 :<|> listTrips
-                 :<|> login
-                 :<|> logout
-                 :<|> unfreezeAccount
+server config@T.Config{..} = createAccount
+                        :<|> verifyAccount
+                        :<|> deleteAccount
+                        :<|> listAccounts
+                        :<|> createTrip
+                        :<|> updateTrip
+                        :<|> deleteTrip
+                        :<|> listTrips
+                        :<|> login
+                        :<|> logout
+                        :<|> unfreezeAccount
   where
     -- Admit Admins + whatever the predicate `p` passes.
     adminsAnd cookie p = Auth.assert dbFile cookie (\acct@T.Account{..} -> accountRole == T.Admin || p acct)
@@ -84,7 +85,7 @@ server T.Config{..} = createAccount
         createAccountRequestPassword
         createAccountRequestRole
         createAccountRequestEmail
-      liftIO $ sendVerifyEmail mailgunAPIKey
+      liftIO $ sendVerifyEmail config mailgunAPIKey
         createAccountRequestUsername
         createAccountRequestEmail
         secretUUID
diff --git a/src/Types.hs b/src/Types.hs
index 7bfdf6cfd000..f47e1419757a 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -33,12 +33,16 @@ import qualified Data.UUID as UUID
 data Config = Config
   { mailgunAPIKey :: Text
   , dbFile :: FilePath
+  , configClient :: Text
+  , configServer :: Text
   } deriving (Eq, Show)
 
 instance FromEnv Config where
   fromEnv _ = do
     mailgunAPIKey <- env "MAILGUN_API_KEY"
     dbFile <- env "DB_FILE"
+    configClient <- env "CLIENT"
+    configServer <- env "SERVER"
     pure Config {..}
 
 -- TODO(wpcarro): Properly handle NULL for columns like profilePicture.