Skip to content

@karr/config

This package handles all the config imports.

Karr can be configured either through environment variables, or with a config file for a more persistent approach.

Environment variables

Any field from the config file can be set with an envvar.

They follow the same naming as their config file counterpart. The only exception is the db config, which is prefixed by DB_, e.g. DB_NAME, DB_PASSWORD, DB_PASSWORD_FILE

Config file

By default the config file should be located in a config/ directory at the root of the project and named karr.config.*. These can be overridden by the CONFIG_DIR and CONFIG_FILENAME environment variables respectively.

The config file can be in .yaml, .yml, .json5 or .json format.

You would typically have your local database credentials in this config file.

config/karr.config.yaml
API_PORT: 1993
API_BASE: http://localhost:1993
LOG_TIMESTAMP: true
LOG_LEVEL: debug
ADMIN_EMAIL: foo@bar.com
DB_CONFIG:
# Only DB_CONFIG keys are lowercase
host: localhost
port: 5432
user: karr
password: password
db_name: karr

With this configuration file, the directory structure would look like this:

  • Directoryapps
    • Directoryweb/
    • Directoryapi/
  • Directoryconfig
    • karr.config.yaml
  • Directorypackages
    • Directoryconfig/
    • Directoryutil/

Exports

The config package has a general export for all config values. The DB config isn’t exported here to avoid leaking credentials.

import { API_PORT,
API_BASE,
LOG_TIMESTAMP,
LOG_LEVEL,
ADMIN_EMAIL,
API_VERSION,
APPLICATION_NAME,
PRODUCTION } from "@karr/config"; // these are all the config values
console.log(API_PORT);

It also has a static import to explicitly import only the hard-coded config values. These values are defined in src/static.ts.

import { APPLICATION_NAME, API_VERSION } from "@karr/config/static";
console.log(APPLICATION_NAME, API_VERSION);