vapor

Receiving Websocket data in Swift

孤者浪人 提交于 2021-02-19 06:46:27
问题 I'm carrying this on from this question, since the focus has changed. I am trying to send string data from a vapor server over a websocket. The client side is where the main question is. This code successfully receives the string, which is expected to be JSON (but not absolutely guaranteed -- out of scope). switch message { case .data(let data): print("data: \(data)") case .string(let str): // let data = str.message(using: .utf8) let jsonData = Data(str.utf8) print("string: \(jsonData)") do {

How Do I Send a Dictionary to a Client using Vapor Websockets

纵然是瞬间 提交于 2021-02-10 19:51:27
问题 I've continued this question here, because the focus has changed, but is related. Vapor is communicating between a server and and iOS client. Just setup code, for learning purposes. Now, I want to send a dictionary of values via the established connection using JSON. I'm getting caught up in the unexplained logic of demo code, but here's where I am, in my vapor routes: app.webSocket("respond") { req, ws in ws.onText { ws, text in print(text) let msgDict = "{'name' = 'bobby'}" let encoder =

How Do I Send a Dictionary to a Client using Vapor Websockets

风格不统一 提交于 2021-02-10 19:51:08
问题 I've continued this question here, because the focus has changed, but is related. Vapor is communicating between a server and and iOS client. Just setup code, for learning purposes. Now, I want to send a dictionary of values via the established connection using JSON. I'm getting caught up in the unexplained logic of demo code, but here's where I am, in my vapor routes: app.webSocket("respond") { req, ws in ws.onText { ws, text in print(text) let msgDict = "{'name' = 'bobby'}" let encoder =

vapor - Obtaining data from mysql using alsodecode()

怎甘沉沦 提交于 2021-02-10 15:59:24
问题 I'm trying to obtain data from a mysql database joining two of my tables (usuarios and colegios) with the function alsodecode(). My code looks like this: func usuariosHandler(_ req: Request) throws -> Future<View> { return Usuario.query(on: req).join(\Colegio.id, to: \Usuario.colegioId).alsoDecode(Colegio.self).all().flatMap(to: View.self) { usuarios in let contexto = UsuariosContext(title: "Usuarios de la aplicación", usuarios: usuarios) return try req.view().render("usuarios", contexto) } }

How to prevent SQL Injections with User-Search-Terms in Vapor 4 (Fluent 4)

不想你离开。 提交于 2021-02-10 12:48:45
问题 I am currently implementing a Vapor 4 application, which will be used to manage machines. The user should be able to search for a machine name, which I accomplished by .filter(Machine.path(for: \Machine.$name), .contains(inverse: false, .anywhere), term) where term is an arbitrary String provided by the user. The code itself works as intended, but I was wondering if there is the possibility of a SQL Injection vulnerability (or other attacks). My Question: Is SQL Injection (or other attacks)

Vapor 3 - How to check for similar email before saving object

给你一囗甜甜゛ 提交于 2021-01-27 14:52:12
问题 I would like to create a route to let users update their data (e.g. changing their email or their username). To make sure a user cannot use the same username as another user, I would like to check if a user with the same username already exists in the database. I have already made the username unique in the migrations. I have a user model that looks like this: struct User: Content, SQLiteModel, Migration { var id: Int? var username: String var name: String var email: String var password:

Vapor 3 PostgreSQL CRUD without requests http

六眼飞鱼酱① 提交于 2021-01-27 14:48:24
问题 I am creating backend based on Vapor 3.1.10 using Xcode 11.2 and Swift 5.1, database is PostgreSQL 12. I have a question: how to interact with database (CRUD) without POST and GET requests. All tutorials show how to CRUD only based on Request through HTTPS. But what if my app needs to save something in database without interacting with network? Look at my code: import Vapor import FluentPostgreSQL final class Device: PostgreSQLModel { var id: Int? var isWorking: Bool var serial: Int init

Vapor 4 PostgreSQL CRUD without http requests

只愿长相守 提交于 2020-06-29 03:59:10
问题 I am writing backend using Swift 5 and Vapor 4, database is PostgreSQL 12. I have a question: how to interact with database (CRUD) locally, without POST or GET requests? All tutorials show how to CRUD only based on Request through HTTPS again. I already asked this question: Vapor 3 PostgreSQL CRUD without requests http BUT IT'S NOT A DUPLICATE QUESTION, Vapor 4 works completely different! Please, look at my current, working class for Vapor 3 (based on this answer: Is is possible to use Vapor

Is is possible to use Vapor 3 Postgres Fluent in a standalone script?

回眸只為那壹抹淺笑 提交于 2020-05-27 04:52:30
问题 I am experimenting with a standalone script that will query a Postgres database using Vapor and Fluent. In a normal Vapor API application this is simply done by: router.get("products") { request in return Product.query(on: request).all() } However, in a standalone script, since there is no "request", I get stuck on what to replace the "request" or DatabaseConnectable with. Here's where I get stuck on: import Fluent import FluentPostgreSQL let databaseConfig = PostgreSQLDatabaseConfig(hostname