Simplified host/port/approot settings, solves #274
This commit is contained in:
parent
c3a9c845e9
commit
c65053ec64
@ -22,6 +22,8 @@ import Data.Maybe (fromMaybe)
|
|||||||
import qualified Data.HashMap.Strict as M
|
import qualified Data.HashMap.Strict as M
|
||||||
import System.Environment (getArgs, getProgName, getEnvironment)
|
import System.Environment (getArgs, getProgName, getEnvironment)
|
||||||
import System.Exit (exitFailure)
|
import System.Exit (exitFailure)
|
||||||
|
import Data.Conduit.Network (HostPreference)
|
||||||
|
import Data.String (fromString)
|
||||||
|
|
||||||
-- | A yesod-provided @'AppEnv'@, allows for Development, Testing, and
|
-- | A yesod-provided @'AppEnv'@, allows for Development, Testing, and
|
||||||
-- Production environments
|
-- Production environments
|
||||||
@ -97,6 +99,7 @@ data AppConfig environment extra = AppConfig
|
|||||||
{ appEnv :: environment
|
{ appEnv :: environment
|
||||||
, appPort :: Int
|
, appPort :: Int
|
||||||
, appRoot :: Text
|
, appRoot :: Text
|
||||||
|
, appHost :: HostPreference
|
||||||
, appExtra :: extra
|
, appExtra :: extra
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
@ -142,15 +145,14 @@ configSettings env0 = ConfigSettings
|
|||||||
-- > host: localhost
|
-- > host: localhost
|
||||||
-- > port: 3000
|
-- > port: 3000
|
||||||
-- >
|
-- >
|
||||||
-- > -- ssl: will default false
|
-- > -- approot: will default to ""
|
||||||
-- > -- approot: will default to "http://localhost:3000"
|
|
||||||
--
|
--
|
||||||
-- > -- typical outward-facing production box
|
-- > -- typical outward-facing production box
|
||||||
-- > Production:
|
-- > Production:
|
||||||
-- > host: www.example.com
|
-- > host: www.example.com
|
||||||
-- >
|
-- >
|
||||||
-- > -- ssl: will default false
|
|
||||||
-- > -- port: will default 80
|
-- > -- port: will default 80
|
||||||
|
-- > -- host: will default to "*"
|
||||||
-- > -- approot: will default "http://www.example.com"
|
-- > -- approot: will default "http://www.example.com"
|
||||||
--
|
--
|
||||||
-- > -- maybe you're reverse proxying connections to the running app
|
-- > -- maybe you're reverse proxying connections to the running app
|
||||||
@ -158,10 +160,7 @@ configSettings env0 = ConfigSettings
|
|||||||
-- > Production:
|
-- > Production:
|
||||||
-- > port: 8080
|
-- > port: 8080
|
||||||
-- > approot: "http://example.com"
|
-- > approot: "http://example.com"
|
||||||
-- >
|
-- > host: "localhost"
|
||||||
-- > -- approot is specified so that the non-80 port is not appended
|
|
||||||
-- > -- automatically.
|
|
||||||
--
|
|
||||||
loadConfig :: ConfigSettings environment extra
|
loadConfig :: ConfigSettings environment extra
|
||||||
-> IO (AppConfig environment extra)
|
-> IO (AppConfig environment extra)
|
||||||
loadConfig (ConfigSettings env parseExtra getFile getObject) = do
|
loadConfig (ConfigSettings env parseExtra getFile getObject) = do
|
||||||
@ -174,30 +173,20 @@ loadConfig (ConfigSettings env parseExtra getFile getObject) = do
|
|||||||
Object m -> return m
|
Object m -> return m
|
||||||
_ -> fail "Expected map"
|
_ -> fail "Expected map"
|
||||||
|
|
||||||
let mssl = lookupScalar "ssl" m
|
let host = fromString $ T.unpack $ fromMaybe "*" $ lookupScalar "host" m
|
||||||
let mhost = lookupScalar "host" m
|
|
||||||
mport <- parseMonad (\x -> x .: "port") m
|
mport <- parseMonad (\x -> x .: "port") m
|
||||||
let mapproot = lookupScalar "approot" m
|
let approot = fromMaybe "" $ lookupScalar "approot" m
|
||||||
|
|
||||||
extra <- parseMonad (parseExtra env) m
|
extra <- parseMonad (parseExtra env) m
|
||||||
|
|
||||||
-- set some default arguments
|
-- set some default arguments
|
||||||
let ssl = maybe False toBool mssl
|
let port' = fromMaybe 80 mport
|
||||||
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"
|
|
||||||
|
|
||||||
return $ AppConfig
|
return $ AppConfig
|
||||||
{ appEnv = env
|
{ appEnv = env
|
||||||
, appPort = port'
|
, appPort = port'
|
||||||
, appRoot = approot
|
, appRoot = approot
|
||||||
|
, appHost = host
|
||||||
, appExtra = extra
|
, appExtra = extra
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,13 +196,6 @@ loadConfig (ConfigSettings env parseExtra getFile getObject) = do
|
|||||||
Just (String t) -> return t
|
Just (String t) -> return t
|
||||||
Just _ -> fail $ "Invalid value for: " ++ show k
|
Just _ -> fail $ "Invalid value for: " ++ show k
|
||||||
Nothing -> fail $ "Not found: " ++ 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
|
-- | Loads the configuration block in the passed file named by the
|
||||||
-- passed environment, yeilds to the passed function as a mapping.
|
-- passed environment, yeilds to the passed function as a mapping.
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import Yesod.Default.Config
|
|||||||
import Yesod.Logger (Logger, defaultDevelopmentLogger, logString)
|
import Yesod.Logger (Logger, defaultDevelopmentLogger, logString)
|
||||||
import Network.Wai (Application)
|
import Network.Wai (Application)
|
||||||
import Network.Wai.Handler.Warp
|
import Network.Wai.Handler.Warp
|
||||||
(runSettings, defaultSettings, settingsPort)
|
(runSettings, defaultSettings, settingsPort, settingsHost)
|
||||||
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
|
import System.Directory (doesDirectoryExist, removeDirectoryRecursive)
|
||||||
import Network.Wai.Middleware.Gzip (gzip, GzipFiles (GzipCacheFolder), gzipFiles, def)
|
import Network.Wai.Middleware.Gzip (gzip, GzipFiles (GzipCacheFolder), gzipFiles, def)
|
||||||
import Network.Wai.Middleware.Autohead (autohead)
|
import Network.Wai.Middleware.Autohead (autohead)
|
||||||
@ -45,8 +45,10 @@ defaultMain load getApp = do
|
|||||||
config <- load
|
config <- load
|
||||||
logger <- defaultDevelopmentLogger
|
logger <- defaultDevelopmentLogger
|
||||||
app <- getApp config logger
|
app <- getApp config logger
|
||||||
|
print $ appHost config
|
||||||
runSettings defaultSettings
|
runSettings defaultSettings
|
||||||
{ settingsPort = appPort config
|
{ settingsPort = appPort config
|
||||||
|
, settingsHost = appHost config
|
||||||
} app
|
} app
|
||||||
|
|
||||||
-- | Run your application continously, listening for SIGINT and exiting
|
-- | Run your application continously, listening for SIGINT and exiting
|
||||||
|
|||||||
@ -17,19 +17,20 @@ library
|
|||||||
if os(windows)
|
if os(windows)
|
||||||
cpp-options: -DWINDOWS
|
cpp-options: -DWINDOWS
|
||||||
|
|
||||||
build-depends: base >= 4 && < 5
|
build-depends: base >= 4 && < 5
|
||||||
, yesod-core >= 1.0 && < 1.1
|
, yesod-core >= 1.0 && < 1.1
|
||||||
, warp >= 1.2 && < 1.3
|
, warp >= 1.2 && < 1.3
|
||||||
, wai >= 1.2 && < 1.3
|
, wai >= 1.2 && < 1.3
|
||||||
, wai-extra >= 1.2 && < 1.3
|
, wai-extra >= 1.2 && < 1.3
|
||||||
, bytestring >= 0.9.1.4
|
, bytestring >= 0.9.1.4
|
||||||
, transformers >= 0.2.2 && < 0.3
|
, transformers >= 0.2.2 && < 0.3
|
||||||
, text >= 0.9
|
, text >= 0.9
|
||||||
, directory >= 1.0
|
, directory >= 1.0
|
||||||
, shakespeare-css >= 0.10.5 && < 0.11
|
, shakespeare-css >= 0.10.5 && < 0.11
|
||||||
, shakespeare-js >= 0.11 && < 0.12
|
, shakespeare-js >= 0.11 && < 0.12
|
||||||
, template-haskell
|
, template-haskell
|
||||||
, yaml >= 0.6 && < 0.7
|
, yaml >= 0.6 && < 0.7
|
||||||
|
, network-conduit >= 0.3 && < 0.4
|
||||||
, unordered-containers
|
, unordered-containers
|
||||||
|
|
||||||
if !os(windows)
|
if !os(windows)
|
||||||
|
|||||||
@ -14,5 +14,7 @@ Staging:
|
|||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
|
||||||
Production:
|
Production:
|
||||||
|
host: "*4" # any IPv4 host
|
||||||
|
port: 3000
|
||||||
approot: "http://www.example.com"
|
approot: "http://www.example.com"
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
|||||||
@ -75,12 +75,12 @@ executable ~project~
|
|||||||
EmptyDataDecls
|
EmptyDataDecls
|
||||||
|
|
||||||
build-depends: base >= 4 && < 5
|
build-depends: base >= 4 && < 5
|
||||||
, yesod >= 0.10 && < 0.11
|
, yesod >= 1.0 && < 1.1
|
||||||
, yesod-core >= 0.10 && < 0.11
|
, yesod-core >= 1.0 && < 1.1
|
||||||
, yesod-auth >= 0.8 && < 0.9
|
, yesod-auth >= 1.0 && < 1.1
|
||||||
, yesod-static >= 0.10 && < 0.11
|
, yesod-static >= 1.0 && < 1.1
|
||||||
, yesod-default >= 0.6 && < 0.7
|
, yesod-default >= 1.0 && < 1.1
|
||||||
, yesod-form >= 0.4 && < 0.5
|
, yesod-form >= 1.0 && < 1.1
|
||||||
, mime-mail >= 0.3.0.3 && < 0.5
|
, mime-mail >= 0.3.0.3 && < 0.5
|
||||||
, clientsession >= 0.7.3 && < 0.8
|
, clientsession >= 0.7.3 && < 0.8
|
||||||
, bytestring >= 0.9 && < 0.10
|
, bytestring >= 0.9 && < 0.10
|
||||||
@ -91,10 +91,10 @@ executable ~project~
|
|||||||
, hamlet >= 0.10 && < 0.11
|
, hamlet >= 0.10 && < 0.11
|
||||||
, shakespeare-css >= 0.10 && < 0.11
|
, shakespeare-css >= 0.10 && < 0.11
|
||||||
, shakespeare-js >= 0.11 && < 0.12
|
, shakespeare-js >= 0.11 && < 0.12
|
||||||
, shakespeare-text >= 0.10 && < 0.11
|
, shakespeare-text >= 0.11 && < 0.12
|
||||||
, hjsmin >= 0.0.14 && < 0.1
|
, hjsmin >= 0.0.14 && < 0.1
|
||||||
, monad-control >= 0.3 && < 0.4
|
, monad-control >= 0.3 && < 0.4
|
||||||
, wai-extra >= 1.0 && < 1.2
|
, wai-extra >= 1.2 && < 1.3
|
||||||
, yaml >= 0.5 && < 0.6
|
, yaml >= 0.6 && < 0.7
|
||||||
, http-conduit >= 1.2 && < 1.3
|
, http-conduit >= 1.2 && < 1.3
|
||||||
|
|
||||||
|
|||||||
@ -66,18 +66,18 @@ executable ~project~
|
|||||||
TypeFamilies
|
TypeFamilies
|
||||||
|
|
||||||
build-depends: base >= 4 && < 5
|
build-depends: base >= 4 && < 5
|
||||||
, yesod-core >= 0.10 && < 0.11
|
, yesod-core >= 1.0 && < 1.1
|
||||||
, yesod-static >= 0.10 && < 0.11
|
, yesod-static >= 1.0 && < 1.1
|
||||||
, yesod-default >= 0.6 && < 0.7
|
, yesod-default >= 1.0 && < 1.1
|
||||||
, clientsession >= 0.7.3 && < 0.8
|
, clientsession >= 0.7.3 && < 0.8
|
||||||
, bytestring >= 0.9 && < 0.10
|
, bytestring >= 0.9 && < 0.10
|
||||||
, text >= 0.11 && < 0.12
|
, text >= 0.11 && < 0.12
|
||||||
, template-haskell
|
, template-haskell
|
||||||
, hamlet >= 0.10 && < 0.11
|
, hamlet >= 0.10 && < 0.11
|
||||||
, shakespeare-text >= 0.10 && < 0.11
|
, shakespeare-text >= 0.11 && < 0.12
|
||||||
, wai >= 1.1 && < 1.2
|
, wai >= 1.2 && < 1.3
|
||||||
, wai-extra >= 1.1 && < 1.2
|
, wai-extra >= 1.2 && < 1.3
|
||||||
, transformers >= 0.2 && < 0.3
|
, transformers >= 0.2 && < 0.3
|
||||||
, monad-control >= 0.3 && < 0.4
|
, monad-control >= 0.3 && < 0.4
|
||||||
, yaml >= 0.5 && < 0.6
|
, yaml >= 0.6 && < 0.7
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user