Skip to content
Draft
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let package = Package(
],
traits: [
.trait(name: "ConfigurationSupport", description: "Enable support for swift-configuration package."),
.trait(name: "NonThrowingRouteHandlers", description: "Don't allow throwing inside Router's get/post/etc. callback handlers."),
.default(enabledTraits: ["ConfigurationSupport"]),
],
dependencies: [
Expand Down
20 changes: 13 additions & 7 deletions Sources/Hummingbird/Router/RouterMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ public protocol RouterMethods<Context>: _HB_SendableMetatype {
}

extension RouterMethods {
#if NonThrowingRouteHandlers
public typealias RouteHandlerError = Never
#else
public typealias RouteHandlerError = any Error
#endif

/// Add path for async closure
@discardableResult public func on(
_ path: RouterPath,
method: HTTPRequest.Method,
use closure: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use closure: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
let responder = self.constructResponder(use: closure)
self.on(path, method: method, responder: responder)
Expand Down Expand Up @@ -133,47 +139,47 @@ extension RouterMethods {
/// GET path for async closure returning type conforming to ResponseGenerator
@discardableResult public func get(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .get, use: handler)
}

/// PUT path for async closure returning type conforming to ResponseGenerator
@discardableResult public func put(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .put, use: handler)
}

/// DELETE path for async closure returning type conforming to ResponseGenerator
@discardableResult public func delete(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .delete, use: handler)
}

/// HEAD path for async closure returning type conforming to ResponseGenerator
@discardableResult public func head(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .head, use: handler)
}

/// POST path for async closure returning type conforming to ResponseGenerator
@discardableResult public func post(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .post, use: handler)
}

/// PATCH path for async closure returning type conforming to ResponseGenerator
@discardableResult public func patch(
_ path: RouterPath = "",
use handler: @Sendable @escaping (Request, Context) async throws -> some ResponseGenerator
use handler: @Sendable @escaping (Request, Context) async throws(RouteHandlerError) -> some ResponseGenerator
) -> Self {
self.on(path, method: .patch, use: handler)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/PerformanceTest/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ router.get("json") { _, _ in
Object(message: "Hello, world")
}

#if !NonThrowingRouteHandlers
// return JSON
// ./wrk -c 128 -d 15s -t 8 http://localhost:8080/json
router.get("wait") { _, _ in
try await Task.sleep(for: .seconds(8))
return "I waited"
}
#endif

var app = Application(
responder: router.buildResponder(),
Expand Down
Loading