From ef2bc828e4ab85cf797cb04487a8d9fe813a51ae Mon Sep 17 00:00:00 2001 From: Euan Harris Date: Mon, 14 Apr 2025 09:27:52 +0100 Subject: [PATCH] Examples: Adopt the structure used in hummingbird-examples --- Examples/HelloWorldHummingbird/Package.swift | 11 ++++-- .../HelloWorldHummingbird/Sources/App.swift | 34 +++++++++++++++++++ .../{main.swift => Application+build.swift} | 20 ++++++++--- 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 Examples/HelloWorldHummingbird/Sources/App.swift rename Examples/HelloWorldHummingbird/Sources/{main.swift => Application+build.swift} (52%) diff --git a/Examples/HelloWorldHummingbird/Package.swift b/Examples/HelloWorldHummingbird/Package.swift index 4e74beb..8361bc4 100644 --- a/Examples/HelloWorldHummingbird/Package.swift +++ b/Examples/HelloWorldHummingbird/Package.swift @@ -21,9 +21,16 @@ let package = Package( platforms: [.macOS(.v14)], dependencies: [ .package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.1.0"), - .package(url: "https://github.com/apple/swift-container-plugin", from: "0.4.0"), + .package(url: "https://github.com/apple/swift-container-plugin", from: "0.5.0"), + .package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"), ], targets: [ - .executableTarget(name: "hello-world", dependencies: [.product(name: "Hummingbird", package: "hummingbird")]) + .executableTarget( + name: "hello-world", + dependencies: [ + .product(name: "Hummingbird", package: "hummingbird"), + .product(name: "ArgumentParser", package: "swift-argument-parser"), + ] + ) ] ) diff --git a/Examples/HelloWorldHummingbird/Sources/App.swift b/Examples/HelloWorldHummingbird/Sources/App.swift new file mode 100644 index 0000000..b61a21a --- /dev/null +++ b/Examples/HelloWorldHummingbird/Sources/App.swift @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftContainerPlugin open source project +// +// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import ArgumentParser + +@main +struct Hello: AsyncParsableCommand { + @Option(name: .shortAndLong) + var hostname: String = "0.0.0.0" + + @Option(name: .shortAndLong) + var port: Int = 8080 + + func run() async throws { + let app = buildApplication( + configuration: .init( + address: .hostname(hostname, port: port), + serverName: "Hummingbird" + ) + ) + try await app.runService() + } +} diff --git a/Examples/HelloWorldHummingbird/Sources/main.swift b/Examples/HelloWorldHummingbird/Sources/Application+build.swift similarity index 52% rename from Examples/HelloWorldHummingbird/Sources/main.swift rename to Examples/HelloWorldHummingbird/Sources/Application+build.swift index ae980aa..3bb1b33 100644 --- a/Examples/HelloWorldHummingbird/Sources/main.swift +++ b/Examples/HelloWorldHummingbird/Sources/Application+build.swift @@ -2,7 +2,7 @@ // // This source file is part of the SwiftContainerPlugin open source project // -// Copyright (c) 2024 Apple Inc. and the SwiftContainerPlugin project authors +// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -14,12 +14,22 @@ import Foundation import Hummingbird +import Logging let myos = ProcessInfo.processInfo.operatingSystemVersionString -let router = Router() -router.get { request, _ -> String in "Hello World, from Hummingbird on \(myos)\n" } +func buildApplication(configuration: ApplicationConfiguration) -> some ApplicationProtocol { + let router = Router() + router.addMiddleware { LogRequestsMiddleware(.info) } + router.get("/") { _, _ in + "Hello World, from Hummingbird on \(myos)\n" + } -let app = Application(router: router, configuration: .init(address: .hostname("0.0.0.0", port: 8080))) + let app = Application( + router: router, + configuration: configuration, + logger: Logger(label: "HelloWorldHummingbird") + ) -try await app.runService() + return app +}