美文网首页
Vapor swift开发数据库安装和heroku设置

Vapor swift开发数据库安装和heroku设置

作者: 小熊翻译App | 来源:发表于2019-03-05 10:45 被阅读0次

    : heroku-18部署失败的解决方案:heroku-16 和 heroku-18的相互切换

    1. 下载管理工具postgresapppostico : 两个协同工作,都需要
    2. 安装两个软件后进行设置
      1). 设置postgresapp
      image.png
      2).点击start按钮后如下图所示
      image.png

    3)设置postico

    image.png
    image.png
    1. 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")
        }
    
    1. 下图是vapor项目中代码配置


      image.png
    1. 下面是heroku的配置:


      image.png
    image.png image.png image.png image.png

    相关文章

      网友评论

          本文标题:Vapor swift开发数据库安装和heroku设置

          本文链接:https://www.haomeiwen.com/subject/pxeuaftx.html