Skip to content

Commit bb69a22

Browse files
authored
Examples: Adopt the structure used in hummingbird-examples (#84)
Motivation ---------- The [official Hummingbird examples](https://github.com/hummingbird-project/hummingbird-examples) split the server into a main entry point file and a separate build function. Adopting the same structure makes this example more more familiar to users who are used to the official examples. Modifications ------------- Adopt the Hummingbird example structure for `HelloWorldHummingbird` Result ------ The example works in the same way as before but has a more familiar structure for Hummingbird users. Test Plan --------- All tests continue to pass.
1 parent 2533fd5 commit bb69a22

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

Examples/HelloWorldHummingbird/Package.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ let package = Package(
2121
platforms: [.macOS(.v14)],
2222
dependencies: [
2323
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.1.0"),
24-
.package(url: "https://github.com/apple/swift-container-plugin", from: "0.4.0"),
24+
.package(url: "https://github.com/apple/swift-container-plugin", from: "0.5.0"),
25+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"),
2526
],
2627
targets: [
27-
.executableTarget(name: "hello-world", dependencies: [.product(name: "Hummingbird", package: "hummingbird")])
28+
.executableTarget(
29+
name: "hello-world",
30+
dependencies: [
31+
.product(name: "Hummingbird", package: "hummingbird"),
32+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
33+
]
34+
)
2835
]
2936
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftContainerPlugin open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import ArgumentParser
16+
17+
@main
18+
struct Hello: AsyncParsableCommand {
19+
@Option(name: .shortAndLong)
20+
var hostname: String = "0.0.0.0"
21+
22+
@Option(name: .shortAndLong)
23+
var port: Int = 8080
24+
25+
func run() async throws {
26+
let app = buildApplication(
27+
configuration: .init(
28+
address: .hostname(hostname, port: port),
29+
serverName: "Hummingbird"
30+
)
31+
)
32+
try await app.runService()
33+
}
34+
}

Examples/HelloWorldHummingbird/Sources/main.swift renamed to Examples/HelloWorldHummingbird/Sources/Application+build.swift

+15-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftContainerPlugin open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the SwiftContainerPlugin project authors
5+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -14,12 +14,22 @@
1414

1515
import Foundation
1616
import Hummingbird
17+
import Logging
1718

1819
let myos = ProcessInfo.processInfo.operatingSystemVersionString
1920

20-
let router = Router()
21-
router.get { request, _ -> String in "Hello World, from Hummingbird on \(myos)\n" }
21+
func buildApplication(configuration: ApplicationConfiguration) -> some ApplicationProtocol {
22+
let router = Router()
23+
router.addMiddleware { LogRequestsMiddleware(.info) }
24+
router.get("/") { _, _ in
25+
"Hello World, from Hummingbird on \(myos)\n"
26+
}
2227

23-
let app = Application(router: router, configuration: .init(address: .hostname("0.0.0.0", port: 8080)))
28+
let app = Application(
29+
router: router,
30+
configuration: configuration,
31+
logger: Logger(label: "HelloWorldHummingbird")
32+
)
2433

25-
try await app.runService()
34+
return app
35+
}

0 commit comments

Comments
 (0)