|
1 | 1 | import CatbirdAPI
|
2 | 2 | import Vapor
|
| 3 | +import NIOSSL |
3 | 4 |
|
4 | 5 | public struct CatbirdInfo: Content {
|
5 | 6 | public static let current = CatbirdInfo(
|
@@ -28,26 +29,38 @@ public func configure(_ app: Application, _ configuration: AppConfiguration) thr
|
28 | 29 | store: InMemoryResponseStore(),
|
29 | 30 | logger: Loggers.inMemoryStore)
|
30 | 31 |
|
31 |
| - // MARK: - Register Middlewares |
| 32 | + // MARK: - Register Middleware |
32 | 33 |
|
33 | 34 | // Pubic resource for web page
|
34 | 35 | app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
|
35 |
| - switch configuration.mode { |
36 |
| - case .read: |
37 |
| - app.logger.info("Read mode") |
38 |
| - // try read from static mocks if route not found |
39 |
| - app.middleware.use(AnyMiddleware.notFound(fileStore.response)) |
40 |
| - // try read from dynamic mocks |
41 |
| - app.middleware.use(AnyMiddleware.notFound(inMemoryStore.response)) |
42 |
| - case .write(let url): |
43 |
| - app.logger.info("Write mode") |
| 36 | + if configuration.isRecordMode { |
| 37 | + app.logger.info("Record mode") |
| 38 | + app.http.client.configuration.decompression = .enabled(limit: .none) |
44 | 39 | // capture response and write to file
|
45 | 40 | app.middleware.use(AnyMiddleware.capture { request, response in
|
| 41 | + if response.headers.contains(name: "Content-encoding") { |
| 42 | + response.headers.remove(name: "Content-encoding") |
| 43 | + } |
46 | 44 | let pattern = RequestPattern(method: .init(request.method.rawValue), url: request.url.string)
|
47 | 45 | let mock = ResponseMock(status: Int(response.status.code), body: response.body.data)
|
48 | 46 | return fileStore.perform(.update(pattern, mock), for: request).map { _ in response }
|
49 | 47 | })
|
50 |
| - // redirect request to another server |
| 48 | + // catch 404 and try read from real server |
| 49 | + if configuration.proxyEnabled { |
| 50 | + app.middleware.use(ProxyMiddleware()) |
| 51 | + } |
| 52 | + } else { |
| 53 | + app.logger.info("Read mode") |
| 54 | + // catch 404 and try read from real server |
| 55 | + if configuration.proxyEnabled { |
| 56 | + app.middleware.use(ProxyMiddleware()) |
| 57 | + } |
| 58 | + // try read from static mocks if route not found |
| 59 | + app.middleware.use(AnyMiddleware.notFound(fileStore.response)) |
| 60 | + // try read from dynamic mocks |
| 61 | + app.middleware.use(AnyMiddleware.notFound(inMemoryStore.response)) |
| 62 | + } |
| 63 | + if let url = configuration.redirectUrl { |
51 | 64 | app.middleware.use(RedirectMiddleware(serverURL: url))
|
52 | 65 | }
|
53 | 66 |
|
|
0 commit comments