Settings
This page provides a reference for all the available settings that can be used to configure Marten projects.
Common settings
allowed_hosts
Default: [] of String
An explicit array of allowed hosts for the application.
The application has to be explicitly configured to serve a list of allowed hosts. This is to mitigate HTTP Host header attacks. The strings in this array can correspond to regular domain names or subdomains (eg. example.com or www.example.com); when this is the case the Host header of the incoming request will be checked to ensure that it exactly matches one of the configured allowed hosts.
It is also possible to match all the subdomains of a specific domain by prepending a . at the beginning of the host string. For example .example.com will matches example.com, www.example.com, sub.example.com, or any other subdomains. Finally, the special * string can be used to match any Host value, but this wildcard value should be used with caution as you wouldn't be protected against Host header attacks.
It should be noted that this setting is automatically set to the following array when a project is running in debug mode (unless it is explicitly set):
[".localhost", "127.0.0.1", "[::1]"]
cache_store
Default: Marten::Cache::Store::Memory.new
The global cache store instance.
This setting allows to configure the cache store returned by the Marten#cache method (which can be used to perform low-level caching operations), and which is also leveraged for other caching features such as template fragment caching. Please refer to Caching to learn more about the caching features provided by Marten.
By default, the global cache store is set to be an in-memory cache (instance of Marten::Cache::Store::Memory). In test environments you might want to use the "null store" by assigning an instance of the `Marten::Cache::Store::Null to this setting. Additional caching store shards are also maintained under the umbrella of the Marten project or by the community itself and can be used as part of your application depending on your caching requirements. These backends are listed in the caching stores backend reference.
date_input_formats
Default:
[
"%Y-%m-%d", # '2024-10-25'
"%m/%d/%Y", # '10/25/2024'
"%m/%d/%y", # '10/25/06'
"%b %d %Y", # 'Oct 25 2024'
"%b %d, %Y", # 'Oct 25, 2024'
"%d %b %Y", # '25 Oct 2024'
"%d %b, %Y", # '25 Oct, 2024'
"%B %d %Y", # 'October 25 2024'
"%B %d, %Y", # 'October 25, 2024'
"%d %B %Y", # '25 October 2024'
"%d %B, %Y", # '25 October, 2024'
]
An array of default date input formats.
This array of default date input formats is used by the date schema field to parse date values from strings. Note that the date input formats coming from locales will be used with priority over the formats defined in this array.
date_time_input_formats
Default:
[
"%Y-%m-%d %H:%M:%S", # '2024-10-25 14:30:00'
"%Y-%m-%d %H:%M:%S.%f", # '2024-10-25 14:30:00.000000'
"%Y-%m-%d %H:%M", # '2024-10-25 14:30'
"%m/%d/%Y %H:%M:%S", # '10/25/2024 14:30:00'
"%m/%d/%Y %H:%M:%S.%f", # '10/25/2024 14:30:00.000000'
"%m/%d/%Y %H:%M", # '10/25/2024 14:30'
]
An array of default date input formats.
This array of default date input formats is used by the date_time schema field to parse date time values from strings. Note that the date time input formats coming from locales will be used with priority over the formats defined in this array.
debug
Default: false
A boolean allowing to enable or disable debug mode.
When running in debug mode, Marten will automatically provide detailed information about raised exceptions (including tracebacks) and incoming HTTP requests. As such this mode is mostly useful for development environments.
host
Default: "127.0.0.1"
The host the HTTP server running the application will be listening on.
installed_apps
Default: [] of Marten::Apps::Config.class
An array of the installed app classes. Each Marten application must define a subclass of Marten::Apps::Config. When those subclasses are specified in the installed_apps setting, the applications' models, migrations, assets, and templates will be made available to the considered project. Please refer to Applications to learn more about applications.
log_backend
Default: Log::IOBackend.new(...)
The log backend used by the application. Any Log::Backend object can be used, which can allow to easily configure how logs are formatted for example.
log_level
Default: Log::Severity::Info
The default log level used by the application. Any severity defined in the Log::Severity enum can be used.
This setting exclusively controls the log level for the Marten server. To set the log level for management commands, use the --log-level command option (see Shared options).
middleware
Default: [] of Marten::Middleware.class
An array of middlewares used by the application. For example:
config.middleware = [
Marten::Middleware::Session,
Marten::Middleware::I18n,
Marten::Middleware::GZip,
]
Middlewares are used to "hook" into Marten's request / response lifecycle. They can be used to alter or implement logics based on incoming HTTP requests and the resulting HTTP responses. Please refer to Middlewares to learn more about middlewares.
port
Default: 8000
The port the HTTP server running the application will be listening on.
port_reuse
Default: true
A boolean indicating whether multiple processes can bind to the same HTTP server port.
referrer_policy
Default: "same-origin"
The value to use for the Referrer-Policy header when the associated middleware is used. This header controls the amount of referrer information sent along with requests from your site to other origins, enhancing user privacy and security.
Possible values for the Referrer-Policy header include:
no-referrer: The Referer header will be omitted entirely. No referrer information is sent with requests.no-referrer-when-downgrade: The Referer header will not be sent to less secure destinations (e.g., from HTTPS to HTTP), but will be sent to same or more secure destinations.origin: Only the origin of the document is sent as the referrer.origin-when-cross-origin: The full URL is sent as the referrer when performing a same-origin request, but only the origin is sent for cross-origin requests.same-origin: The Referer header is sent with same-origin requests, but not with cross-origin requests.strict-origin: Only the origin is sent as the referrer, and only for same-origin requests.strict-origin-when-cross-origin: The full URL is sent as the referrer when performing a same-origin request, but only the origin is sent for cross-origin requests. No referrer information is sent to less secure destinations.unsafe-url: The full URL is always sent as the referrer, regardless of the request's security.
This setting will be used by the Marten::Middleware::ReferrerPolicy middleware when inserting the Referrer-Policy header in HTTP responses. By configuring this setting, you can control how much referrer information is included with requests from your site to other origins.
request_max_parameters
Default: 1000
The maximum number of allowed parameters per request (such as GET or POST parameters).
A large number of parameters will require more time to process and might be the sign of a denial-of-service attack, which is why this setting can be used. This protection can also be disabled by setting request_max_parameters to nil.
root_path
Default: nil
The root path of the application.
The root path of the application specifies the actual location of the project sources in your system. This can prove helpful in scenarios where the project was compiled in a specific location different from the final destination where the project sources (and the lib folder) are copied. For instance, platforms like Heroku often fall under this category. By configuring the root path, you can ensure that your application correctly locates the required project sources and avoids any discrepancies arising from inconsistent source paths. This can prevent issues related to missing dependencies or missing app-related files (eg. locales, assets, or templates) and make your application more robust and reliable.
For example, deploying a Marten app on Heroku will usually involves setting the root path as follows:
config.root_path = "/app"
secret_key
Default: ""
A secret key used for cryptographic signing for the considered Marten project.
The secret key should be set to a unique and unpredictable string value. The secret key can be used by Marten to encrypt or sign messages (eg. for cookie-based sessions), or by other authentication applications.
The secret_key setting value must be kept secret. You should never commit this setting value to source control (instead, consider loading it from environment variables for example).
time_zone
Default: Time::Location.load("UTC")
The default time zone used by the application when it comes to storing date times in the database and displaying them. Any Time::Location object can be used.
trailing_slash
Default: :do_nothing
The trailing slash behavior applied in case an incoming request URL does not match any of the configured routes.
This setting allows you to configure whether an HTTP permanent redirect (301) should be issued when an incoming URL that does not match any of the configured routes either ends with a slash or does not. Three values are supported:
:do_nothing- No redirect is issued (this is the default behavior).:add- If the incoming URL does not end with a slash and does not match any routes, a redirect is issued to the same URL with a trailing slash appended.:remove- If the incoming URL ends with a slash and does not match any routes, a redirect is issued to the same URL with the trailing slash removed.
unsupported_http_method_strategy
Default: :deny
The strategy to use when an unsupported HTTP method is encountered.
This setting allows you to configure the strategy to use when a handler processes an unsupported HTTP method. The default strategy is :deny, which means that the application will return a 405 Method Not Allowed response when an unsupported HTTP method is encountered. The other available strategy is :hide, which will results in 404 Not Found responses to be returned instead.
use_x_forwarded_host
Default: false
A boolean indicating whether the X-Forwarded-Host header is used to look for the host. This setting can be enabled if the Marten application is served behind a proxy that sets this header.
use_x_forwarded_port
Default: false
A boolean indicating if the X-Forwarded-Port header is used to determine the port of a request. This setting can be enabled if the Marten application is served behind a proxy that sets this header.
use_x_forwarded_proto
Default: false
A boolean indicating if the X-Forwarded-Proto header is used to determine whether a request is secure. This setting can be enabled if the Marten application is served behind a proxy that sets this header. For example, if such proxy sets this header to https, Marten will assume that the request is secure at the application level only if use_x_forwarded_proto is set to true.
handler400
Default: Marten::Handlers::Defaults::BadRequest
The handler class that should generate responses for Bad Request responses (HTTP 400). Please refer to Error handlers to learn more about error handlers.
handler403
Default: Marten::Handlers::Defaults::PermissionDenied
The handler class that should generate responses for Permission Denied responses (HTTP 403). Please refer to Error handlers to learn more about error handlers.
handler404
Default: Marten::Handlers::Defaults::PageNotFound
The handler class that should generate responses for Not Found responses (HTTP 404). Please refer to Error handlers to learn more about error handlers.
handler500
Default: Marten::Handlers::Defaults::ServerError
The handler class that should generate responses for Internal Error responses (HTTP 500). Please refer to Error handlers to learn more about error handlers.
x_frame_options
Default: "DENY"
The value to use for the X-Frame-Options header when the associated middleware is used. The value of this setting will be used by the Marten::Middleware::XFrameOptions middleware when inserting the X-Frame-Options header in HTTP responses.
Assets settings
Assets settings allow configuring how Marten should interact with assets. These settings are all available under the assets namespace:
config.assets.root = "assets"
config.assets.url = "/assets/"
app_dirs
Default: true
A boolean indicating whether assets should be looked for inside installed application folders. When this setting is set to true, this means that assets provided by installed applications will be collected by the collectassets command (please refer to Asset handling for more details regarding how to manage assets in your project).
dirs
Default: [] of String
An array of directories where assets should be looked for. The order of these directories is important as it defines the order in which assets are searched for.
It should be noted that path objects or symbols can also be used to configure this setting:
config.assets.dirs = [
Path["src/path1/assets"],
:"src/path2/assets",
]
manifests
Default: [] of String
An array of paths to manifest JSON files to use to resolve assets URLs. Manifest files will be used to return the right fingerprinted asset path for a generic path, which can be useful if your asset bundling strategy support this. You can read more about this capability in Asset manifests and fingerprinting.
max_age
Defaults: 3600
Allows to set the max-age directive value used as part of the Cache-Control header that is set by the Marten::Middleware::AssetServing middleware.
root
Default: "assets"
A string containing the absolute path where collected assets will be persisted (when running the collectassets command). By default, assets will be persisted in a folder that is relative to the Marten project's directory. Obviously, this folder should be empty before running the collectassets command in order to not overwrite existing files: assets should be defined as part of your applications' assets folders instead.
This setting is only used if assets.storage is nil.
storage
Default: nil
An optional storage object, which must be an instance of a subclass of Marten::Core::Store::Base. This storage object will be used when collecting asset files to persist them in a given location.
By default this setting value is set to nil, which means that a Marten::Core::Store::FileSystem storage is automatically constructed by using the assets.root and assets.url setting values: in this situation, asset files are collected and persisted in a local directory, and it is expected that they will be served from this directory by the web server running the application.
A specific storage can be set instead to ensure that collected assets are persisted somewhere else in the cloud and served from there (for example in an Amazon's S3 bucket). When this is the case, the assets.root and assets.url setting values are basically ignored and are overridden by the use of the specified storage.
url
Default: "/assets/"
The base URL to use when exposing asset URLs. This base URL will be used by the default Marten::Core::Store::FileSystem storage to construct asset URLs. For example, requesting a css/App.css asset might generate a /assets/css/App.css URL by default.
This setting is only used if assets.storage is nil.
CSRF settings
CSRF settings allow configuring how Cross-Site Request Forgeries (CSRF) attack protection measures are implemented within the considered Marten project. Please refer to Cross-Site Request Forgery protection for more details about this topic.
The following settings are all available under the csrf namespace:
config.csrf.protection_enabled = true
config.csrf.cookie_name = "csrf-token"
cookie_domain
Default: nil
An optional domain to use when setting the CSRF cookie. This can be used to share the CSRF cookie across multiple subdomains for example. For example, setting this option to .example.com will make it possible to send a POST request from a form on one subdomain (eg. foo.example.com) to another subdomain (eg. bar.example.com ).
cookie_http_only
Default: false
A boolean indicating whether client-side scripts should be prevented from accessing the CSRF token cookie. If this option is set to true, Javascript scripts won't be able to access the CSRF cookie.
cookie_max_age
Default: 31_556_952 (approximately one year)
The max age (in seconds) of the CSRF cookie.
cookie_name
Default: "csrftoken"
The name of the cookie to use for the CSRF token. This cookie name should be different than any other cookies created by your application.
cookie_same_site
Default: "Lax"
The value of the SameSite flag to use for the CSRF cookie. Accepted values are "Lax", "Strict", or "None".
cookie_secure
Default: false
A boolean indicating whether to use a secure cookie for the CSRF cookie. Setting this to true will force browsers to send the cookie with an encrypted request over the HTTPS protocol only.
protection_enabled
Default: true
A boolean indicating if the CSRF protection is enabled globally. When set to true, handlers will automatically perform a CSRF check to protect unsafe requests (ie. requests whose methods are not GET, HEAD, OPTIONS, or TRACE). Regardless of the value of this setting, it is always possible to explicitly enable or disable CSRF protection on a per-handler basis. See Cross-Site Request Forgery protection for more details.
session_key
Default: "csrftoken"
The name of the session key to use for the CSRF token. This session key should be different than any other session key created by your application.
This value is only relevant if use_session is set to true.
trusted_origins
Default: [] of String
An array of trusted origins.
These origins will be trusted for CSRF-protected requests (such as POST requests) and they will be used to check either the Origin or the Referer header depending on the request scheme. This is done to ensure that a specific subdomain such as sub1.example.com cannot issue a POST request to sub2.example.com. To enable CSRF-protected requests over different origins, it's possible to add trusted origins to this array. For example https://sub1.example.com can be configured as a trusted domain that way, but it's possible to allow CSRF-protected requests for all the subdomains of a specific domain by using https://*.example.com.
For example:
config.csrf.trusted_origins = [
"https://*.example.com",
"https://other.example.org",
]