From a4f60df0f3b72553380bdd3ca960abf42048ed7e Mon Sep 17 00:00:00 2001 From: Joris Guyonvarch Date: Sat, 8 Aug 2015 19:50:58 +0200 Subject: Getting the hostname and the port in config file --- .gitignore | 1 + config.txt | 2 ++ kimsufi.tar.gz | Bin 0 -> 6173149 bytes sharedCost.cabal | 3 ++ src/server/Application.hs | 19 ++++++----- src/server/Config.hs | 31 +++++++++++++++++ src/server/Main.hs | 85 +++++++++++++++++++++++++--------------------- src/server/View/Page.hs | 10 +++--- 8 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 config.txt create mode 100644 kimsufi.tar.gz create mode 100644 src/server/Config.hs diff --git a/.gitignore b/.gitignore index 788ced7..dcf92d3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ database elm-stuff/ public/javascripts/client.js sessionKey +deploy diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..adf7b84 --- /dev/null +++ b/config.txt @@ -0,0 +1,2 @@ +hostname = localhost:3000 +port = 3000 diff --git a/kimsufi.tar.gz b/kimsufi.tar.gz new file mode 100644 index 0000000..95b93f5 Binary files /dev/null and b/kimsufi.tar.gz differ diff --git a/sharedCost.cabal b/sharedCost.cabal index 8a55707..b1c73eb 100644 --- a/sharedCost.cabal +++ b/sharedCost.cabal @@ -32,3 +32,6 @@ executable sharedCost , uuid == 1.3.10 , email-validate == 2.1.3 , mime-mail == 0.4.9 + , ConfigFile == 1.1.4 + , mtl == 2.2.1 + , lens == 4.12.3 diff --git a/src/server/Application.hs b/src/server/Application.hs index 59aa252..5306e17 100644 --- a/src/server/Application.hs +++ b/src/server/Application.hs @@ -17,7 +17,6 @@ module Application import Web.Scotty import Network.HTTP.Types.Status (ok200, badRequest400) -import Network.Wai (requestHeaderHost) import Database.Persist @@ -29,6 +28,7 @@ import qualified Data.Text.Lazy as TL import qualified Data.Text.Encoding as TE import Data.String (fromString) import Data.Time.Clock (getCurrentTime, diffUTCTime) +import Data.Maybe (isJust) import Text.Email.Validate (isValid) @@ -42,20 +42,21 @@ import Model.Payment import Model.SignIn import Model.Message +import Config + import View.Page (page) import Mail -signInAction :: Text -> ActionM () -signInAction login = +signInAction :: Config -> Text -> ActionM () +signInAction config login = if isValid (TE.encodeUtf8 login) then do maybeUser <- liftIO . runDb $ getUser login - maybeHost <- fmap TE.decodeUtf8 . requestHeaderHost <$> request - case (maybeUser, maybeHost) of - (Just _, Just host) -> do + if isJust maybeUser + then do token <- liftIO . runDb $ createSignInToken login - let url = T.concat ["http://", host ,"/validateSignIn?token=", token] + let url = T.concat ["http://", hostname config, "/validateSignIn?token=", token] let mail = Mail [login] "Sign in" url url maybeSentMail <- liftIO . sendMail $ mail case maybeSentMail of @@ -63,8 +64,8 @@ signInAction login = status ok200 Left _ -> errorResponse "Sorry, we failed to send you the sign up email." - _ -> - errorResponse "You are not authorized to sign in." + else + errorResponse "You are not authorized to sign in." else errorResponse "Please enter a valid email address." diff --git a/src/server/Config.hs b/src/server/Config.hs new file mode 100644 index 0000000..f4144f7 --- /dev/null +++ b/src/server/Config.hs @@ -0,0 +1,31 @@ +{-# LANGUAGE FlexibleContexts #-} + +module Config + ( getConfig + , Config(..) + ) where + +import Data.ConfigFile +import Data.Text (Text) +import qualified Data.Text as T + +import Control.Monad.Trans.Error (runErrorT) +import Control.Monad.IO.Class (liftIO) +import Control.Monad (join) +import Control.Arrow (left) +import Control.Applicative (liftA2) + +data Config = Config + { hostname :: Text + , port :: Int + } deriving (Read, Eq, Show) + +getConfig :: FilePath -> IO (Either String Config) +getConfig filePath = + left show <$> (runErrorT $ do + cp <- join $ liftIO $ readfile emptyCP filePath + liftA2 + Config + (T.pack <$> get cp "DEFAULT" "hostname") + (get cp "DEFAULT" "port") + ) diff --git a/src/server/Main.hs b/src/server/Main.hs index 2ae319b..3033f58 100644 --- a/src/server/Main.hs +++ b/src/server/Main.hs @@ -10,44 +10,51 @@ import Application import Model.Database (runMigrations) +import Config + main :: IO () main = do - runMigrations - scotty 3000 $ do - - middleware $ - staticPolicy (noDots >-> addBase "public") - - get "/" $ - getIndexAction - - post "/signIn" $ do - login <- param "login" :: ActionM Text - signInAction login - - get "/validateSignIn" $ do - token <- param "token" :: ActionM Text - validateSignInAction token - - get "/userName" $ - getUserName - - get "/payments" $ - getPaymentsAction - - post "/payment/add" $ do - name <- param "name" :: ActionM Text - cost <- param "cost" :: ActionM Int - createPaymentAction name cost - - post "/signOut" $ - signOutAction - - get "/users" getUsersAction - post "/user/add" $ do - email <- param "email" :: ActionM Text - name <- param "name" :: ActionM Text - addUserAction email name - post "/user/delete" $ do - email <- param "email" :: ActionM Text - deleteUserAction email + config <- getConfig "config.txt" + case config of + Left error -> + putStrLn error + Right config -> do + runMigrations + scotty (port config) $ do + + middleware $ + staticPolicy (noDots >-> addBase "public") + + get "/" $ + getIndexAction + + post "/signIn" $ do + login <- param "login" :: ActionM Text + signInAction config login + + get "/validateSignIn" $ do + token <- param "token" :: ActionM Text + validateSignInAction token + + get "/userName" $ + getUserName + + get "/payments" $ + getPaymentsAction + + post "/payment/add" $ do + name <- param "name" :: ActionM Text + cost <- param "cost" :: ActionM Int + createPaymentAction name cost + + post "/signOut" $ + signOutAction + + get "/users" getUsersAction + post "/user/add" $ do + email <- param "email" :: ActionM Text + name <- param "name" :: ActionM Text + addUserAction email name + post "/user/delete" $ do + email <- param "email" :: ActionM Text + deleteUserAction email diff --git a/src/server/View/Page.hs b/src/server/View/Page.hs index b5a80e2..a397a96 100644 --- a/src/server/View/Page.hs +++ b/src/server/View/Page.hs @@ -20,10 +20,10 @@ page = H.head $ do meta ! charset "UTF-8" H.title "Shared Cost" - script ! src "/javascripts/client.js" $ "" - link ! rel "stylesheet" ! type_ "text/css" ! href "/css/reset.css" - link ! rel "stylesheet" ! href "/css/font-awesome/css/font-awesome.min.css" - link ! rel "icon" ! type_ "image/png" ! href "/images/icon.png" + script ! src "javascripts/client.js" $ "" + link ! rel "stylesheet" ! type_ "text/css" ! href "css/reset.css" + link ! rel "stylesheet" ! href "css/font-awesome/css/font-awesome.min.css" + link ! rel "icon" ! type_ "image/png" ! href "images/icon.png" H.style $ toHtml globalDesign body $ - script ! src "/javascripts/elmLauncher.js" $ "" + script ! src "javascripts/elmLauncher.js" $ "" -- cgit v1.2.3