-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestServer.swift
More file actions
107 lines (90 loc) · 3.74 KB
/
TestServer.swift
File metadata and controls
107 lines (90 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// TestServer.swift
// CBL-Tests-iOS
//
// Created by Callum Birks on 01/08/2023.
//
import os
import Vapor
class TestServer : ObservableObject {
var app : Vapor.Application
public static let maxAPIVersion = 1
public static let serverID = UUID()
init(port: Int) {
do {
var env : Environment = .development
try LoggingSystem.bootstrap(from: &env)
Log.initialize()
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let sessionManager = try SessionManager(filesDirectory: urls[0])
app = Application(env)
app.storage[SessionManagerKey.self] = sessionManager
configure(port: port)
} catch {
fatalError(error.localizedDescription)
}
}
public func run() async {
defer { app.shutdown() }
do {
try await app.runFromAsyncMainEntrypoint()
} catch {
fatalError(error.localizedDescription)
}
}
private func configure(port: Int) {
app.http.server.configuration.hostname = "0.0.0.0"
app.http.server.configuration.port = port
Log.log(level: .info, message: "Configuring HTTP server with hostname: 0.0.0.0 and port: \(port)")
// Use custom error middleware
app.middleware = .init()
app.middleware.use(TestServerMiddleware(app: app))
app.middleware.use(TestServerErrorMiddleware())
setupRoutes()
}
/// Implement API v1.2.1 + Merge-Dict Conflict Resolver defined in 2.0.1.
private func setupRoutes() {
app.get("", use: Handlers.getRoot)
app.post("newSession", use: Handlers.newSession)
app.post("reset", use: Handlers.resetHandler)
app.post("getAllDocuments", use: Handlers.getAllDocuments)
app.post("getDocument", use: Handlers.getDocument)
app.post("updateDatabase", use: Handlers.updateDatabase)
app.post("snapshotDocuments", use: Handlers.snapshotDocuments)
app.post("verifyDocuments", use: Handlers.verifyDocuments)
app.post("startReplicator", use: Handlers.startReplicator)
app.post("getReplicatorStatus", use: Handlers.getReplicatorStatus)
app.post("startMultipeerReplicator", use: Handlers.startMultipeerReplicator)
app.post("stopMultipeerReplicator", use: Handlers.stopMultipeerReplicator)
app.post("performMaintenance", use: Handlers.performMaintenance)
app.post("getMultipeerReplicatorStatus", use: Handlers.getMultipperReplicatorStatus)
app.post("runQuery", use: Handlers.runQuery)
app.post("startListener", use: Handlers.startListener)
app.post("stopListener", use: Handlers.stopListener)
Log.log(level: .debug, message: "Server configured with the following routes: \n\(app.routes.description)")
}
}
private struct SessionManagerKey: StorageKey {
typealias Value = SessionManager
}
extension Vapor.Application {
var sessionManager : SessionManager {
return self.storage.get(SessionManagerKey.self)!
}
}
/// This extension is temporary and can be removed once Vapor gets this support.
private extension Vapor.Application {
static let baseExecutionQueue = DispatchQueue(label: "vapor.codes.entrypoint", qos: .background)
func runFromAsyncMainEntrypoint() async throws {
try await withCheckedThrowingContinuation { continuation in
Vapor.Application.baseExecutionQueue.async { [self] in
do {
try self.run()
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
}
}