From 145f68268a059f3828b36d6b530e186efacc0eba Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 23 Jan 2022 20:49:06 +0100 Subject: [PATCH] Configure first WebSocket test --- Package.swift | 31 +++++++++++++++++++++++++++++++ Sources/App/configure.swift | 7 +++++++ Sources/App/routes.swift | 31 +++++++++++++++++++++++++++++++ Sources/Run/main.swift | 9 +++++++++ Tests/AppTests/AppTests.swift | 15 +++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 Package.swift create mode 100755 Sources/App/configure.swift create mode 100755 Sources/App/routes.swift create mode 100644 Sources/Run/main.swift create mode 100644 Tests/AppTests/AppTests.swift diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..7974942 --- /dev/null +++ b/Package.swift @@ -0,0 +1,31 @@ +// swift-tools-version:5.5 +import PackageDescription + +let package = Package( + name: "SesameServer", + platforms: [ + .macOS(.v10_15) + ], + dependencies: [ + .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), + ], + targets: [ + .target( + name: "App", + dependencies: [ + .product(name: "Vapor", package: "vapor") + ], + 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 for details. + .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release)) + ] + ), + .executableTarget(name: "Run", dependencies: [.target(name: "App")]), + .testTarget(name: "AppTests", dependencies: [ + .target(name: "App"), + .product(name: "XCTVapor", package: "vapor"), + ]) + ] +) diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift new file mode 100755 index 0000000..52fcba5 --- /dev/null +++ b/Sources/App/configure.swift @@ -0,0 +1,7 @@ +import Vapor + +// configures your application +public func configure(_ app: Application) throws { + app.http.server.configuration.port = 10000 + try routes(app) +} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift new file mode 100755 index 0000000..adcdf89 --- /dev/null +++ b/Sources/App/routes.swift @@ -0,0 +1,31 @@ +import Vapor + +var connection: WebSocket? + +func routes(_ app: Application) throws { + + app.get { req in + return "It works!" + } + + /** + Start a new websocket connection for the client to receive table updates from the server + - Returns: Nothing + - Note: The first (and only) message from the client over the connection must be a valid session token. + */ + app.webSocket("listen") { req, socket in + socket.onBinary { socket, data in + print("\(data)") + } + socket.onText { socket, text in + print(text) + } + + _ = socket.onClose.always { result in + connection = nil + print("Socket closed") + } + connection = socket + print("Socket connected") + } +} diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift new file mode 100644 index 0000000..373be5f --- /dev/null +++ b/Sources/Run/main.swift @@ -0,0 +1,9 @@ +import App +import Vapor + +var env = try Environment.detect() +try LoggingSystem.bootstrap(from: &env) +let app = Application(env) +defer { app.shutdown() } +try configure(app) +try app.run() diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift new file mode 100644 index 0000000..9817630 --- /dev/null +++ b/Tests/AppTests/AppTests.swift @@ -0,0 +1,15 @@ +@testable import App +import XCTVapor + +final class AppTests: XCTestCase { + func testHelloWorld() throws { + let app = Application(.testing) + defer { app.shutdown() } + try configure(app) + + try app.test(.GET, "hello", afterResponse: { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.body.string, "Hello, world!") + }) + } +}