Configuration
This page shows how configuration works in Vix Core.
Use it when you want to configure the server port, request timeout, I/O threads, session timeout, WAF, TLS, logging, benchmark mode, or database settings.
Public header
#include <vix.hpp>You can also include the configuration header directly:
#include <vix/config/Config.hpp>What configuration provides
vix::config::Config is the configuration object used by Vix Core.
It provides typed access to:
- server port
- request timeout
- I/O thread count
- session timeout
- benchmark mode
- logging options
- WAF options
- TLS options
- database options
- environment-based configuration
- dotted key access
- runtime overrides
Most applications can start with the default configuration.
vix::App app;
app.run(8080);For advanced setup, create a config object and pass it to the app.
vix::config::Config cfg;
cfg.setServerPort(8080);
vix::App app;
app.run(cfg);Basic configuration
#include <vix.hpp>
int main()
{
vix::config::Config cfg;
cfg.setServerPort(8080);
vix::App app;
app.get("/", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.text("configured app");
});
app.run(cfg);
return 0;
}Run:
vix run main.cppThen open:
http://localhost:8080Default configuration
A default app owns a default Config.
vix::App app;You can access it with:
app.config();Example:
app.config().setServerPort(8080);Then run:
app.run(app.config());Or simply pass the port to run.
app.run(8080);Construct a Config
Create a config object with the default path.
vix::config::Config cfg;You can also provide a config path.
vix::config::Config cfg{".env.production"};When a path is provided, Vix can use it to determine layered environment loading.
Environment loading
Config loads environment values using the Vix env module.
The default config path is:
.envWhen an environment-specific file is used, Vix can load layered files.
Example:
vix::config::Config cfg{".env.production"};This can load environment-specific values for production.
Server port
Use setServerPort(...) to set the server port.
vix::config::Config cfg;
cfg.setServerPort(8080);Read the port with:
const int port = cfg.getServerPort();Run with the config:
vix::App app;
app.run(cfg);Port from App
You can also pass the port directly.
vix::App app;
app.run(8080);This is the simplest option for local development.
Port 0
Port 0 means the operating system chooses an available port.
vix::App app;
app.listen_port(0, [](int port)
{
vix::print("listening on", port);
});
app.wait();This is useful for tests.
Request timeout
Use getRequestTimeout() to read the configured request timeout in milliseconds.
const int timeout_ms = cfg.getRequestTimeout();This value can be used by Core and higher-level systems to control request behavior.
I/O threads
Use getIOThreads() to read the configured I/O thread count.
const int io_threads = cfg.getIOThreads();A value of 0 means automatic selection.
0 -> autoThe HTTP server can use hardware concurrency when auto mode is selected.
Session timeout
Use getSessionTimeoutSec() to read the session timeout in seconds.
const int timeout_sec = cfg.getSessionTimeoutSec();Sessions use this value to protect connections from staying active forever.
Conceptually:
session work starts
-> timer starts
-> timeout fires
-> connection closesBenchmark mode
Use isBenchMode() to check whether benchmark mode is enabled.
if (cfg.isBenchMode())
{
vix::print("bench mode enabled");
}Benchmark mode can reduce some runtime overhead for measuring HTTP performance.
Normal applications usually keep benchmark mode disabled.
Logging configuration
The config exposes logging-related values.
cfg.getLogAsync();
cfg.getLogQueueMax();
cfg.getLogDropOnOverflow();These control:
| API | Purpose |
|---|---|
getLogAsync() | Whether async logging is enabled. |
getLogQueueMax() | Maximum async log queue size. |
getLogDropOnOverflow() | Whether logs may be dropped when the queue is full. |
The app also reads logging behavior from environment variables such as VIX_LOG_LEVEL and VIX_LOG_FORMAT.
WAF configuration
Vix Core includes basic request filtering options.
cfg.getWafMode();
cfg.getWafMaxTargetLen();
cfg.getWafMaxBodyBytes();These control request checks in the session layer.
| API | Purpose |
|---|---|
getWafMode() | WAF mode: off, basic, or strict. |
getWafMaxTargetLen() | Maximum allowed target length. |
getWafMaxBodyBytes() | Maximum body bytes checked by WAF logic. |
WAF modes
The WAF mode can be:
off
basic
strictUse off when you want to disable WAF checks.
Use basic for lightweight checks.
Use strict when you want stronger checks.
The exact behavior depends on the session request validation path.
TLS configuration
TLS settings are exposed through Config.
cfg.isTlsEnabled();
cfg.getTlsCertFile();
cfg.getTlsKeyFile();
cfg.getTlsConfig();These values are used by HTTPServer and TlsSession.
Enable TLS
Use config keys to enable TLS.
vix::config::Config cfg;
cfg.setServerPort(8443);
cfg.set("server.tls.enabled", true);
cfg.set("server.tls.cert_file", "/etc/letsencrypt/live/example.com/fullchain.pem");
cfg.set("server.tls.key_file", "/etc/letsencrypt/live/example.com/privkey.pem");Then run:
vix::App app;
app.run(cfg);Get TLS config
Use getTlsConfig() to get a complete TLS configuration object.
const auto tls = cfg.getTlsConfig();
if (tls.is_valid())
{
vix::print("TLS is configured");
}The returned type is:
vix::server::TlsConfigIt contains:
bool enabled;
std::string cert_file;
std::string key_file;Plain HTTP configuration
For plain HTTP, keep TLS disabled.
vix::config::Config cfg;
cfg.setServerPort(8080);
cfg.set("server.tls.enabled", false);Then run:
app.run(cfg);The server accepts plain HTTP connections.
http://localhost:8080HTTPS configuration
For HTTPS, enable TLS and provide certificate paths.
vix::config::Config cfg;
cfg.setServerPort(8443);
cfg.set("server.tls.enabled", true);
cfg.set("server.tls.cert_file", "/etc/letsencrypt/live/example.com/fullchain.pem");
cfg.set("server.tls.key_file", "/etc/letsencrypt/live/example.com/privkey.pem");Then run:
app.run(cfg);The server accepts HTTPS connections.
https://localhost:8443Dotted keys
Use set(...) with dotted keys to override configuration values.
cfg.set("server.port", 8080);
cfg.set("server.tls.enabled", true);
cfg.set("server.tls.cert_file", "/path/fullchain.pem");
cfg.set("server.tls.key_file", "/path/privkey.pem");Dotted keys are useful for structured configuration.
Check a key
Use has(...).
if (cfg.has("server.port"))
{
vix::print("server port is set");
}Get an integer value
Use getInt(...).
const int port = cfg.getInt("server.port", 8080);The second argument is the fallback value.
Get a boolean value
Use getBool(...).
const bool tls_enabled = cfg.getBool("server.tls.enabled", false);Get a string value
Use getString(...).
const std::string cert =
cfg.getString("server.tls.cert_file", "");Database configuration
Config exposes database-related accessors.
cfg.getDbHost();
cfg.getDbUser();
cfg.getDbName();
cfg.getDbPort();
cfg.getDbPasswordFromEnv();These values are useful for modules or applications that need database configuration.
Database password
Database passwords are read from environment variables.
The config checks common names such as:
VIX_DB_PASSWORD
DATABASE_DEFAULT_PASSWORD
DB_PASSWORD
MYSQL_PASSWORDIf none is set, it falls back to the default value.
Environment variables
Common environment variables include:
SERVER_PORT=8080
SERVER_REQUEST_TIMEOUT=2000
SERVER_IO_THREADS=0
SERVER_SESSION_TIMEOUT_SEC=30
SERVER_BENCH_MODE=false
SERVER_TLS_ENABLED=false
SERVER_TLS_CERT_FILE=
SERVER_TLS_KEY_FILE=
LOGGING_ASYNC=true
LOGGING_QUEUE_MAX=20000
LOGGING_DROP_ON_OVERFLOW=true
WAF_MODE=basic
WAF_MAX_TARGET_LEN=4096
WAF_MAX_BODY_BYTES=1048576Database-related variables can include:
DATABASE_DEFAULT_HOST=localhost
DATABASE_DEFAULT_USER=root
DATABASE_DEFAULT_NAME=
DATABASE_DEFAULT_PORT=3306
DATABASE_DEFAULT_PASSWORD=Example .env
SERVER_PORT=8080
SERVER_REQUEST_TIMEOUT=2000
SERVER_IO_THREADS=0
SERVER_SESSION_TIMEOUT_SEC=30
SERVER_BENCH_MODE=false
WAF_MODE=basic
WAF_MAX_TARGET_LEN=4096
WAF_MAX_BODY_BYTES=1048576
SERVER_TLS_ENABLED=false
SERVER_TLS_CERT_FILE=
SERVER_TLS_KEY_FILE=
LOGGING_ASYNC=true
LOGGING_QUEUE_MAX=20000
LOGGING_DROP_ON_OVERFLOW=trueExample .env.production
SERVER_PORT=8443
SERVER_REQUEST_TIMEOUT=5000
SERVER_IO_THREADS=0
SERVER_SESSION_TIMEOUT_SEC=60
SERVER_BENCH_MODE=false
WAF_MODE=strict
WAF_MAX_TARGET_LEN=4096
WAF_MAX_BODY_BYTES=1048576
SERVER_TLS_ENABLED=true
SERVER_TLS_CERT_FILE=/etc/letsencrypt/live/example.com/fullchain.pem
SERVER_TLS_KEY_FILE=/etc/letsencrypt/live/example.com/privkey.pem
LOGGING_ASYNC=true
LOGGING_QUEUE_MAX=50000
LOGGING_DROP_ON_OVERFLOW=trueLoad it with:
vix::config::Config cfg{".env.production"};App config access
The app owns a mutable config.
vix::App app;
auto &cfg = app.config();You can update it before starting the server.
app.config().setServerPort(8080);
app.run(app.config());Run with explicit config
#include <vix.hpp>
int main()
{
vix::config::Config cfg;
cfg.setServerPort(8080);
cfg.set("server.tls.enabled", false);
vix::App app;
app.get("/", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.text("hello");
});
app.run(cfg);
return 0;
}Listen with explicit config
#include <vix.hpp>
int main()
{
vix::config::Config cfg;
cfg.setServerPort(8080);
vix::App app;
app.get("/", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.text("hello");
});
app.listen(cfg);
app.wait();
return 0;
}Configuration and HTTPServer
The HTTP server receives the config from the app.
App
-> Config
-> HTTPServerThe server uses config for:
- port
- TLS
- benchmark mode
- session behavior
- WAF behavior
Configuration and Session
Sessions also use config.
Session
-> ConfigSession behavior can depend on:
- session timeout
- benchmark mode
- WAF mode
- WAF limits
- TLS behavior through transport selection
Configuration and TLS
The HTTP server checks TLS configuration when handling a client.
HTTPServer
-> Config::getTlsConfig
-> TlsConfig::is_valid
-> Session or TlsSessionIf TLS is valid, the server creates a TLS session.
If TLS is disabled, the server creates a normal session.
Configuration and logging
The app initializes logging during startup.
Environment variables can control logging behavior.
Common examples:
VIX_LOG_LEVEL=debug
VIX_LOG_FORMAT=json
VIX_LOG_ASYNC=true
VIX_INTERNAL_LOGS=false
VIX_ACCESS_LOGS=trueThese are useful for development and production diagnostics.
Configuration and benchmark mode
Benchmark mode can be enabled through environment configuration.
SERVER_BENCH_MODE=trueor through a config key:
cfg.set("server.bench_mode", true);Benchmark mode is useful for performance tests.
It should normally be disabled for regular applications.
Configuration and WAF
WAF behavior can be configured through environment values.
WAF_MODE=basic
WAF_MAX_TARGET_LEN=4096
WAF_MAX_BODY_BYTES=1048576In the session layer, these values can block malformed or suspicious requests before they reach user handlers.
Complete plain HTTP example
#include <vix.hpp>
int main()
{
vix::config::Config cfg;
cfg.setServerPort(8080);
cfg.set("server.tls.enabled", false);
cfg.set("server.session_timeout_sec", 30);
cfg.set("waf.mode", "basic");
vix::App app;
app.get("/", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.text("plain HTTP app");
});
app.get("/api/status", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.json({
{"status", "ok"},
{"tls", false}
});
});
app.run(cfg);
return 0;
}Complete HTTPS example
#include <vix.hpp>
int main()
{
vix::config::Config cfg{".env.production"};
cfg.setServerPort(8443);
cfg.set("server.tls.enabled", true);
cfg.set("server.tls.cert_file", "/etc/letsencrypt/live/example.com/fullchain.pem");
cfg.set("server.tls.key_file", "/etc/letsencrypt/live/example.com/privkey.pem");
vix::App app;
app.get("/", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.text("HTTPS app");
});
app.get("/api/status", [](vix::Request &req, vix::Response &res)
{
(void)req;
res.json({
{"status", "ok"},
{"tls", true}
});
});
app.run(cfg);
return 0;
}API summary
| API | Purpose |
|---|---|
Config(path) | Construct config with an optional config path. |
loadConfig() | Reload configuration from environment files. |
set(key, value) | Set a dotted configuration value. |
has(key) | Check whether a dotted key exists. |
getInt(key, fallback) | Read an integer config value. |
getBool(key, fallback) | Read a boolean config value. |
getString(key, fallback) | Read a string config value. |
setServerPort(port) | Set the HTTP server port. |
getServerPort() | Return the HTTP server port. |
getRequestTimeout() | Return request timeout in milliseconds. |
getIOThreads() | Return configured I/O threads. |
isBenchMode() | Return whether benchmark mode is enabled. |
getSessionTimeoutSec() | Return session timeout in seconds. |
getWafMode() | Return WAF mode. |
getWafMaxTargetLen() | Return maximum WAF target length. |
getWafMaxBodyBytes() | Return maximum WAF body bytes. |
isTlsEnabled() | Return whether TLS is enabled. |
getTlsCertFile() | Return TLS certificate path. |
getTlsKeyFile() | Return TLS private key path. |
getTlsConfig() | Return complete TLS configuration. |
getLogAsync() | Return async logging setting. |
getLogQueueMax() | Return async log queue max size. |
getLogDropOnOverflow() | Return log overflow behavior. |
getDbHost() | Return database host. |
getDbUser() | Return database user. |
getDbName() | Return database name. |
getDbPort() | Return database port. |
getDbPasswordFromEnv() | Return database password from environment. |
Best practices
Use direct app.run(port) for simple local development.
app.run(8080);Use Config when you need TLS, WAF, timeouts, or production settings.
vix::config::Config cfg{".env.production"};
app.run(cfg);Keep secrets in environment variables, not source code.
DATABASE_DEFAULT_PASSWORD=...Use TLS through a reverse proxy when certificate management should stay outside the app.
Nginx or Caddy -> VixUse direct TLS when the Vix process should terminate HTTPS itself.
cfg.set("server.tls.enabled", true);Use benchmark mode only for performance testing.
SERVER_BENCH_MODE=trueNext steps
Read the next pages: