DEV Community

Bernhard Speiser
Bernhard Speiser

Posted on • Updated on

Building an Express Gateway Policy

This post will show you how to build a policy (middleware) for your express gateway. Before creating a policy, we need to create a plugin.

Creating a plugin

To create a plugin, we need to add a folder plugins/my-plugin for it and a manifest file plugins/my-plugin/manifest.js with the following content

// plugins/my-plugin/manifest.js
module.exports = {
  version: '0.0.1',
  schema: {
    "$id": "https://express-gateway.io/schemas/plugins/my-plugin.json"
  }
}
Enter fullscreen mode Exit fullscreen mode

Once this is done, we need to register the plugin in the system.config.yml file. Depending on your file structure, the file path may contain more or less ../ than this example.

# system.config.yml

plugins:
+  my-plugin:
+    package: '../../../plugins/my-plugin/manifest.js'
Enter fullscreen mode Exit fullscreen mode

Creating the policy

Now that we have a plugin, we can start creating our policy. Add a policies folder plugins/my-plugin/policies and the policy itself plugins/my-plugin/policies/my-policy.js with the following content

// plugins/my-plugin/policies/my-policy.js
module.exports = {
  name: 'my-policy',
  schema: {
    $id: 'http://express-gateway.io/schemas/policies/my-policy.json',
    type: 'object'
  },
  policy: (actionParams) => {
    return (req, res, next) => {
      // TODO: Implement me
      console.log('hello world');
      next();
    };
  }
};
Enter fullscreen mode Exit fullscreen mode

The policy function will be the place where you implement your specific use case.

Now we need to register the policy as part of the plugin

// plugins/my-plugin/manifest.js
module.exports = {
  version: '0.0.1',
+ init: function (pluginContext) {
+   let policy = require('./policies/my-policy')
+   pluginContext.registerPolicy(policy)
+ },
+ policies: ['my-policy'],
  schema: {
    "$id": "https://express-gateway.io/schemas/plugins/my-plugin.json"
  }
}
Enter fullscreen mode Exit fullscreen mode

Using the policy

Finally, we can add the policy as any other default policy in the gateway.config.yml

# gateway.config.yml

policies:
  - log
  - jwt
+ - my-plugin
Enter fullscreen mode Exit fullscreen mode

and use it in our pipelines

# gateway.config.yml

pipelines:
  my-pipeline:
    policies:
+     - my-plugin:
      - log:
        - action:
            message: ${req.method} ${req.originalUrl}
Enter fullscreen mode Exit fullscreen mode

Now we are finished with our policy, but we can still enhance it a bit.

Adding parameters

In the case, that you want to configure your policy (without changing the code) you can specify parameters. The syntax used is called Json Schema. You can have a look at their documentation for the type that you need. In this case, we add one parameter myarray with the type string[]

// plugins/my-plugin/policies/my-policy.js

  schema: {
    $id: 'http://express-gateway.io/schemas/policies/my-policy.json',
-   type: 'object'
+   type: 'object',
+   properties: {
+     myarray: {
+       "type": "array",
+       "items": {
+         "type": "string"
+       }
+     }
+   }
  },
Enter fullscreen mode Exit fullscreen mode

After that, we can pass our value from the pipeline

# gateway.config.yml

    policies:
     - my-plugin:
+       - action:
+           myarray: ['value1', 'value2']
Enter fullscreen mode Exit fullscreen mode

and use it within the policy function: actionParams.myarray.


I hope this helped you understand how to set up a policy in an express gateway pipeline.

Sources/Further Reading

Top comments (0)