标签: vapor

在Fluent + Vapor中查询子项

我对如何查询模型对象的子项并立即使用它感到困惑.我ClientStation儿子很多

final class Client: PostgreSQLModel {
    var stations: Children<Client, Station> {
        return children(\.clientID)
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的控制器中,我有一个客户列表,我想看看并抓住他们的电台.

func clientIndexHandler(_ req: Request) throws -> Future<View> {
    return Client.query(on: req).all().flatMap(to: View.self) { clients in
        let sorted = clients.sorted { ... }
        let content = try sorted.map { client in
            let stations = try client.stations
                                       .query(on: req)
                                       .all()
                                       .map(to: [String].self) { stations in
                return stations.map { $0.name }
            }

            // Now I want to use the stations for other stuff. …
Run Code Online (Sandbox Code Playgroud)

fluent vapor

8
推荐指数
1
解决办法
422
查看次数

蒸汽连接被拒绝 (errno 61)

我在尝试运行 Vapor 3 应用程序时看到以下错误:

线程 1:致命错误:在顶层引发错误:NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "127.0.0.1", port: 3306, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIO.SingleConnectionFailure(target) :[IPv4]127.0.0.1/127.0.0.1:3306,错误:连接重置(错误集):连接被拒绝(错误号:61))]))

它曾经工作得很好。
我能做什么?

swift vapor

8
推荐指数
1
解决办法
2154
查看次数

如何在Swift 3中将response.body从Vapor转换为String?

我正在使用Vapor尝试从另一台服务器获取XML文件,问题是我不知道如何将响应体转换为swift String.

let bikesResponse = try drop.client.get("http://www.c-bike.com.tw/xml/stationlistopendata.aspx")

let bodyBytes = bikesResponse.body
let string = String(bytes) // <-- WHAT DO I DO HERE?
Run Code Online (Sandbox Code Playgroud)

谢谢

vapor

7
推荐指数
1
解决办法
818
查看次数

如何使用JWT for Google firebase生成身份验证令牌?

所以我正在尝试验证Firebase REST API. 我正在使用Vapor框架服务器端swift,我安装了JWT软件包.

我正在尝试使用serviceAccountKey.json文件和JWT中的数据来生成身份验证令牌.

这是我尝试过的代码:

let payload = try JSON(node: [
        "iat": Date().timeIntervalSince1970,
        "exp": Date().timeIntervalSince1970 + 3600,
        "iss": "client_email from serviceAccountKey.json",
        "aud": "https://accounts.google.com/o/oauth2/token",
        "scope": [
            "https://www.googleapis.com/auth/firebase.database",
            "https://www.googleapis.com/auth/userinfo.email"
        ]
    ])
    let privateKey = "copied from serviceAccountKey.json"

    let signer = try HS256(bytes: privateKey.bytes)

