Skip to content

Examples: Adopt the structure used in hummingbird-examples #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Examples/HelloWorldHummingbird/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
)
]
)
34 changes: 34 additions & 0 deletions Examples/HelloWorldHummingbird/Sources/App.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}