Skip to content

Commit f2b2fb8

Browse files
committed
Examples: Adopt the structure used in hummingbird-examples
1 parent 339b3b8 commit f2b2fb8

File tree

3 files changed

+80
-49
lines changed

3 files changed

+80
-49
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
@main
16+
struct Hello {
17+
static let hostname = "0.0.0.0"
18+
static let port = 8080
19+
20+
static func main() async throws {
21+
let app = buildApplication(
22+
configuration: .init(
23+
address: .hostname(hostname, port: port),
24+
serverName: "Hummingbird"
25+
)
26+
)
27+
try await app.runService()
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Foundation
16+
import Hummingbird
17+
import Logging
18+
19+
let myos = ProcessInfo.processInfo.operatingSystemVersionString
20+
21+
func buildApplication(configuration: ApplicationConfiguration) -> some ApplicationProtocol {
22+
let router = Router()
23+
router.addMiddleware { LogRequestsMiddleware(.info) }
24+
router.get("/") { _, _ in
25+
let faces = [
26+
"happy-cat-face",
27+
"slightly-smiling-face",
28+
"smiling-face-with-sunglasses",
29+
]
30+
31+
guard let resourceURL = Bundle.module.url(forResource: faces.randomElement(), withExtension: "jpg") else {
32+
throw HTTPError(.internalServerError)
33+
}
34+
35+
let image = try Data(contentsOf: resourceURL)
36+
37+
return Response(
38+
status: .ok,
39+
headers: [.contentType: "image/jpg"],
40+
body: .init(byteBuffer: ByteBuffer(bytes: image))
41+
)
42+
}
43+
44+
let app = Application(
45+
router: router,
46+
configuration: configuration,
47+
logger: Logger(label: "hello-with-resources")
48+
)
49+
50+
return app
51+
}

Examples/HelloWorldWithResources/Sources/main.swift

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)