Skip to content

Commit 0c9ea97

Browse files
authored
Examples: Adopt structure used in the Vapor template repository (#85)
Motivation ---------- The [Vapor template repository](https://github.com/vapor/template-bare) splits the server into a main entry point file and a separate route installation function. Adopting the same structure makes this example more familiar to users who are used to the official template. Fixes #61 Modifications ------------- * Adopt the Vapor template structure for `HelloWorldVapor` * Use the `-warnings-as-errors` flag when building end to end examples. Result ------ The example works in the same way as before but has a more familiar structure for Vapor users. Test Plan --------- All tests continue to pass.
1 parent bb69a22 commit 0c9ea97

File tree

5 files changed

+62
-10
lines changed

5 files changed

+62
-10
lines changed

.github/workflows/endtoend_tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
cat ${{ matrix.example }}/Package.swift
4545
swift package \
4646
--package-path ${{ matrix.example }} \
47+
-Xswiftc -warnings-as-errors \
4748
--swift-sdk x86_64-swift-linux-musl \
4849
--allow-network-connections all \
4950
build-container-image \

Examples/HelloWorldVapor/Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ let package = Package(
2020
name: "hello-world",
2121
platforms: [.macOS(.v13)],
2222
dependencies: [
23-
.package(url: "https://github.com/vapor/vapor", .upToNextMajor(from: "4.102.0")),
24-
.package(url: "https://github.com/apple/swift-container-plugin", from: "0.4.0"),
23+
.package(url: "https://github.com/vapor/vapor", from: "4.102.0"),
24+
.package(url: "https://github.com/apple/swift-container-plugin", from: "0.5.0"),
2525
],
2626
targets: [.executableTarget(name: "hello-world", dependencies: [.product(name: "Vapor", package: "vapor")])]
2727
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 Vapor
16+
17+
func configure(_ app: Application) async throws {
18+
try routes(app)
19+
}
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 Vapor
16+
17+
@main
18+
enum Entrypoint {
19+
static func main() async throws {
20+
let env = try Environment.detect()
21+
let app = try await Application.make(env)
22+
app.http.server.configuration.hostname = "0.0.0.0"
23+
24+
do {
25+
try await configure(app)
26+
try await app.execute()
27+
} catch {
28+
app.logger.report(error: error)
29+
try? await app.asyncShutdown()
30+
throw error
31+
}
32+
try await app.asyncShutdown()
33+
}
34+
}

Examples/HelloWorldVapor/Sources/main.swift renamed to Examples/HelloWorldVapor/Sources/routes.swift

+6-8
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
@@ -17,10 +17,8 @@ import Vapor
1717

1818
let myos = ProcessInfo.processInfo.operatingSystemVersionString
1919

20-
let app = try Application(.detect())
21-
app.http.server.configuration.hostname = "0.0.0.0"
22-
defer { app.shutdown() }
23-
24-
app.get { _ in "Hello World, from Vapor on \(myos)\n" }
25-
26-
try app.run()
20+
func routes(_ app: Application) throws {
21+
app.get { req async in
22+
"Hello World, from Vapor on \(myos)\n"
23+
}
24+
}

0 commit comments

Comments
 (0)