Question

I have an acid-state backend that complements my snap website. It is running in its own process and my snap web server requires an IP address to connect to it. For debugging and deployment purposes I would like to be able to pass in the IP address as a command line argument when running my compiled snap application. This IP address would be accessible inside the SnapletInit monad where the acid state handler gets called.

How can I extend the command line parameter system in Snap to account for this?

Ideally, I'd like something like.

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

Then apply it like this.

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

Solution

I would recommend changing the Acid State snaplet to read it's IP from a config instead of the command line. The configs in Snap are set up so that it'll load whatever you pass as the -e argument on the command line. For example, starting with -e prod will load snaplet/acidstate/prod.conf and starting with no -e or -e devel will load snaplet/acidstate/devel.conf. This helps keep all your environmental settings together instead of allowing any possible combination of command line flags.

Here's an example from one of my snaplets:

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

OTHER TIPS

You could use getEnv, lookupEnv or getArgs from System.Environment

Personally, I'd go with the ENV variables approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top