    let jwt = try JWT(payload: payload, signer: signer)
    let token = try jwt.createToken()
    print(token)
Run Code Online (Sandbox Code Playgroud)

serviceAccountKey.json

{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": …
Run Code Online (Sandbox Code Playgroud)

oauth jwt firebase swift vapor

7
推荐指数
1
解决办法
1757
查看次数

如何将Vapor 2 Swift 4项目推向Heroku

使用Xcode 9和Swift 4以及vapor heroku push/ vapor heroku init,我收到:

-----> Swift app detected
Cloning into 'swiftenv'...
Swift 3 Heroku Installer
  Version: 3.1.1
  Operating System: ubuntu1404
 Installing Swiftenv
Cloning into '/app/.swiftenv'...
 Installing Swift
Downloading https://swift.org/builds/swift-3.1.1-release/ubuntu1604/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-ubuntu16.04.tar.gz
/tmp/swiftenv-3.1.1- /tmp/build_d
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
/tmp/build_d
3.1.1 has been installed.
?  Done
-----> Installing clang-3.7.0
precompile
-----> Building Package ... this will take a while
swift-build: error: Package requires …
Run Code Online (Sandbox Code Playgroud)

heroku swift vapor xcode9

7
推荐指数
1
解决办法
794
查看次数

如何访问蒸汽中的查询参数3

基本上是标题.我想知道如何在Vapor 3中使用url查询参数.我似乎无法在文档中找到任何内容.

例如/objects?fancy=true,我如何访问fancy参数.

rest query-parameters swift vapor

7
推荐指数
1
解决办法
1634
查看次数

Vapor 4 中的原始查询

在 Vapor 3 中,我能够获取到数据库的 newConnection 并使用以下命令调用原始 sql 查询:

return request.withNewConnection(to: .mysql) { (con) -> EventLoopFuture<T> in  
    return con.raw(sql)...
}
Run Code Online (Sandbox Code Playgroud)

现在我将我的代码迁移到 Vapor 4,函数 raw 或 withNewConnection 消失了,我可以用什么来进行原始查询。

重现步骤

return request.withNewConnection(to: .mysql) { (con) -> EventLoopFuture<T> in  
...
}
Run Code Online (Sandbox Code Playgroud)

错误:无法推断参考成员“mysql”的上下文基础“请求”类型的值没有成员“withNewConnection”

预期行为

在 request.db 中有一个函数可以让我获得新连接或运行原始查询。

环境

  • Vapor 框架版本:4.7.1
  • Vapor 工具箱版本:vapor-beta
  • 操作系统版本:macOS Catalina

vapor

7
推荐指数
2
解决办法
736
查看次数

如何使用 Swift Package Manager 更新单个依赖项?

Package.swift我以以下形式存在本地依赖性

.package(url: "file:///Users/User/Documents/.../my-dependency", .branch("master")),
Run Code Online (Sandbox Code Playgroud)

本地依赖项正在开发中。我希望更新到最新版本,但我无法找到一种方法来仅更新该包,而无需拉取和重建所有其他项目依赖项。到目前为止,每次我对本地依赖项进行小更改时(假设我什至可以访问互联网),我都必须拉取并重建 Vapor 和 Fluent。

当我运行时,swift package update --help我看到有一条指令:

POSITIONAL ARGUMENTS:
packages        The packages to update (optional)
Run Code Online (Sandbox Code Playgroud)

然而,每当我尝试类似swift package update my-dependencyor 的东西时swift package update My-Dependency,它都会做同样的事情并重新拉动所有内容。

我该如何更新这个单一依赖项?假设我最终确实想要更新所有其他第三方依赖项,我如何告诉 SPM 在重新拉取和重建包之前检查 GitHub 上的包是否有实际更改?

swift swift-package-manager vapor

7
推荐指数
1
解决办法
3793
查看次数

Vapor 框架:使用 SSL 配置 postgres 连接

我正在尝试连接到我的 Heroku PostgreSQL 数据库,但出现以下错误:

cannotEstablishConnection("FATAL:  no pg_hba.conf entry for host \"37.167.93.189\", user \"clpnkpyttmdtyq\", database \"d3h6147v73mgtu\", SSL off\n")
Run Code Online (Sandbox Code Playgroud)

我知道 Heroku postgres 数据库需要使用 SSL 连接,但我不知道如何在我的Droplet对象上配置连接。

这是我的postgresql.json配置文件:

{
    "host": "ec2-54-163-224-108.compute-1.amazonaws.com",
    "user": "clpnkpyttmdtyq",
    "password": "99201aa07c48e18e7bdf210937857b85bee37cd8d8cb904381b1ddff934c7a4f",
    "database": "d3h6147v73mgtu",
    "port": 5432
}
Run Code Online (Sandbox Code Playgroud)

也许有ssl我不知道的参数?

我如何添加VaporPostgresSQLProvider

let drop = Droplet()

// Tell the droplet to use our SQL provider service
try drop.addProvider(VaporPostgreSQL.Provider.self)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗 ?

当我尝试使用本地 postgres 数据库时,它可以工作,因为它不需要 ssl 连接。

postgresql ssl heroku vapor

6
推荐指数
1
解决办法
984
查看次数

终端蒸汽更新正在使用不再支持的 Swift 工具版本 3.1.0;改用 4.0.0 或更新版本\n",输出:

我在做教程时遇到了这个错误。我无法解决问题。因此,我删除了该项目并创建了一个新的简单项目来找出问题,但仍然存在相同的问题。我正在发布相关代码 -

package.swift 代码 -

// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "dep",
products: [
    .library(name: "dep", targets: ["App"]),
],
dependencies: [
    //  A server-side Swift web framework.
    .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

    //  Swift ORM (queries, models, relations, etc) built on SQLite      3.
    .package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),
        .package(url: "https://github.com/vapor/leaf.git", from: "3.0.0"),

],
targets: [
    .target(name: "App", dependencies: ["Leaf","FluentSQLite", "Vapor"]),
    .target(name: "Run", dependencies: ["App"]),
    .testTarget(name: "AppTests", dependencies: ["App"])
]
Run Code Online (Sandbox Code Playgroud)

)

当我在终端中执行“vapor build”并按回车键时,终端显示错误 - “”/Users/apple/dep: error: package at …

swift vapor leaf

6
推荐指数
2
解决办法
3763
查看次数