使用Haskell Snap扩展命令行选项

时间:2014-02-05 23:42:58

标签: haskell haskell-snap-framework

我有一个酸状态后端,可以补充我的snap网站。它在自己的进程中运行,我的快照Web服务器需要一个IP地址才能连接到它。出于调试和部署的目的,我希望能够在运行编译的snap应用程序时将IP地址作为命令行参数传递。这个IP地址可以在调用酸状态处理程序的SnapletInit monad中访问。

如何在Snap中扩展命令行参数系统以解决此问题?

理想情况下,我喜欢类似的东西。

./app -ip 192.168.0.2 -p 8080 -e prod +RTS -I0 -A4M -qg1

然后像这样申请。

app :: SnapletInit App App
app = makeSnaplet "app" "Snapplication" Nothing $ do
    ip <- getConfig "ip"
    d <- nestSnaplet "acid" acid $ acidInitRemote ip
    return $ App d

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

我建议更改Acid State snaplet以从配置而不是命令行读取它的IP。设置Snap中的配置,以便它将在命令行上加载您传递的任何-e参数。例如,从-e prod开始将加载snaplet/acidstate/prod.conf,从无-e-e devel开始将加载snaplet/acidstate/devel.conf。这有助于将所有环境设置保持在一起,而不是允许任何可能的命令行标记组合。

这是我的一个快照中的an example

initStripe :: SnapletInit b StripeState
initStripe = makeSnaplet "stripe" "Stripe credit card payment" Nothing $ do
  config <- getSnapletUserConfig

  (stripeState, errors) <- runWriterT $ do
    secretKey <- logErr "Must specify Strip secret key"  $ C.lookup config "secret_key"
    publicKey <- logErr "Must specify Strip public key"  $ C.lookup config "public_key"
    clientId  <- logErr "Must specify Strip client ID"   $ C.lookup config "client_id"
    version   <- Just . maybe V20110915d OtherVersion <$> liftIO (C.lookup config "version")
    let caFilePath = Just "" -- This is unused by Stripe but vestigial in the Haskell library.

    return $ StripeState <$> (StripeConfig <$> (SecretKey <$> secretKey) <*> caFilePath <*> version) <*> (PublicKey <$> publicKey) <*> clientId
  return $ fromMaybe (error $ intercalate "\n" errors) stripeState

答案 2 :(得分:0)

您可以使用System.Environment中的getEnvlookupEnvgetArgs

就个人而言,我会选择ENV变量方法。

相关问题