Skip to content

Configuration

Ludovic Muller edited this page Feb 1, 2023 · 13 revisions

Extend from other configs

It's possible to inherit from other configuration files with the extends property. Multiple files can be given. The value is always an array. All files are merged in sequence starting at index 0 of the extends array. The current config is the last one merged to the complete config.

YAML JSON
extends:
  - trifid:config-sparql.json
  - trifid:config-sparql-addons.json
{
  "extends": [
    "trifid:config-sparql.json",
    "trifid:config-sparql-addons.json"
  ]  
}

Server

All server-related settings can be found under the server property. The listener section contains all information about the listener. Express settings are defined in the express section.

YAML JSON
server:
  listener:
    port: 8080
    host: 0.0.0.0
  logLevel: info
  express:
    trust proxy: loopback
{
  "server": {
    "listener": {
      "port": 8080,
      "host": "0.0.0.0"
    },
    "logLevel": "info",
    "express": {
      "trust proxy": "loopback"
    }
  }
}

Middlewares

All middlewares are defined in the middlewares property. The value of the property is an object. Each middleware instance uses it own key. The order of loading the middlewares is defined by the value of the order property. The module property points to the module to load. A string is used that will be processed by ESM import().

Some additional properties:

  • paths: can either be a string or an array of strings: the paths where to load this particular middleware
  • methods: can either be a string or an array of strings: the HTTP methods to use for this specific middleware (by default, the middleware will be called on each call)
  • hosts: can either be a string or an array of strings: the hosts where to load this particular middleware
YAML JSON
middlewares:
  templateEngine:
    order: 10
    module: trifid-plugin-cotton-candy
{
  "middlewares": {
    "templateEngine": {
      "order": 10,
      "module": "trifid-plugin-cotton-candy"
    }
  }
}

Configure middlewares

Properties defined in globals are merged into the scoped config.

The value given in globals can be overwritten in the scoped config.

Using a null-value will prevent merging the specific property.

YAML JSON
globals:
  endpointUrl: env:SPARQL_ENDPOINT_URL
middlewares:
  templateEngine:
    order: 10
    module: trifid-plugin-cotton-candy
    config:
      endpointUrl: env:ENDPOINT_URL
      empty: null
{
  "globals": {
    "endpointUrl": "env:SPARQL_ENDPOINT_URL"
  },
  "middlewares": {
    "templateEngine": {
      "order": 10,
      "module": "trifid-plugin-cotton-candy",
      "config": {
        "endpointUrl": "env:ENDPOINT_URL",
        "empty": null
      }
    }
  }
}

Middleware Factory

The middleware factory is called with a single argument trifid which will be an object. This object will have a property called config to access a configuration key (it will take globals and add every property from the config part defined in the middleware). To access a particular config field from the middleware (called particularField for example), the following can be used: trifid.config.particularField.

The trifid object also exposes the following fields:

  • logger: a logger to use (default supported log levels: 'fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent') ; the default log level can be configured using the server.logLevel key
  • server: the Express instance (if the middleware needs to do really special stuff)
const factory = (trifid) => {
  const { config, logger } = trifid

  // access a configuration property:
  const { particularField } = config

  // …do things

  return (req, res, next) => {
    // …so some things

    // use the logger for example
    logger.debug('the middleware was called!')
    
  }
}

Template engine

The trifid-core package includes a simple template engine that middlewares can rely on. The objective is to provide an simple, configurable and standard option to serve HTML content.

Here is all configuration fields:

template:
  files:
    main: path/to/another/default/layout.hbs
    header: path/to/another/header.hbs
    footer: path/to/another/footer.hbs
  partials:
    name: path/to/a/partial.hbs
  title: "Default Trifid instance title"
  scripts:
    - path/to/additional/script1.js
    - path/to/additional/script2.js
  styles:
    - path/to/additional/style1.css
    - path/to/additional/style2.css
  disableHeader: false
  disableFooter: false

The trifid-core package will try to do the following:

  1. read all entries in the files property
  2. load each file content, and map it with the key
  3. use the main entry as the default layout, and replace {{{ key }}} with the content of the file specified with that key.
  4. do some Handlebars transformations if some are specified in the layout

The default value of the <title> tag can be set by using the title value.

Some additional scripts and styles can be added by using the specified fields in the configuration.

If a middleware do not want to render the header or the footer part, they can rely on the disableHeader and disableFooter options.

For complex templates, usage of Handlebars partials can be required. Declare partials the same way you declare template files, but in the partials section instead of files. The partial can be accessible from templates by using {{> name}}, where name is the key of the partial you used in the configuration file.

Well-known configuration fields

To have things consistent across the majority of middlewares, here is a list of configuration properties to use:

  • datasetBaseUrl: the base URL for this dataset
  • sparqlEndpoint: an object containing the following fields:
    • url: URL of the SPARQL endpoint
    • username: username to authenticate against the SPARQL endpoint
    • password: password used for authentication
Clone this wiki locally