Skip to content

Commit 339b3b8

Browse files
committed
Examples: Add example using bundled resources
1 parent 1d1fac5 commit 339b3b8

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// swift-tools-version: 6.0
2+
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// This source file is part of the SwiftContainerPlugin open source project
6+
//
7+
// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors
8+
// Licensed under Apache License v2.0
9+
//
10+
// See LICENSE.txt for license information
11+
// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
import PackageDescription
18+
19+
let package = Package(
20+
name: "hello-world",
21+
platforms: [.macOS(.v14)],
22+
dependencies: [
23+
.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.5.0"),
25+
],
26+
targets: [
27+
.executableTarget(
28+
name: "hello-world",
29+
dependencies: [.product(name: "Hummingbird", package: "hummingbird")],
30+
resources: [.process("resources")]
31+
)
32+
]
33+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
let router = Router()
22+
router.addMiddleware { LogRequestsMiddleware(.info) }
23+
router.get { _, _ in
24+
let faces = [
25+
"happy-cat-face",
26+
"slightly-smiling-face",
27+
"smiling-face-with-sunglasses",
28+
]
29+
30+
guard let resourceURL = Bundle.module.url(forResource: faces.randomElement(), withExtension: "jpg") else {
31+
throw HTTPError(.internalServerError)
32+
}
33+
34+
let image = try Data(contentsOf: resourceURL)
35+
36+
return Response(
37+
status: .ok,
38+
headers: [.contentType: "image/jpg"],
39+
body: .init(byteBuffer: ByteBuffer(bytes: image))
40+
)
41+
}
42+
43+
let app = Application(
44+
router: router,
45+
configuration: .init(address: .hostname("0.0.0.0", port: 8080)),
46+
logger: Logger(label: "hello-with-resources")
47+
)
48+
49+
try await app.runService()
Loading
Loading
Loading

0 commit comments

Comments
 (0)