Skip to content

Examples: Add an example which uses a resource bundle #82

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 4 commits 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
13 changes: 12 additions & 1 deletion .github/workflows/endtoend_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
example:
- Examples/HelloWorldVapor
- Examples/HelloWorldHummingbird
- Examples/HelloWorldWithResources
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand Down Expand Up @@ -55,4 +56,14 @@ jobs:
- name: Check that the service is running
run: |
curl -v localhost:8080 | grep "Hello World"
# The curious combination of --verbose and --silent causes
# curl to print the request and response (--verbose) but not
# the transmission progress messages (--silent).
#
# --fail-with-body causes curl to exit with a nonzero exit code
# if the HTTP response code is >= 400. Without this flag, curl
# only returns a nonzero exit code if something went wrong while
# connecting to the server - a successful HTTP transaction which
# indicates a server error is still considered to be a successful
# transaction from the client's point of view.
curl --verbose --silent --output /dev/null --fail-with-body localhost:8080
1 change: 1 addition & 0 deletions .licenseignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/*.docc/*
**/*.jpg
**/*.md
**/.gitignore
**/Package.resolved
Expand Down
37 changes: 37 additions & 0 deletions Examples/HelloWorldWithResources/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// swift-tools-version: 6.0

//===----------------------------------------------------------------------===//
//
// 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 PackageDescription

let package = Package(
name: "hello-world",
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.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"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
],
resources: [.process("resources")]
)
]
)
34 changes: 34 additions & 0 deletions Examples/HelloWorldWithResources/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()
}
}
51 changes: 51 additions & 0 deletions Examples/HelloWorldWithResources/Sources/Application+build.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// 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 Foundation
import Hummingbird
import Logging

let myos = ProcessInfo.processInfo.operatingSystemVersionString

func buildApplication(configuration: ApplicationConfiguration) -> some ApplicationProtocol {
let router = Router()
router.addMiddleware { LogRequestsMiddleware(.info) }
router.get("/") { _, _ in
let faces = [
"happy-cat-face",
"slightly-smiling-face",
"smiling-face-with-sunglasses",
]

guard let resourceURL = Bundle.module.url(forResource: faces.randomElement(), withExtension: "jpg") else {
throw HTTPError(.internalServerError)
}

let image = try Data(contentsOf: resourceURL)

return Response(
status: .ok,
headers: [.contentType: "image/jpg"],
body: .init(byteBuffer: ByteBuffer(bytes: image))
)
}

let app = Application(
router: router,
configuration: configuration,
logger: Logger(label: "hello-with-resources")
)

return app
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.