项目编写好后,我们需要部署到服务器上,为了保持进程保活,使用了 supervisor 工具。假设有配置:
# /etc/supervisor/conf.d/xxx-host-com.conf
[program:xxx-host-com]
command=/www/wwwroot/xxx-host-com/hello serve --env production --auto-migrate --port 11802
directory=/www/wwwroot/xxx.host.com
autostart=true
autorestart=true
user=root
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log
其中有运行项目的指令:
/www/wwwroot/xxx-host-com/hello serve --env production --auto-migrate --port 11802
-
/www/wwwroot/xxx-host-com/hello:这是可执行文件路径,通常是 Vapor 项目构建出来的二进制文件。
-
serve:表示启动服务器。
-
--env production:指定运行环境为生产环境。默认环境为开发(development)。
-
--auto-migrate:自动运行数据库迁移(migrations),常用于确保数据库结构是最新的。
-
--port 11802:指定服务监听的端口为 11802。
上面这些命令的配置均可从官方文档了解到,这里不在继续说明。
我现在 vscode 和 xcode 均会使用,那么这些参数在这些工具上该如何传递运行参数,且区分环境?这些文档中没有细写,本文在这里做下介绍。
区分环境
环境配置可前往官方文档:docs.vapor.codes/zh/basics/e…
这边建议通过 dotenv 方式管理环境,这样可以灵活按需变动。
在终端指定环境是非常容易的,但如何在工具中指定环境呢?
vscode
vscode 运行 vapor 项目,vscode 会有个相关配置生成,在.vscode目录下查看。
而我们要指定配置,需要修改 launch.json:
{
"configurations": [
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:hello}",
"name": "Debug hello",
"program": "${workspaceFolder:hello}/.build/debug/hello",
"preLaunchTask": "swift: Build Debug hello",
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:hello}",
"name": "Release hello",
"program": "${workspaceFolder:hello}/.build/release/hello",
"preLaunchTask": "swift: Build Release hello"
}
]
}
细读下配置,我们很容易知道,这是两个环境的运行配置,通常我们只需更改 Debug 的运行环境,然后关注下 args 这个就是我们填写运行参数的地方。
{
"configurations": [
{
"type": "swift",
"request": "launch",
"args": [
"serve",
"--auto-migrate",
"-b",
"0.0.0.0:11802",
"-e",
"development",
"--log",
"debug"
],
"cwd": "${workspaceFolder:hello}",
"name": "Debug hello",
"program": "${workspaceFolder:hello}/.build/debug/hello",
"preLaunchTask": "swift: Build Debug hello",
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:hello}",
"name": "Release hello",
"program": "${workspaceFolder:hello}/.build/release/hello",
"preLaunchTask": "swift: Build Release hello"
}
]
}
这样就配置好了。
xcode
如果你使用 Xcode,需要通过编辑 App scheme 配置参数。
然后:
配置完成后,自行运行检查下。
更多最新文章,请关注微信公众号:OldBirds,老鸟们的聚集地