Skip to content

Commit cea36f1

Browse files
committed
add simplified router
1 parent e93b1c0 commit cea36f1

File tree

5 files changed

+102
-29
lines changed

5 files changed

+102
-29
lines changed

Examples/quoteapi/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
1919
// .package(url: "https://github.com/swift-server/swift-openapi-lambda.git", from: "0.3.0"),
2020
.package(name: "swift-openapi-lambda", path: "../.."),
21-
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.8.0")
21+
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.8.0"),
2222
],
2323
targets: [
2424
.executableTarget(
@@ -28,7 +28,7 @@ let package = Package(
2828
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
2929
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
3030
.product(name: "OpenAPILambda", package: "swift-openapi-lambda"),
31-
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle")
31+
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
3232
],
3333
path: "Sources/QuoteAPI",
3434
resources: [

Examples/quoteapi/Sources/QuoteAPI/QuoteService.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
2727
try self.registerHandlers(on: transport)
2828

2929
// you have a chance here to customize the routes, for example
30-
try transport.router.add(
31-
method: .get,
32-
path: "/health"
33-
) { (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse,HTTPBody?) in
34-
return (.init(status: .ok), .init("OK"))
30+
try transport.router.get("/health") { _, _ in
31+
"OK"
3532
}
3633
}
3734

3835
static func main() async throws {
3936

4037
// when you just need to run the Lambda function, call Self.run()
41-
let openAPIService = QuoteServiceImpl(i: 42) // with dependency injection
38+
let openAPIService = QuoteServiceImpl(i: 42) // with dependency injection
4239
try await openAPIService.run()
4340

4441
// =============================
@@ -47,8 +44,8 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
4744
// (dependency injection, Service LifeCycle, etc)
4845
//
4946
// 1. Create the open API lambda service,
50-
// 2. Pass it to an OpenAPI Lambda Handler
51-
// 3. Create the Lambda Runtime service, passing the handler
47+
// 2. Pass it to an OpenAPI Lambda Handler
48+
// 3. Create the Lambda Runtime service, passing the handler
5249
// 4. Either start the runtime, or add it to a service lifecycle group.
5350

5451
// Here is an example where you start your own Lambda runtime
@@ -83,7 +80,7 @@ struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
8380

8481
// example of dependency injection
8582
let i: Int
86-
init(i: Int) { // 1.
83+
init(i: Int) {
8784
self.i = i
8885
}
8986

Sources/OpenAPILambdaHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct OpenAPILambdaHandler<OALS: OpenAPILambdaService>: LambdaHandler, S
3434
///
3535
/// This initializer decouples the OpenAPILambdaService creation from the registration of the transport
3636
/// this allows users to control the lifecycle of their service and to inject dependencies.
37-
///
37+
///
3838
/// - Parameters
3939
/// - withService: The OpenAPI Lambda service to bind to this Lambda handler function
4040
public init(withService openAPILambdaService: OALS) throws {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift OpenAPI Lambda open source project
4+
//
5+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates
6+
// and the Swift OpenAPI Lambda project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
import AWSLambdaEvents
16+
import Foundation
17+
import HTTPTypes
18+
import OpenAPIRuntime
19+
20+
21+
// TODO: add more method to simplify usage
22+
23+
// Current API :
24+
25+
// try router.add(
26+
// method: .get,
27+
// path: "/health"
28+
// ) { (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse,HTTPBody?) in
29+
// return (.init(status: .ok), .init("OK"))
30+
// }
31+
32+
// should be
33+
34+
// try router.get("/health") { _ in
35+
// "OK"
36+
// }
37+
38+
extension OpenAPILambdaRouter {
39+
40+
/// Adds a GET route to the router for the given path.
41+
///
42+
/// The given handler retruns a String that will be converted to the correct response type.
43+
/// It will return an HTTP 200 response or HTTP 500 if your handler throws and error
44+
/// - Parameters:
45+
/// - path: The path for the route.
46+
/// - handler: The handler to be executed for the route.
47+
/// - Throws: An error if the route cannot be added.
48+
public func get(
49+
_ path: String,
50+
handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String
51+
) throws {
52+
53+
let openAPIHandler: OpenAPIHandler = {
54+
(request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in
55+
56+
do {
57+
let response = try await handler(request, body)
58+
return (.init(status: .ok), .init(response))
59+
}
60+
catch {
61+
return (.init(status: .internalServerError), nil)
62+
}
63+
}
64+
try add(method: .get, path: path, handler: openAPIHandler)
65+
}
66+
67+
/// Adds a POST route to the router for the given path.
68+
///
69+
/// The given handler retruns a String that will be converted to the correct response type.
70+
/// It will return an HTTP 200 response or HTTP 500 if your handler throws and error
71+
/// - Parameters:
72+
/// - path: The path for the route.
73+
/// - handler: The handler to be executed for the route.
74+
/// - Throws: An error if the route cannot be added.
75+
public func post(
76+
_ path: String,
77+
handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String
78+
) throws {
79+
80+
let openAPIHandler: OpenAPIHandler = {
81+
(request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in
82+
do {
83+
let response = try await handler(request, body)
84+
return (.init(status: .ok), .init(response))
85+
}
86+
catch {
87+
return (.init(status: .internalServerError), nil)
88+
}
89+
}
90+
91+
try add(method: .post, path: path, handler: openAPIHandler)
92+
}
93+
}

Sources/Router/OpenAPILambdaRouter.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,3 @@ public protocol OpenAPILambdaRouter: Sendable {
3333
OpenAPIHandler, OpenAPILambdaRequestParameters
3434
)
3535
}
36-
37-
// TODO: add more method to simplify usage
38-
39-
// Current API :
40-
41-
// try router.add(
42-
// method: .get,
43-
// path: "/health"
44-
// ) { (request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse,HTTPBody?) in
45-
// return (.init(status: .ok), .init("OK"))
46-
// }
47-
48-
// should be
49-
50-
// try router.get("/health") { _ in
51-
// "OK"
52-
// }

0 commit comments

Comments
 (0)