From c65053ec641d05da774a19bf3273290b7e63e265 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Mon, 12 Mar 2012 15:35:43 +0200 Subject: [PATCH] Simplified host/port/approot settings, solves #274 --- yesod-default/Yesod/Default/Config.hs | 38 +++++++-------------------- yesod-default/Yesod/Default/Main.hs | 4 ++- yesod-default/yesod-default.cabal | 19 +++++++------- yesod/scaffold/config/settings.yml.cg | 2 ++ yesod/scaffold/project.cabal.cg | 18 ++++++------- yesod/scaffold/tiny/project.cabal.cg | 14 +++++----- 6 files changed, 41 insertions(+), 54 deletions(-) diff --git a/yesod-default/Yesod/Default/Config.hs b/yesod-default/Yesod/Default/Config.hs index 5a6bb88b..dfc10a68 100644 --- a/yesod-default/Yesod/Default/Config.hs +++ b/yesod-default/Yesod/Default/Config.hs @@ -22,6 +22,8 @@ import Data.Maybe (fromMaybe) import qualified Data.HashMap.Strict as M import System.Environment (getArgs, getProgName, getEnvironment) import System.Exit (exitFailure) +import Data.Conduit.Network (HostPreference) +import Data.String (fromString) -- | A yesod-provided @'AppEnv'@, allows for Development, Testing, and -- Production environments @@ -97,6 +99,7 @@ data AppConfig environment extra = AppConfig { appEnv :: environment , appPort :: Int , appRoot :: Text + , appHost :: HostPreference , appExtra :: extra } deriving (Show) @@ -142,15 +145,14 @@ configSettings env0 = ConfigSettings -- > host: localhost -- > port: 3000 -- > --- > -- ssl: will default false --- > -- approot: will default to "http://localhost:3000" +-- > -- approot: will default to "" -- -- > -- typical outward-facing production box -- > Production: -- > host: www.example.com -- > --- > -- ssl: will default false -- > -- port: will default 80 +-- > -- host: will default to "*" -- > -- approot: will default "http://www.example.com" -- -- > -- maybe you're reverse proxying connections to the running app @@ -158,10 +160,7 @@ configSettings env0 = ConfigSettings -- > Production: -- > port: 8080 -- > approot: "http://example.com" --- > --- > -- approot is specified so that the non-80 port is not appended --- > -- automatically. --- +-- > host: "localhost" loadConfig :: ConfigSettings environment extra -> IO (AppConfig environment extra) loadConfig (ConfigSettings env parseExtra getFile getObject) = do @@ -174,30 +173,20 @@ loadConfig (ConfigSettings env parseExtra getFile getObject) = do Object m -> return m _ -> fail "Expected map" - let mssl = lookupScalar "ssl" m - let mhost = lookupScalar "host" m + let host = fromString $ T.unpack $ fromMaybe "*" $ lookupScalar "host" m mport <- parseMonad (\x -> x .: "port") m - let mapproot = lookupScalar "approot" m + let approot = fromMaybe "" $ lookupScalar "approot" m extra <- parseMonad (parseExtra env) m -- set some default arguments - let ssl = maybe False toBool mssl - let port' = fromMaybe (if ssl then 443 else 80) mport - - approot <- case (mhost, mapproot) of - (_ , Just ar) -> return ar - (Just host, _ ) -> return $ T.concat - [ if ssl then "https://" else "http://" - , host - , addPort ssl port' - ] - _ -> fail "You must supply either a host or approot" + let port' = fromMaybe 80 mport return $ AppConfig { appEnv = env , appPort = port' , appRoot = approot + , appHost = host , appExtra = extra } @@ -207,13 +196,6 @@ loadConfig (ConfigSettings env parseExtra getFile getObject) = do Just (String t) -> return t Just _ -> fail $ "Invalid value for: " ++ show k Nothing -> fail $ "Not found: " ++ show k - toBool :: Text -> Bool - toBool = (`elem` ["true", "TRUE", "yes", "YES", "Y", "1"]) - - addPort :: Bool -> Int -> Text - addPort True 443 = "" - addPort False 80 = "" - addPort _ p = T.pack $ ':' : show p -- | Loads the configuration block in the passed file named by the -- passed environment, yeilds to the passed function as a mapping. diff --git a/yesod-default/Yesod/Default/Main.hs b/yesod-default/Yesod/Default/Main.hs index 9f560109..9eb131ca 100644 --- a/yesod-default/Yesod/Default/Main.hs +++ b/yesod-default/Yesod/Default/Main.hs @@ -10,7 +10,7 @@ import Yesod.Default.Config import Yesod.Logger (Logger, defaultDevelopmentLogger, logString) import Network.Wai (Application) import Network.Wai.Handler.Warp - (runSettings, defaultSettings, settingsPort) + (runSettings, defaultSettings, settingsPort, settingsHost) import System.Directory (doesDirectoryExist, removeDirectoryRecursive) import Network.Wai.Middleware.Gzip (gzip, GzipFiles (GzipCacheFolder), gzipFiles, def) import Network.Wai.Middleware.Autohead (autohead) @@ -45,8 +45,10 @@ defaultMain load getApp = do config <- load logger <- defaultDevelopmentLogger app <- getApp config logger + print $ appHost config runSettings defaultSettings { settingsPort = appPort config + , settingsHost = appHost config } app -- | Run your application continously, listening for SIGINT and exiting diff --git a/yesod-default/yesod-default.cabal b/yesod-default/yesod-default.cabal index a92bf844..99ec7887 100644 --- a/yesod-default/yesod-default.cabal +++ b/yesod-default/yesod-default.cabal @@ -17,19 +17,20 @@ library if os(windows) cpp-options: -DWINDOWS - build-depends: base >= 4 && < 5 - , yesod-core >= 1.0 && < 1.1 - , warp >= 1.2 && < 1.3 - , wai >= 1.2 && < 1.3 - , wai-extra >= 1.2 && < 1.3 + build-depends: base >= 4 && < 5 + , yesod-core >= 1.0 && < 1.1 + , warp >= 1.2 && < 1.3 + , wai >= 1.2 && < 1.3 + , wai-extra >= 1.2 && < 1.3 , bytestring >= 0.9.1.4 - , transformers >= 0.2.2 && < 0.3 + , transformers >= 0.2.2 && < 0.3 , text >= 0.9 , directory >= 1.0 - , shakespeare-css >= 0.10.5 && < 0.11 - , shakespeare-js >= 0.11 && < 0.12 + , shakespeare-css >= 0.10.5 && < 0.11 + , shakespeare-js >= 0.11 && < 0.12 , template-haskell - , yaml >= 0.6 && < 0.7 + , yaml >= 0.6 && < 0.7 + , network-conduit >= 0.3 && < 0.4 , unordered-containers if !os(windows) diff --git a/yesod/scaffold/config/settings.yml.cg b/yesod/scaffold/config/settings.yml.cg index 6335e3db..d1a503b3 100644 --- a/yesod/scaffold/config/settings.yml.cg +++ b/yesod/scaffold/config/settings.yml.cg @@ -14,5 +14,7 @@ Staging: <<: *defaults Production: + host: "*4" # any IPv4 host + port: 3000 approot: "http://www.example.com" <<: *defaults diff --git a/yesod/scaffold/project.cabal.cg b/yesod/scaffold/project.cabal.cg index adbb6ac9..dbda15f9 100644 --- a/yesod/scaffold/project.cabal.cg +++ b/yesod/scaffold/project.cabal.cg @@ -75,12 +75,12 @@ executable ~project~ EmptyDataDecls build-depends: base >= 4 && < 5 - , yesod >= 0.10 && < 0.11 - , yesod-core >= 0.10 && < 0.11 - , yesod-auth >= 0.8 && < 0.9 - , yesod-static >= 0.10 && < 0.11 - , yesod-default >= 0.6 && < 0.7 - , yesod-form >= 0.4 && < 0.5 + , yesod >= 1.0 && < 1.1 + , yesod-core >= 1.0 && < 1.1 + , yesod-auth >= 1.0 && < 1.1 + , yesod-static >= 1.0 && < 1.1 + , yesod-default >= 1.0 && < 1.1 + , yesod-form >= 1.0 && < 1.1 , mime-mail >= 0.3.0.3 && < 0.5 , clientsession >= 0.7.3 && < 0.8 , bytestring >= 0.9 && < 0.10 @@ -91,10 +91,10 @@ executable ~project~ , hamlet >= 0.10 && < 0.11 , shakespeare-css >= 0.10 && < 0.11 , shakespeare-js >= 0.11 && < 0.12 - , shakespeare-text >= 0.10 && < 0.11 + , shakespeare-text >= 0.11 && < 0.12 , hjsmin >= 0.0.14 && < 0.1 , monad-control >= 0.3 && < 0.4 - , wai-extra >= 1.0 && < 1.2 - , yaml >= 0.5 && < 0.6 + , wai-extra >= 1.2 && < 1.3 + , yaml >= 0.6 && < 0.7 , http-conduit >= 1.2 && < 1.3 diff --git a/yesod/scaffold/tiny/project.cabal.cg b/yesod/scaffold/tiny/project.cabal.cg index d5f41777..8623c67c 100644 --- a/yesod/scaffold/tiny/project.cabal.cg +++ b/yesod/scaffold/tiny/project.cabal.cg @@ -66,18 +66,18 @@ executable ~project~ TypeFamilies build-depends: base >= 4 && < 5 - , yesod-core >= 0.10 && < 0.11 - , yesod-static >= 0.10 && < 0.11 - , yesod-default >= 0.6 && < 0.7 + , yesod-core >= 1.0 && < 1.1 + , yesod-static >= 1.0 && < 1.1 + , yesod-default >= 1.0 && < 1.1 , clientsession >= 0.7.3 && < 0.8 , bytestring >= 0.9 && < 0.10 , text >= 0.11 && < 0.12 , template-haskell , hamlet >= 0.10 && < 0.11 - , shakespeare-text >= 0.10 && < 0.11 - , wai >= 1.1 && < 1.2 - , wai-extra >= 1.1 && < 1.2 + , shakespeare-text >= 0.11 && < 0.12 + , wai >= 1.2 && < 1.3 + , wai-extra >= 1.2 && < 1.3 , transformers >= 0.2 && < 0.3 , monad-control >= 0.3 && < 0.4 - , yaml >= 0.5 && < 0.6 + , yaml >= 0.6 && < 0.7