Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何配置Fluent/MySQL数据库连接而不将密码输入Vapor 3中的configure.swift?_Swift_Vapor - Fatal编程技术网

如何配置Fluent/MySQL数据库连接而不将密码输入Vapor 3中的configure.swift?

如何配置Fluent/MySQL数据库连接而不将密码输入Vapor 3中的configure.swift?,swift,vapor,Swift,Vapor,除了“将DatabasesConfigstruct注册到您的服务中”之外,没有太多关于数据库配置的内容 教程(如)建议您在App/configure.swift文件中实现如下配置: let mysqlConfig = MySQLDatabaseConfig( hostname: "127.0.0.1", port: 3306, username: "root", password: "root", database: "mycooldb" ) servic

除了“将
DatabasesConfig
struct注册到您的服务中”之外,没有太多关于数据库配置的内容

教程(如)建议您在
App/configure.swift
文件中实现如下配置:

let mysqlConfig = MySQLDatabaseConfig(
    hostname: "127.0.0.1",
    port: 3306,
    username: "root",
    password: "root",
    database: "mycooldb"
)
services.register(mysqlConfig)
但是git正在跟踪我的
configure.swift
文件,我不想提交我的用户名和密码

如何提供用于处理数据库连接的外部配置文件


Vapor的早期版本似乎使用了JSON配置文件。这个功能完全消失了吗?我在当前的文档中找不到任何提到它的地方。

您可以添加一个环境变量,然后像这样加载它:

Environment.get("MY_VAR")

最常用的方法是使用环境变量。您可以在Xcode方案或终端中设置它们:

export DB_PASSWORD=root
然后将其放入您的配置中:

guard let password = Environment.get("DB_PASSWORD") else {
    throw Abort(.internalServerError)
}

这个问题还不清楚,但应该注意的是,这对iOS应用程序不起作用,只适用于macOS,因为除了使用Xcode进行开发外,您无法为iOS应用程序设置环境变量。没错,但这目前不是问题,因为此时Vapor甚至没有在iOS上运行。