我希望我的yesod web app中的初始数据库状态由在app启动时解析的配置文件定义。我目前实现此目的的方法是编译,但似乎没有做任何事情,我怀疑这是因为数据库可能还没有准备好在那时使用,或者需要以不同方式访问。
import System.Directory
import Data.Ini
-- | The @main@ function for an executable running this site.
appMain :: IO ()
appMain = do
settings <- loadYamlSettingsArgs
[configSettingsYmlValue]
useEnv
foundation <- makeFoundation settings
loadConfigs -- | load configs and fill db
app <- makeApplication foundation
runSettings (warpSettings foundation) app
loadConfigs :: IO ()
loadConfigs = do allFiles <- getDirectoryContents "/config"
let filtered = filter (isSuffixOf ".cfg") allFiles
forM_ filtered parseConfig
parseConfig :: String -> IO ()
parseConfig fileName = do
res <- readIniFile fileName
case res of
Left msg -> do
return ()
Right ini -> do
let name = fromRight (pack "-") $ lookupValue (pack "name") (pack "-") ini
db $ insert (User name "" "")
return ()
-- | Run DB queries
db :: ReaderT SqlBackend (HandlerT App IO) a -> IO a
db = handler . runDB
你能指教一位没有经验的哈斯克勒吗?谢谢!