fix option parsing & settings
This commit is contained in:
parent
7452726d40
commit
942590a9e3
@ -100,6 +100,7 @@ scaffold = do
|
|||||||
mkDir "static/css"
|
mkDir "static/css"
|
||||||
mkDir "config"
|
mkDir "config"
|
||||||
|
|
||||||
|
writeFile' ("config/Settings.yaml") $(codegen "Settings_yaml")
|
||||||
writeFile' ("config/" ++ project ++ ".hs") $(codegen "test_hs")
|
writeFile' ("config/" ++ project ++ ".hs") $(codegen "test_hs")
|
||||||
writeFile' (project ++ ".cabal") $ if backendS == "m" then $(codegen "mini-cabal") else $(codegen "cabal")
|
writeFile' (project ++ ".cabal") $ if backendS == "m" then $(codegen "mini-cabal") else $(codegen "cabal")
|
||||||
writeFile' ".ghci" $(codegen "dotghci")
|
writeFile' ".ghci" $(codegen "dotghci")
|
||||||
|
|||||||
@ -76,7 +76,7 @@ data AppConfig = AppConfig {
|
|||||||
|
|
||||||
loadConfig :: AppEnvironment -> IO AppConfig
|
loadConfig :: AppEnvironment -> IO AppConfig
|
||||||
loadConfig env = do
|
loadConfig env = do
|
||||||
allSettings <- (join $ decodeFile ("Settings.yaml" :: String)) >>= fromMapping
|
allSettings <- (join $ decodeFile ("config/Settings.yaml" :: String)) >>= fromMapping
|
||||||
settings <- lookupMapping (show env) allSettings
|
settings <- lookupMapping (show env) allSettings
|
||||||
appPortS <- lookupScalar "appPort" settings
|
appPortS <- lookupScalar "appPort" settings
|
||||||
appRootS <- lookupScalar "appRoot" settings
|
appRootS <- lookupScalar "appRoot" settings
|
||||||
@ -84,7 +84,7 @@ loadConfig env = do
|
|||||||
return $ AppConfig {
|
return $ AppConfig {
|
||||||
appEnv = env
|
appEnv = env
|
||||||
, appPort = read $ appPortS
|
, appPort = read $ appPortS
|
||||||
, appRoot = read $ appRootS
|
, appRoot = read $ (show appRootS)
|
||||||
, connectionPoolSize = read $ connectionPoolSizeS
|
, connectionPoolSize = read $ connectionPoolSizeS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
Default: &default
|
Default: &defaults
|
||||||
appRoot: http://localhost
|
appRoot: http://localhost
|
||||||
appPort: 3000
|
appPort: 3000
|
||||||
connectionPoolLimit: 10
|
connectionPoolSize: 10
|
||||||
|
|
||||||
Development:
|
Development:
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
{-# LANGUAGE CPP, DeriveDataTypeable #-}
|
{-# LANGUAGE CPP, DeriveDataTypeable #-}
|
||||||
import qualified Settings as Settings
|
import qualified Settings as Settings
|
||||||
|
import Settings (AppConfig(..))
|
||||||
import Controller (with~sitearg~)
|
import Controller (with~sitearg~)
|
||||||
import Network.Wai.Handler.Warp (run)
|
import Network.Wai.Handler.Warp (run)
|
||||||
import System.Console.CmdArgs
|
import System.Console.CmdArgs
|
||||||
@ -8,44 +9,49 @@ import Data.Char (toUpper, toLower)
|
|||||||
#if PRODUCTION
|
#if PRODUCTION
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
appEnv <- getAppEnv
|
args <- cmdArgs argConfig
|
||||||
|
appEnv <- getAppEnv args
|
||||||
config <- Settings.loadConfig appEnv
|
config <- Settings.loadConfig appEnv
|
||||||
with~sitearg~ config $ run (Settings.appPort settings)
|
let c = if (port args) /= 0 then config {appPort = (port args) } else config
|
||||||
|
with~sitearg~ c $ run (appPort c)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
import System.IO (hPutStrLn, stderr)
|
import System.IO (hPutStrLn, stderr)
|
||||||
import Network.Wai.Middleware.Debug (debug)
|
import Network.Wai.Middleware.Debug (debug)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
appEnv <- getAppEnv
|
args <- cmdArgs argConfig
|
||||||
|
appEnv <- getAppEnv args
|
||||||
config <- Settings.loadConfig appEnv
|
config <- Settings.loadConfig appEnv
|
||||||
hPutStrLn stderr $ "Application launched, listening on port " ++ show (Settings.appPort config)
|
let c = if (port args) /= 0 then config {appPort = (port args) } else config
|
||||||
with~sitearg~ config $ run (Settings.appPort config) . debug
|
do hPutStrLn stderr $ "Application launched, listening on port " ++ show (appPort c)
|
||||||
|
with~sitearg~ c $ run (appPort c) . debug
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
data ArgConfig = ArgConfig {environment :: String}
|
data ArgConfig = ArgConfig {environment :: String, port :: Int}
|
||||||
deriving (Show, Data, Typeable)
|
deriving (Show, Data, Typeable)
|
||||||
|
|
||||||
config = ArgConfig{ environment = def
|
argConfig = ArgConfig{ environment = def
|
||||||
&= help "application environment, one of:" ++ (foldl1 (++) environments)
|
&= help ("application environment, one of: " ++ (foldl1 (\a b -> a ++ ", " ++ b) environments))
|
||||||
&= typ "ENVIRONMENT"
|
&= typ "ENVIRONMENT"
|
||||||
#if PRODUCTION
|
,port = def &= typ "PORT"
|
||||||
&= opt "production"
|
|
||||||
#else
|
|
||||||
&= opt "development"
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
environments :: [String]
|
environments :: [String]
|
||||||
environments = map show ([minBound..maxBound] :: [Settings.AppEnvironment])
|
environments = map ((map toLower) . show) ([minBound..maxBound] :: [Settings.AppEnvironment])
|
||||||
|
|
||||||
|
|
||||||
-- | retrieve the -e environment option
|
-- | retrieve the -e environment option
|
||||||
getAppEnv :: IO Settings.AppEnvironment
|
getAppEnv :: ArgConfig -> IO Settings.AppEnvironment
|
||||||
getAppEnv = do
|
getAppEnv cfg = do
|
||||||
cfg <- cmdArgs config
|
let e = if (environment cfg) /= "" then (environment cfg)
|
||||||
return $ read $ capitalize $ environment cfg
|
else
|
||||||
|
#if PRODUCTION
|
||||||
|
"production"
|
||||||
|
#else
|
||||||
|
"development"
|
||||||
|
#endif
|
||||||
|
return $ read $ capitalize e
|
||||||
where
|
where
|
||||||
capitalize [] = []
|
capitalize [] = []
|
||||||
capitalize (x:xs) = toUpper x : map toLower xs
|
capitalize (x:xs) = toUpper x : map toLower xs
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
cd .. &&
|
|
||||||
cabal clean && cabal install &&
|
cabal clean && cabal install &&
|
||||||
rm -rf foobar && runghc scaffold.hs init < sample-input.txt && cd foobar && cabal install && cabal install -fdevel && cd .. &&
|
rm -rf foobar && runghc scaffold.hs init < tests/sample-input.txt && cd foobar && cabal install && cabal install -fdevel && cd ..
|
||||||
cd tests
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user