fix option parsing & settings

This commit is contained in:
Greg Weber 2011-07-10 08:12:28 -07:00
parent 7452726d40
commit 942590a9e3
5 changed files with 32 additions and 27 deletions

View File

@ -100,6 +100,7 @@ scaffold = do
mkDir "static/css"
mkDir "config"
writeFile' ("config/Settings.yaml") $(codegen "Settings_yaml")
writeFile' ("config/" ++ project ++ ".hs") $(codegen "test_hs")
writeFile' (project ++ ".cabal") $ if backendS == "m" then $(codegen "mini-cabal") else $(codegen "cabal")
writeFile' ".ghci" $(codegen "dotghci")

View File

@ -76,7 +76,7 @@ data AppConfig = AppConfig {
loadConfig :: AppEnvironment -> IO AppConfig
loadConfig env = do
allSettings <- (join $ decodeFile ("Settings.yaml" :: String)) >>= fromMapping
allSettings <- (join $ decodeFile ("config/Settings.yaml" :: String)) >>= fromMapping
settings <- lookupMapping (show env) allSettings
appPortS <- lookupScalar "appPort" settings
appRootS <- lookupScalar "appRoot" settings
@ -84,7 +84,7 @@ loadConfig env = do
return $ AppConfig {
appEnv = env
, appPort = read $ appPortS
, appRoot = read $ appRootS
, appRoot = read $ (show appRootS)
, connectionPoolSize = read $ connectionPoolSizeS
}

View File

@ -1,7 +1,7 @@
Default: &default
Default: &defaults
appRoot: http://localhost
appPort: 3000
connectionPoolLimit: 10
connectionPoolSize: 10
Development:
<<: *defaults

View File

@ -1,5 +1,6 @@
{-# LANGUAGE CPP, DeriveDataTypeable #-}
import qualified Settings as Settings
import Settings (AppConfig(..))
import Controller (with~sitearg~)
import Network.Wai.Handler.Warp (run)
import System.Console.CmdArgs
@ -8,44 +9,49 @@ import Data.Char (toUpper, toLower)
#if PRODUCTION
main :: IO ()
main = do
appEnv <- getAppEnv
args <- cmdArgs argConfig
appEnv <- getAppEnv args
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
import System.IO (hPutStrLn, stderr)
import Network.Wai.Middleware.Debug (debug)
main :: IO ()
main = do
appEnv <- getAppEnv
args <- cmdArgs argConfig
appEnv <- getAppEnv args
config <- Settings.loadConfig appEnv
hPutStrLn stderr $ "Application launched, listening on port " ++ show (Settings.appPort config)
with~sitearg~ config $ run (Settings.appPort config) . debug
let c = if (port args) /= 0 then config {appPort = (port args) } else config
do hPutStrLn stderr $ "Application launched, listening on port " ++ show (appPort c)
with~sitearg~ c $ run (appPort c) . debug
#endif
data ArgConfig = ArgConfig {environment :: String}
data ArgConfig = ArgConfig {environment :: String, port :: Int}
deriving (Show, Data, Typeable)
config = ArgConfig{ environment = def
&= help "application environment, one of:" ++ (foldl1 (++) environments)
argConfig = ArgConfig{ environment = def
&= help ("application environment, one of: " ++ (foldl1 (\a b -> a ++ ", " ++ b) environments))
&= typ "ENVIRONMENT"
#if PRODUCTION
&= opt "production"
#else
&= opt "development"
#endif
,port = def &= typ "PORT"
}
environments :: [String]
environments = map show ([minBound..maxBound] :: [Settings.AppEnvironment])
environments = map ((map toLower) . show) ([minBound..maxBound] :: [Settings.AppEnvironment])
-- | retrieve the -e environment option
getAppEnv :: IO Settings.AppEnvironment
getAppEnv = do
cfg <- cmdArgs config
return $ read $ capitalize $ environment cfg
getAppEnv :: ArgConfig -> IO Settings.AppEnvironment
getAppEnv cfg = do
let e = if (environment cfg) /= "" then (environment cfg)
else
#if PRODUCTION
"production"
#else
"development"
#endif
return $ read $ capitalize e
where
capitalize [] = []
capitalize (x:xs) = toUpper x : map toLower xs

View File

@ -1,6 +1,4 @@
#!/bin/sh
cd .. &&
cabal clean && cabal install &&
rm -rf foobar && runghc scaffold.hs init < sample-input.txt && cd foobar && cabal install && cabal install -fdevel && cd .. &&
cd tests
rm -rf foobar && runghc scaffold.hs init < tests/sample-input.txt && cd foobar && cabal install && cabal install -fdevel && cd ..