Strapi-使用环境变量进行配置

Strapi - Configure with environment variables

本文关键字:配置 环境变量 Strapi-      更新时间:2023-09-26

使用strapi 1.5.4

是否可以使用环境变量配置strapi?如果没有,如何在不提交/暴露数据库凭据和其他机密的情况下配置strapi?

module.exports = {
  "orm": {
    "adapters": {
      "disk": "sails-disk",
      "mysql": "sails-mysql"
    },
    "defaultConnection": "default",
    "connections": {
      "default": {
        "adapter": "disk",
        "filePath": ".tmp/",
        "fileName": "default.db",
        "migrate": "alter"
      },
      "permanent": {
        "adapter": "mysql",
"user": process.env.DB_USER,
"password": process.env.DB_PASSWORD,
        "migrate": "alter"
      }
    }
  }
};

看起来唯一的方法就是使用钩子。在我的server.js文件中(我会将配置移到它自己的文件中并清理它)

const orm = {
  "adapters": {
    "disk": "sails-disk",
    "mysql": "sails-mysql"
  },
  "defaultConnection": "default",
  "connections": {
    "default": {
      "adapter": "disk",
      "filePath": ".tmp/",
      "fileName": "default.db",
      "migrate": "alter"
    },
    "permanent": {
      "adapter": "mysql",
      "user": process.env.DB_USER || 'root',
      "password": process.env.DB_PASSWORD || 'password',
      "database": process.env.DB_NAME || 'test',
      "host": "127.0.0.1",
      "migrate": "alter"
    }
  }
};
(function () {
  const strapi = require('strapi');
  // Use a hook to override the config
  strapi.on('hook:_config:loaded', () => {
    strapi.config.orm = orm;
  });
  strapi.start();
})();

您可以使用此插件来管理您的秘密:https://github.com/cyberark/summon上面的插件将为您的秘密值提供更多的抽象,它们也得到了很多提供者的支持。

2021年,Strapi提供了一个开箱即用的解决方案。

https://strapi.io/documentation/developer-docs/latest/setup-deployment-guides/configurations.html#configuration-使用环境变量