Custom Configuration
Dealing with Secrets
Sometimes you want to have a custom configuration flag inside your application. The recommended way is to declare a customnewtype in Config/Config.hs like this:
STRIPE_PUBLIC_KEY env variable. Therefore we add this to our Config.hs:
STRIPE_PUBLIC_KEY env variable at startup and makes it available to the app.
Before we proceed we should add a default value for this in dev mode. Create a file .env and add the following env variables:
.env is not committed to the repo as it’s part of the default .gitignore for IHP projects. The .envrc has a snippet to load environment variables from .env into your shell.
If you are ok to commit your secrets to git repo, you can also put the env vars directly into the .envrc file.
Using Custom Config Parameters
You can now access theStripePublicKey parameter by calling getAppConfig @Config.StripePublicKey:
?context:
Environment Variables
Reading Environment Variables
InsideConfig/Config.hs you can use env to read environment variables.
env function will raise an error if the env var is not defined.
The env function can also deal with other common types:
Default Values
UseenvOrDefault to provide a default value for an env var:
Optional Env Variables
When an env variable is optional and has no good default value, useenvOrNothing. It will return Nothing if the env variable is not set:
Custom Parser
When you’re dealing with a custom enum type it can be useful to write a custom env parser by implementing anEnvVarReader:
Custom Middleware
IHP provides an “escape-hatch” from the framework with theCustomMiddleware option.
This can be used to run any WAI middleware after IHP’s middleware stack, allowing for possibilities
such as embedding a Servant or Yesod app into an IHP app, adding GZIP compression, or any other
number of possibilities. See wai-extra for examples
of WAI middleware that could be added.
The following example sets up a custom middleware that infers the real IP using X-Forwarded-For
and adds a custom header for every request.
Compression Middleware
We can compress assets using gzip or brotli.Create compression middleware
Create a function
compressionMiddleware that combines Gzip and Brotli compression middleware:It’s important that brotli middleware wraps the gzip middleware, so the responses are not compressed by both. If the client supports brotli, compress with brotli, otherwise gzip, fallback to no compression.
text/* content types will be compressed, including application/json, application/javascript, application/ecmascript and image/x-icon. Simply put, html, text, css, javascript, json and icons.
Database Connection Pool
IHP uses two database connection pools:- postgresql-simple pool - Used for inserts, updates, deletes, and transactions
- hasql pool - Used for fetch queries with prepared statements (better performance)
Hasql Pool Configuration
The hasql pool can be configured using environment variables:| Variable | Default | Description |
|---|---|---|
HASQL_POOL_SIZE | 3 | Number of connections in the pool |
HASQL_IDLE_TIME | 600 | Seconds before idle connections are closed |
.env configuration: