Skip to content
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

Use add(middleware:) #82

Merged
merged 4 commits into from
Jun 18, 2024
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
2 changes: 1 addition & 1 deletion auth-jwt/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-beta.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"),
Expand Down
4 changes: 2 additions & 2 deletions auth-jwt/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func buildApplication(_ args: AppArguments) async throws -> some ApplicationProt
jwtAuthenticator.useSigner(.hs256(key: "my-secret-key"), kid: jwtLocalSignerKid)

let router = Router(context: BasicAuthRequestContext.self)
router.middlewares.add(LogRequestsMiddleware(.debug))
router.middlewares.add(
router.add(middleware: LogRequestsMiddleware(.debug))
router.add(middleware:
CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.accept, .authorization, .contentType, .origin],
Expand Down
2 changes: 1 addition & 1 deletion auth-srp/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-beta.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0"),
Expand Down
6 changes: 3 additions & 3 deletions auth-srp/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func buildApplication(_ args: some AppArguments) async throws -> some Applicatio
}

let router = Router(context: BasicAuthRequestContext.self)
router.middlewares.add(RedirectMiddleware())
router.middlewares.add(FileMiddleware(logger: logger))
router.middlewares.add(
router.add(middleware: RedirectMiddleware())
router.add(middleware: FileMiddleware(logger: logger))
router.add(middleware:
LogRequestsMiddleware(
.info,
includeHeaders: .all(),
Expand Down
2 changes: 1 addition & 1 deletion html-form/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
Expand Down
2 changes: 1 addition & 1 deletion html-form/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public func buildApplication(args: AppArguments) async throws -> some Applicatio
assert(library.getTemplate(named: "page") != nil, "Set your working directory to the root folder of this example to get it to work")

let router = Router(context: HTMLFormRequestContext.self)
router.middlewares.add(FileMiddleware())
router.add(middleware: FileMiddleware())
WebController(mustacheLibrary: library).addRoutes(to: router)
let app = Application(
router: router,
Expand Down
2 changes: 1 addition & 1 deletion multipart-form/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
.package(url: "https://github.com/swift-extras/swift-extras-base64.git", .upToNextMinor(from: "0.7.0")),
Expand Down
2 changes: 1 addition & 1 deletion multipart-form/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func buildApplication(_ args: AppArguments) async throws -> some ApplicationProt
assert(library.getTemplate(named: "page") != nil, "Set your working directory to the root folder of this example to get it to work")

let router = Router(context: MultipartRequestContext.self)
router.middlewares.add(FileMiddleware())
router.add(middleware: FileMiddleware())
WebController(mustacheLibrary: library).addRoutes(to: router)
return Application(router: router, configuration: .init(address: .hostname(args.hostname, port: args.port)))
}
2 changes: 1 addition & 1 deletion proxy-server/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.22.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.6.0"),
Expand Down
2 changes: 1 addition & 1 deletion proxy-server/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func buildApplication(_ args: some AppArguments) -> some ApplicationProtocol {
let eventLoopGroup = MultiThreadedEventLoopGroup.singleton
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
let router = Router(context: ProxyRequestContext.self)
router.middlewares.add(
router.add(middleware:
ProxyServerMiddleware(
httpClient: httpClient,
proxy: .init(location: args.location, target: args.target)
Expand Down
2 changes: 1 addition & 1 deletion sessions/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-beta.2"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.16.0"),
Expand Down
2 changes: 1 addition & 1 deletion sessions/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func buildApplication(_ arguments: AppArguments, configuration: ApplicationConfi
let router = Router(context: BasicAuthRequestContext.self)

// add logging middleware
router.middlewares.add(LogRequestsMiddleware(.debug))
router.add(middleware: LogRequestsMiddleware(.debug))

// routes
router.get("/") { _, _ in
Expand Down
2 changes: 1 addition & 1 deletion todos-auth-fluent/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-auth.git", from: "2.0.0-beta.3"),
.package(url: "https://github.com/hummingbird-project/hummingbird-fluent.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0-beta.1"),
Expand Down
6 changes: 3 additions & 3 deletions todos-auth-fluent/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func buildApplication(_ arguments: some AppArguments) async throws -> some Appli
let router = Router(context: TodosAuthRequestContext.self)

// add logging middleware
router.middlewares.add(LogRequestsMiddleware(.info))
router.add(middleware: LogRequestsMiddleware(.info))
// add file middleware to server css and js files
router.middlewares.add(FileMiddleware(logger: logger))
router.middlewares.add(CORSMiddleware(
router.add(middleware: FileMiddleware(logger: logger))
router.add(middleware: CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.contentType],
allowMethods: [.get, .options, .post, .delete, .patch]
Expand Down
4 changes: 2 additions & 2 deletions todos-dynamodb/Sources/App/TodosApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct TodosApp: ApplicationProtocol {
var responder: some HTTPResponder<Context> {
let router = Router(context: Context.self)
// middleware
router.middlewares.add(LogRequestsMiddleware(.debug))
router.middlewares.add(CORSMiddleware(
router.add(middleware: LogRequestsMiddleware(.debug))
router.add(middleware: CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.contentType],
allowMethods: [.get, .options, .post, .delete, .patch]
Expand Down
6 changes: 3 additions & 3 deletions todos-fluent/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ func buildApplication(_ arguments: some AppArguments) async throws -> some Appli
let router = Router()

// add logging middleware
router.middlewares.add(LogRequestsMiddleware(.info))
router.add(middleware: LogRequestsMiddleware(.info))
// add file middleware to server css and js files
router.middlewares.add(FileMiddleware(logger: logger))
router.middlewares.add(CORSMiddleware(
router.add(middleware: FileMiddleware(logger: logger))
router.add(middleware: CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.contentType],
allowMethods: [.get, .options, .post, .delete, .patch]
Expand Down
2 changes: 1 addition & 1 deletion todos-lambda/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-lambda.git", from: "2.0.0-beta.3"),
.package(url: "https://github.com/soto-project/soto.git", from: "7.0.0-rc.1"),
],
Expand Down
6 changes: 3 additions & 3 deletions todos-lambda/Sources/App/lambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ struct AppLambda: APIGatewayLambdaFunction {

let router = Router(context: Context.self)
// middleware
router.middlewares.add(ErrorMiddleware())
router.middlewares.add(LogRequestsMiddleware(.debug))
router.add(middleware: ErrorMiddleware())
router.add(middleware: LogRequestsMiddleware(.debug))
router.get("/") { _, _ in
return "Hello"
}
router.middlewares.add(CORSMiddleware(
router.add(middleware: CORSMiddleware(
allowOrigin: .originBased,
allowHeaders: [.contentType],
allowMethods: [.get, .options, .post, .delete, .patch]
Expand Down
4 changes: 2 additions & 2 deletions todos-mongokitten-openapi/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird.git", from: "2.0.0-beta.1"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/swift-server/swift-openapi-hummingbird.git", from: "2.0.0-beta.2"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.2.0"),
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.3.0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct HummingbirdArguments: AsyncParsableCommand {
let mongo = try await MongoDatabase.connect(to: self.connectionString)

let router = Router()
router.middlewares.add(LogRequestsMiddleware(.info))
router.add(middleware: LogRequestsMiddleware(.info))
let api = API(mongo: mongo)
try api.registerHandlers(on: router)

Expand Down
2 changes: 1 addition & 1 deletion todos-postgres-tutorial/Sources/Todos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func buildApplication(_ args: some AppArguments) async throws -> some Applicatio
// create router
let router = Router()
// add logging middleware
router.middlewares.add(LogRequestsMiddleware(.info))
router.add(middleware: LogRequestsMiddleware(.info))
// add hello route
router.get("/") { request, context in
"Hello\n"
Expand Down
2 changes: 1 addition & 1 deletion upload-s3/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/soto-project/soto.git", from: "7.0.0-rc.1"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
Expand Down
2 changes: 1 addition & 1 deletion upload-s3/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func buildApplication(_ args: some AppArguments) -> some ApplicationProtocol {
let s3 = S3(client: awsClient, region: .euwest1)

let router = Router()
router.middlewares.add(LogRequestsMiddleware(.info))
router.add(middleware: LogRequestsMiddleware(.info))

router.addRoutes(
S3FileController(
Expand Down
2 changes: 1 addition & 1 deletion upload/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(
.macOS(.v14),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.5"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
targets: [
Expand Down
16 changes: 8 additions & 8 deletions upload/Tests/AppTests/AppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ final class AppTests: XCTestCase {
var hostname: String { "127.0.0.1" }
var port: Int { 0 }
}

func testUploadDownload() async throws {
let app = buildApplication(args: TestArguments())

try await app.test(.live) { client in
let textString = "Hello, World!"
let testFileName = "Hello.txt"
Expand All @@ -21,7 +21,7 @@ final class AppTests: XCTestCase {
try? FileManager.default.removeItem(at: uploadURL)
}
let buffer = ByteBuffer(string: textString)

try await client.execute(
uri: "/files",
method: .post,
Expand All @@ -32,14 +32,14 @@ final class AppTests: XCTestCase {
let bodyString = String(buffer: response.body)
XCTAssertTrue(bodyString.contains(testFileName))
}

try await client.execute(uri: "/files/\(testFileName)", method: .get) { response in
let downloadString = String(buffer: response.body)
XCTAssertEqual(downloadString, textString, "Downloaded bytes should match uploaded bytes")
}
}
}

func testUploadDownload2() async throws {
let app = buildApplication(args: TestArguments())

Expand All @@ -55,7 +55,7 @@ final class AppTests: XCTestCase {
}
}
}

func runUploadTest(
_ client: any TestClientProtocol,
_ i: Int,
Expand All @@ -82,15 +82,15 @@ final class AppTests: XCTestCase {
) { response in
XCTAssertEqual(response.status, .ok)
}

print("--download-- \(testFileName)")
try await client.execute(uri: "/files/\(testFileName)", method: .get) { response in
let dlBytes = response.body.getBytes(at: 0, length: response.body.readableBytes)

let isOk = dlBytes == bytes
XCTAssertTrue(isOk, "Downloaded bytes should match uploaded bytes")
}

print("--downloaded-- \(testFileName)")
}
}
2 changes: 1 addition & 1 deletion websocket-chat/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0-beta.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
Expand Down
6 changes: 3 additions & 3 deletions websocket-chat/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func buildApplication(_ arguments: some AppArguments) async throws -> some Appli

// Router
let router = Router()
router.middlewares.add(LogRequestsMiddleware(.debug))
router.middlewares.add(FileMiddleware(logger: logger))
router.add(middleware: LogRequestsMiddleware(.debug))
router.add(middleware: FileMiddleware(logger: logger))

// Separate router for websocket upgrade
let wsRouter = Router(context: BasicWebSocketRequestContext.self)
wsRouter.middlewares.add(LogRequestsMiddleware(.debug))
wsRouter.add(middleware: LogRequestsMiddleware(.debug))
wsRouter.ws("chat") { request, _ in
// only allow upgrade if username query parameter exists
guard request.uri.queryParameters["username"] != nil else {
Expand Down
2 changes: 1 addition & 1 deletion websocket-echo/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "App", targets: ["App"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.8"),
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0-beta.4"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.4.0"),
],
Expand Down
6 changes: 3 additions & 3 deletions websocket-echo/Sources/App/Application+build.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func buildApplication(_ arguments: some AppArguments) async throws -> some Appli

// Router
let router = Router()
router.middlewares.add(LogRequestsMiddleware(.debug))
router.middlewares.add(FileMiddleware(logger: logger))
router.add(middleware: LogRequestsMiddleware(.debug))
router.add(middleware: FileMiddleware(logger: logger))

// Separate router for websocket upgrade
let wsRouter = Router(context: BasicWebSocketRequestContext.self)
wsRouter.middlewares.add(LogRequestsMiddleware(.debug))
wsRouter.add(middleware: LogRequestsMiddleware(.debug))
wsRouter.ws("echo") { _, _ in
.upgrade([:])
} onUpgrade: { inbound, outbound, _ in
Expand Down