注
: heroku-18部署失败的解决方案:heroku-16 和 heroku-18的相互切换
- 下载管理工具postgresapp 和 postico : 两个协同工作,都需要
- 安装两个软件后进行设置
1). 设置postgresapp
image.png
2).点击start
按钮后如下图所示
image.png
3)设置postico
image.png
- configure.swift文件的代码如下
import FluentSQLite
import Vapor
import FluentPostgreSQL
import Leaf
/// Called before your application initializes.
public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
/// Register providers first
try services.register(FluentPostgreSQLProvider())
try services.register(LeafProvider())
/// Register routes to the router
let router = EngineRouter.default()
try routes(router)
services.register(router, as: Router.self)
/// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(ErrorMiddleware.self) // Catches errors and converts to HTTP response
services.register(middlewares)
var databases = DatabasesConfig()
var databaseConfig:PostgreSQLDatabaseConfig
if let url = Environment.get("DATABASE_URL") { // 这是部署到heroku的生产环境数据库地址(heroku配置图示本文会在下面给出图示)
databaseConfig = PostgreSQLDatabaseConfig(url: url)!
}
else { // 这是本地数据库配置
databaseConfig = PostgreSQLDatabaseConfig(hostname: "localhost",
username: "liuhongli",
database: "postgres")
}
let database = PostgreSQLDatabase(config: databaseConfig)
databases.add(database: database, as: .psql)
services.register(databases)
var migrations = MigrationConfig()
migrations.setupModels()
services.register(migrations)
config.prefer(LeafRenderer.self, for: ViewRenderer.self)
}
如果需要密码可以这样写:
if let url = Environment.get("DATABASE_URL") {
databaseConfig = PostgreSQLDatabaseConfig(url: url)!
}
else {
databaseConfig = PostgreSQLDatabaseConfig(hostname: "localhost",
username: "liuhongli",
database: "postgres",
password: "123456")
}
-
下图是vapor项目中代码配置
image.png
-
下面是heroku的配置:
image.png
网友评论