我刚刚开始快速后端开发。我注意到 Vapor 并安装了 swift 包管理器(Swiftenv)。我按照其说明通过以下方式安装 Vapor CLI:
wget cli.qutheory.io -O vapor
Run Code Online (Sandbox Code Playgroud)
但不断报错:
Resolving cli.qutheory.io... 138.197.255.240
Connecting to cli.qutheory.io|138.197.255.240|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2016-08-07 10:41:15 ERROR 404: Not Found.
Run Code Online (Sandbox Code Playgroud)
有人面临同样的问题吗?如何摆脱它?
我还尝试了另一种方法:
curl -L cli.qutheory.io -o vapor
Run Code Online (Sandbox Code Playgroud)
输出是:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 38 100 38 0 0 21 0 0:00:01 0:00:01 --:--:-- 21
Run Code Online (Sandbox Code Playgroud)
然后,我运行命令:
chmod +x vapor
Run Code Online (Sandbox Code Playgroud)
进而:
sudo mv …Run Code Online (Sandbox Code Playgroud) 我尝试使用 Vapor 1.5 和 Firebase 旧协议向 Firebase 通知 API 发出 POST 请求,但收到失败响应。
响应为 JSON(node: Node.Node.object(["multicast_id": Node.Node.number(5936281277445399934), "failure": Node.Node.number(0), "canonical_ids": Node.Node.number(0) ), "结果": Node.Node.array([Node.Node.object(["message_id": Node.Node.string("0:1527074314969790%c7ade8b9f9fd7ecd")])]), "成功": Node.Node 。1号)]))
编辑 通过 POSTMan 发出请求失败,并出现错误“请求缺少身份验证密钥(FCM 令牌)”。
class FirebaseRequester {
let fcmLegacyServerKey = "AIzaSyDSuXXXXXXkCafTQay5_r8j3snvVos"
func sendNotification(payLoad: JSON) throws -> Response {
var response: Response?
do {
let responseFCM = try drop.client.post("https://fcm.googleapis.com/fcm/send",
headers: ["Content-Type":"application/json","Authorization": "key\(fcmLegacyServerKey)"],
query: [:],
body: payLoad.makeBody())
response = responseFCM
}catch let error {
let message = error.localizedDescription
logErr.prints(message: message)
throw Abort.custom(status: .badRequest, message: …Run Code Online (Sandbox Code Playgroud) 我在使用 Vapor 3 发送正文包含 JSON 的 POST 请求时遇到问题。我正在使用https://docs.postman-echo.com/来测试它,它使用发送的相同 JSON 进行响应。
我已查看此处的答案,但在编码和内容类型方面出现错误。
router.get("hooray") { req -> Future<View> in
var postHeaders: HTTPHeaders = .init()
postHeaders.add(name: .contentType, value: "application/json")
postHeaders.add(name: .accept, value: "*/*")
postHeaders.add(name: .acceptEncoding, value: "gzip, deflate")
let oneField = singleGet(foo: "barfoobar")
// { foo: "barfoobar" } - JSON string
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try encoder.encode(oneField)
let jsonString = String(data: jsonData, encoding: .utf8)!
let postBody = HTTPBody(string: jsonString)
let httpReq = HTTPRequest(method: …Run Code Online (Sandbox Code Playgroud) 我在 Ubuntu 服务器上使用 Vapor 连接到我的 DigitalOcean 管理的 PostgreSQL 数据库。
从命令行,运行以下命令可以正常工作:
psql postgresql://user:password@host:port/dbname?sslmode=require
Run Code Online (Sandbox Code Playgroud)
但是使用以下代码运行等效内容会给出:
Fatal error: Error raised at top level: NIOOpenSSL.NIOOpenSSLError.handshakeFailed(NIOOpenSSL.OpenSSLError.sslError([Error: 337047686 error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed])): file /home/buildnode/jenkins/workspace/oss-swift-5.1-package-linux-ubuntu-18_04/swift/stdlib/public/core/ErrorType.swift, line 200
Run Code Online (Sandbox Code Playgroud)
这是代码:
psql postgresql://user:password@host:port/dbname?sslmode=require
Run Code Online (Sandbox Code Playgroud)
将传输参数切换为.unverifiedTLS有效。
我需要帮助才能让 Vapor 正常建立 SSL 连接,但我不知道从哪里开始。
这让我很困惑:
\n我有一个模型,我想在其中使用枚举。我首先声明枚举:
\nenum MenuChoices: String, Codable {\n case reachableAt\n case attentionTo\n case reasonVisit\n case reasonProblem\n}\nRun Code Online (Sandbox Code Playgroud)\n然后是我的班级领域:
\n@Enum(key: "menu_choices")\nvar menuChoices: MenuChoices\nRun Code Online (Sandbox Code Playgroud)\n然后我使用迁移在数据库中创建它:
\nstruct CreateUserMenu: Migration { \nfunc prepare(on database: Database) -> EventLoopFuture<Void> {\n return database.enum("menu_choices")\n .case("reachable_at")\n .case("attention_to")\n .case("reason_visit")\n .case("reason_problem")\n .create()\n .flatMap { menu_choices in\n return database.schema("user_menus")\n .id()\n .field("created_at", .datetime, .required)\n .field("updated_at",.datetime, .required)\n .field("deleted_at",.datetime)\n .field("menu_choices", menu_choices)\n .field("be_nl", .string)\n .field("be_fr", .string)\n .field("en_us", .string)\n .create()\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n到目前为止,一切都很好。此迁移有效并且数据库看起来正常。但是当我想添加一些数据以在另一个迁移中为数据库提供种子时,我收到错误:
\nlet test = UserMenu( menuChoices: MenuChoices.reachableAt, beNl: "nl", …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试为我的 Vapor 应用程序创建一个可执行文件,但我无法在我的服务器上运行它。
我的期望是它能够使用我的所有代码和依赖项创建一个应用程序。我当前的参考文献是 .NET 和 Go,您可以在其中从代码创建单个可执行文件并部署此独立文件 - 这使得部署非常简单。
我尝试在 Docker 上编译并执行它,但没有那么幸运 - 它失败了,因为缺少一些 libSwift 东西。
那么,Swift 可以做这样的事情吗?
期待您的答复
我构建了一个 Vapor 4 应用程序,目前部署在本地 Ubuntu 18 服务器虚拟机上,在 NGINX 后面运行并为用户提供服务,没有任何问题。
现在我希望我的一个网络服务器路由通过执行 Bash 命令来对特定的 POST 做出反应Process(这是为了通过slack-cli将消息发送到专用的 Slack 通道,这是我已经用于其他目的的工具,即已经在我的开发机器和 Ubuntu 服务器上配置并运行)。
使用以下代码,当我在本地计算机上运行 Vapor 应用程序时,一切都按预期工作(即:在 POST 到路由之后,预期消息立即出现在 Slack 通道中):
// What follows is placed inside my dedicated app.post route, after checking the response is valid...
let slackCLIPath = "/home/linuxbrew/.linuxbrew/bin/" // This is the slack-cli path on the Linux VM; I swap it with "/opt/homebrew/bin/" when running the app on my local Mac
_ = runShellScript("\(slackCLIPath)slack chat send …Run Code Online (Sandbox Code Playgroud) 我正在使用 Vapor Swift 构建一个开放 AI 聊天流后端。它使用 MacPaw 的OpenAI包装器连接到 Open AI API 。但我不确定如何使用 SSE 将结果流式传输到客户端,而不是作为单个响应。
我当前的代码如下所示:
func postChatStreamHandler(_ req: Request) throws -> EventLoopFuture<Response> {
let openAI = OpenAI(configuration: configuration)
let promise = req.eventLoop.makePromise(of: Data.self)
let query = ChatQuery(model: .gpt4, messages: messages)
openAI.chatsStream(query: query) { partialResult in
switch partialResult {
case .success(let result):
if let detla = result.choices.first?.delta,
let data = try? JSONEncoder().encode(result) {
promise.succeed(data)
}
case .failure(let error):
...
}
} completion: { error in
...
}
return promise.futureResult.map …Run Code Online (Sandbox Code Playgroud) 我正在尝试在过去没有问题的 Vapor/Fluent 应用程序上运行快速命令。我正在运行swift run App migrate,它通常会更新任何迁移。但它抛出了标题中的错误。
我查看了包文件,其中有:
targets: [
.target(
name: "App",
dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
.product(name: "Fluent", package: "fluent"),
.product(name: "Leaf", package: "leaf"),
// .product(name: "SendGrid", package: "sendgrid"),
],
swiftSettings: [
// Enable better optimizations when building in Release configuration. -- Despite the use of
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
// builds. See <https://github.com/swift-server/guides#building-for-production> for details.
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
]
),
.target(name: "Run", …Run Code Online (Sandbox Code Playgroud) vapor ×10
swift ×8
vapor-fluent ×2
cryptography ×1
enums ×1
firebase ×1
http ×1
ios ×1
json ×1
nstask ×1
openai-api ×1
postgresql ×1
random ×1
ssl ×1
xcode ×1