Skip to content

Commit

Permalink
cors and optional hanlding
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Nov 29, 2024
1 parent 87a4e24 commit 6ab1630
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions RPC/Sources/RPC/JSONRPC/RequestParameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public struct Request1<T: FromJSON>: RequestParameter {
guard case let .array(arr) = json else {
throw RequestError.notArray
}
guard arr.count == 1 else {
guard arr.count <= 1 else {
throw RequestError.unexpectedLength
}
value = try T(from: arr[0])
value = try T(from: arr[safe: 0])
}

Check warning on line 36 in RPC/Sources/RPC/JSONRPC/RequestParameter.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Sources/RPC/JSONRPC/RequestParameter.swift#L25-L36

Added lines #L25 - L36 were not covered by tests
}

Expand All @@ -48,10 +48,10 @@ public struct Request2<T1: FromJSON, T2: FromJSON>: RequestParameter {
guard case let .array(arr) = json else {
throw RequestError.notArray
}
guard arr.count == 2 else {
guard arr.count <= 2 else {
throw RequestError.unexpectedLength
}
valuu = try (T1(from: arr[0]), T2(from: arr[1]))
valuu = try (T1(from: arr[safe: 0]), T2(from: arr[safe: 1]))
}

Check warning on line 55 in RPC/Sources/RPC/JSONRPC/RequestParameter.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Sources/RPC/JSONRPC/RequestParameter.swift#L44-L55

Added lines #L44 - L55 were not covered by tests
}

Expand All @@ -67,10 +67,10 @@ public struct Request3<T1: FromJSON, T2: FromJSON, T3: FromJSON>: RequestParamet
guard case let .array(arr) = json else {
throw RequestError.notArray
}
guard arr.count == 3 else {
guard arr.count <= 3 else {
throw RequestError.unexpectedLength
}
value = try (T1(from: arr[0]), T2(from: arr[1]), T3(from: arr[2]))
value = try (T1(from: arr[safe: 0]), T2(from: arr[safe: 1]), T3(from: arr[safe: 2]))
}

Check warning on line 74 in RPC/Sources/RPC/JSONRPC/RequestParameter.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Sources/RPC/JSONRPC/RequestParameter.swift#L63-L74

Added lines #L63 - L74 were not covered by tests
}

Expand All @@ -86,9 +86,9 @@ public struct Request4<T1: FromJSON, T2: FromJSON, T3: FromJSON, T4: FromJSON>:
guard case let .array(arr) = json else {
throw RequestError.notArray
}
guard arr.count == 4 else {
guard arr.count <= 4 else {
throw RequestError.unexpectedLength
}
value = try (T1(from: arr[0]), T2(from: arr[1]), T3(from: arr[2]), T4(from: arr[3]))
value = try (T1(from: arr[safe: 0]), T2(from: arr[safe: 1]), T3(from: arr[safe: 2]), T4(from: arr[safe: 3]))
}

Check warning on line 93 in RPC/Sources/RPC/JSONRPC/RequestParameter.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Sources/RPC/JSONRPC/RequestParameter.swift#L82-L93

Added lines #L82 - L93 were not covered by tests
}
10 changes: 10 additions & 0 deletions RPC/Sources/RPC/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public class Server {
let env = try Environment.detect(arguments: ["--env"])
app = Application(env)

// TODO: configure cors origins
let corsConfiguration = CORSMiddleware.Configuration(
allowedOrigin: .all,
allowedMethods: [.GET, .POST, .PUT, .OPTIONS],
allowedHeaders: [.accept, .authorization, .contentType, .origin, .xRequestedWith, .userAgent, .accessControlAllowOrigin]
)
let cors = CORSMiddleware(configuration: corsConfiguration)
// cors middleware should come before default error middleware using `at: .beginning`
app.middleware.use(cors, at: .beginning)

let handlers = AllHandlers.getHandlers(source: source)

Check warning on line 40 in RPC/Sources/RPC/Server.swift

View check run for this annotation

Codecov / codecov/patch

RPC/Sources/RPC/Server.swift#L30-L40

Added lines #L30 - L40 were not covered by tests

// Register routes
Expand Down

0 comments on commit 6ab1630

Please sign in to comment.