3

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

3 Answers 3

1

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
1

Check out snap-server's Config module. Specifically, extendedCommandLineConfig.

4
  • 2
    Can you elaborate? The docs seem to be written exclusively for people who already know snap's internals. I have no idea how to use that function, or how to use the higher level commandLineAppConfig that calls it. Apr 2, 2014 at 15:09
  • The first argument to extendedCommandLineConfigSource is a list of your own custom command line options. The second argument is a combining function for those options.
    – mightybyte
    Apr 2, 2014 at 15:29
  • 2
    That is what the API doc says. That does not make it clear how one is supposed to go about using it. Creating a list of custom command line options is fine, but passing them to extendedCommandLineConfigSource removes the built in ones, if you manually add back the built in ones then -e is still missing, and since it needs to be a Config Snap AppConfig, how do you deal with multiple command line options? AppConfig is just a Maybe String? Apr 2, 2014 at 17:36
  • You'll want to use your own data type instead of AppConfig. Then you'll also want to include -e in there along with whatever other information you need so you can set it like it currently is.
    – mightybyte
    Apr 2, 2014 at 22:35
0

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

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

4
  • Yea that works, went with ip <- fromMaybe "localhost" <$> lookupEnv "ACIDIP". Sometimes I want to run the web server on port 80 locally for testing, and the env variables weren't being respected. So I re-exported it using sudo visudoand Defaults env_keep += "ACIDIP" Feb 6, 2014 at 3:11
  • The only problem is that I still can't hot swap IP addresses w/o changing the env variables Feb 6, 2014 at 3:15
  • You should include all requirements in your question next time. You can trigger a site reload to reload snaplets, or create a snap route to do the reloading, or create your own snaplet which lets you change such things. Running snap init will get you some example code that is used for reloading. Also, you can set ENV variables on the command line myenv="this" mySnapProject Feb 6, 2014 at 3:32
  • Wow, did not know you could do myenv="this" mySnapProject Thank you. Feb 6, 2014 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.