From c2539d22500f0c0c43d7627cb3495f05add4b78d Mon Sep 17 00:00:00 2001 From: Christian Risi <75698846+CnF-Gris@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:33:37 +0000 Subject: [PATCH] Initial Commit It should work for everyone --- .devcontainer/devcontainer.json | 8 ++++++- .gitignore | 2 -- .vscode/launch.json | 22 ++++++++++++++++++ .vscode/settings.json | 1 + Package.swift | 40 ++++++++++++++++++++++++++++++++ Public/.gitkeep | 0 Sources/App/Controllers/.gitkeep | 0 Sources/App/configure.swift | 9 +++++++ Sources/App/entrypoint.swift | 31 +++++++++++++++++++++++++ Sources/App/routes.swift | 11 +++++++++ Tests/AppTests/AppTests.swift | 29 +++++++++++++++++++++++ 11 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 Package.swift create mode 100644 Public/.gitkeep create mode 100644 Sources/App/Controllers/.gitkeep create mode 100644 Sources/App/configure.swift create mode 100644 Sources/App/entrypoint.swift create mode 100644 Sources/App/routes.swift create mode 100644 Tests/AppTests/AppTests.swift diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 55f0d03..58f5dce 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -20,7 +20,13 @@ ], // The WorkspaceFolder inside container - "workspaceFolder": "/workspace" + "workspaceFolder": "/workspace", + + // RunArgs + "runArgs": [ + "--name", + "IoT-Simulator-Vapor" + ] } \ No newline at end of file diff --git a/.gitignore b/.gitignore index ad8058b..78d9cc0 100644 --- a/.gitignore +++ b/.gitignore @@ -108,11 +108,9 @@ DerivedData/ Package.resolved .swiftpm Tests/LinuxMain.swift -.vscode .bash_history .cache/ # API Docs Generation generate-package-api-docs.swift -public/ theme-settings.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..00bed33 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + "configurations": [ + { + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:workspace}", + "name": "Debug IoT-Simulator", + "program": "${workspaceFolder:workspace}/.build/debug/IoT-Simulator", + "preLaunchTask": "swift: Build Debug IoT-Simulator" + }, + { + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "${workspaceFolder:workspace}", + "name": "Release IoT-Simulator", + "program": "${workspaceFolder:workspace}/.build/release/IoT-Simulator", + "preLaunchTask": "swift: Build Release IoT-Simulator" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..ec6a733 --- /dev/null +++ b/Package.swift @@ -0,0 +1,40 @@ +// swift-tools-version:6.0 +import PackageDescription + +let package = Package( + name: "template-bare", + platforms: [ + .macOS(.v13) + ], + dependencies: [ + // 💧 A server-side Swift web framework. + .package(url: "https://github.com/vapor/vapor.git", from: "4.99.3"), + // 🔵 Non-blocking, event-driven networking for Swift. Used for custom executors + .package(url: "https://github.com/apple/swift-nio.git", from: "2.65.0"), + ], + targets: [ + .executableTarget( + name: "App", + dependencies: [ + .product(name: "Vapor", package: "vapor"), + .product(name: "NIOCore", package: "swift-nio"), + .product(name: "NIOPosix", package: "swift-nio"), + ], + swiftSettings: swiftSettings + ), + .testTarget( + name: "AppTests", + dependencies: [ + .target(name: "App"), + .product(name: "XCTVapor", package: "vapor"), + ], + swiftSettings: swiftSettings + ) + ], + swiftLanguageModes: [.v5] +) + +var swiftSettings: [SwiftSetting] { [ + .enableUpcomingFeature("DisableOutwardActorInference"), + .enableExperimentalFeature("StrictConcurrency"), +] } diff --git a/Public/.gitkeep b/Public/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Sources/App/Controllers/.gitkeep b/Sources/App/Controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift new file mode 100644 index 0000000..b84cb15 --- /dev/null +++ b/Sources/App/configure.swift @@ -0,0 +1,9 @@ +import Vapor + +// configures your application +public func configure(_ app: Application) async throws { + // uncomment to serve files from /Public folder + // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) + // register routes + try routes(app) +} diff --git a/Sources/App/entrypoint.swift b/Sources/App/entrypoint.swift new file mode 100644 index 0000000..2e85ece --- /dev/null +++ b/Sources/App/entrypoint.swift @@ -0,0 +1,31 @@ +import Vapor +import Logging +import NIOCore +import NIOPosix + +@main +enum Entrypoint { + static func main() async throws { + var env = try Environment.detect() + try LoggingSystem.bootstrap(from: &env) + + let app = try await Application.make(env) + + // This attempts to install NIO as the Swift Concurrency global executor. + // You can enable it if you'd like to reduce the amount of context switching between NIO and Swift Concurrency. + // Note: this has caused issues with some libraries that use `.wait()` and cleanly shutting down. + // If enabled, you should be careful about calling async functions before this point as it can cause assertion failures. + // let executorTakeoverSuccess = NIOSingletons.unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor() + // app.logger.debug("Tried to install SwiftNIO's EventLoopGroup as Swift's global concurrency executor", metadata: ["success": .stringConvertible(executorTakeoverSuccess)]) + + do { + try await configure(app) + } catch { + app.logger.report(error: error) + try? await app.asyncShutdown() + throw error + } + try await app.execute() + try await app.asyncShutdown() + } +} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift new file mode 100644 index 0000000..2edcc8f --- /dev/null +++ b/Sources/App/routes.swift @@ -0,0 +1,11 @@ +import Vapor + +func routes(_ app: Application) throws { + app.get { req async in + "It works!" + } + + app.get("hello") { req async -> String in + "Hello, world!" + } +} diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift new file mode 100644 index 0000000..aa1e35a --- /dev/null +++ b/Tests/AppTests/AppTests.swift @@ -0,0 +1,29 @@ +@testable import App +import XCTVapor +import Testing + +@Suite("App Tests") +struct AppTests { + private func withApp(_ test: (Application) async throws -> ()) async throws { + let app = try await Application.make(.testing) + do { + try await configure(app) + try await test(app) + } + catch { + try await app.asyncShutdown() + throw error + } + try await app.asyncShutdown() + } + + @Test("Test Hello World Route") + func helloWorld() async throws { + try await withApp { app in + try await app.test(.GET, "hello", afterResponse: { res async in + #expect(res.status == .ok) + #expect(res.body.string == "Hello, world!") + }) + } + } +}