Compare commits
10 Commits
62ae594e06
...
master
Author | SHA1 | Date | |
---|---|---|---|
091d9554ad | |||
9bd7174bdd | |||
479635344d | |||
10194066db | |||
32055ecdec | |||
dabc94b8b5 | |||
a08dc4f175 | |||
e62ccb9241 | |||
1e649f297e | |||
2974c63fec |
@ -2,7 +2,7 @@
|
|||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "SchafkopfServer",
|
name: "Schafkopf-Server",
|
||||||
platforms: [
|
platforms: [
|
||||||
.macOS(.v12)
|
.macOS(.v12)
|
||||||
],
|
],
|
||||||
@ -20,7 +20,7 @@ let package = Package(
|
|||||||
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
|
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.1"),
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.executableTarget(
|
||||||
name: "App",
|
name: "App",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.product(name: "Fluent", package: "fluent"),
|
.product(name: "Fluent", package: "fluent"),
|
||||||
@ -30,18 +30,7 @@ let package = Package(
|
|||||||
.product(name: "Clairvoyant", package: "Clairvoyant"),
|
.product(name: "Clairvoyant", package: "Clairvoyant"),
|
||||||
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
|
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
|
||||||
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
|
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
|
||||||
],
|
|
||||||
swiftSettings: [
|
|
||||||
// Enable better optimizations when building in Release configuration. Despite the use of
|
|
||||||
// the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
|
|
||||||
// builds. See <https://github.com/swift-server/guides/blob/main/docs/building.md#building-for-production> for details.
|
|
||||||
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
|
|
||||||
]
|
]
|
||||||
),
|
)
|
||||||
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
|
|
||||||
// .testTarget(name: "AppTests", dependencies: [
|
|
||||||
// .target(name: "App"),
|
|
||||||
// .product(name: "XCTVapor", package: "vapor"),
|
|
||||||
// ])
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
If the server runs under https://example.com/schafkopf
|
If the server runs under https://example.com/schafkopf
|
||||||
then apiPath = "/schafkopf"
|
then apiPath = "/schafkopf"
|
||||||
*/
|
*/
|
||||||
const apiPath = "/schafkopf"
|
const apiPath = ""
|
||||||
|
|
||||||
var useEnglishTexts = false
|
var useEnglishTexts = false
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
{
|
{
|
||||||
"serverPort": 8000,
|
"serverPort": 8000,
|
||||||
"production": false,
|
|
||||||
"mail": {
|
"mail": {
|
||||||
"serverDomain": "https://example.com/schafkopf",
|
"serverDomain": "https://example.com/schafkopf",
|
||||||
"emailHostname": "example.com",
|
"emailHostname": "example.com",
|
||||||
@ -9,4 +8,5 @@
|
|||||||
"tokenExpiryDuration": 15,
|
"tokenExpiryDuration": 15,
|
||||||
},
|
},
|
||||||
"monitoringTokens": [],
|
"monitoringTokens": [],
|
||||||
|
"dataDirectory" : "/data/schafkopf"
|
||||||
}
|
}
|
||||||
|
@ -1,34 +1,10 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import Clairvoyant
|
|
||||||
import Vapor
|
import Vapor
|
||||||
import NIOCore
|
import Clairvoyant
|
||||||
|
|
||||||
final class EventLoopScheduler {
|
extension MultiThreadedEventLoopGroup: AsyncScheduler {
|
||||||
|
|
||||||
private let backgroundGroup: EventLoopGroup
|
public func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
||||||
|
_ = any().makeFutureWithTask(asyncJob)
|
||||||
init(numberOfThreads: Int = 2) {
|
|
||||||
backgroundGroup = MultiThreadedEventLoopGroup(numberOfThreads: numberOfThreads)
|
|
||||||
}
|
|
||||||
|
|
||||||
func next() -> EventLoop {
|
|
||||||
backgroundGroup.next()
|
|
||||||
}
|
|
||||||
|
|
||||||
func provider() -> NIOEventLoopGroupProvider {
|
|
||||||
return .shared(backgroundGroup)
|
|
||||||
}
|
|
||||||
|
|
||||||
func shutdown() {
|
|
||||||
backgroundGroup.shutdownGracefully { _ in
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension EventLoopScheduler: AsyncScheduler {
|
|
||||||
|
|
||||||
func schedule(asyncJob: @escaping @Sendable () async throws -> Void) {
|
|
||||||
_ = backgroundGroup.any().makeFutureWithTask(asyncJob)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,6 @@ struct Configuration {
|
|||||||
|
|
||||||
let mail: EMail?
|
let mail: EMail?
|
||||||
|
|
||||||
/**
|
|
||||||
Use a database file and reduce logging.
|
|
||||||
|
|
||||||
If this property is set to `false`, then an in-memory database is used with increased logging.
|
|
||||||
*/
|
|
||||||
let production: Bool
|
|
||||||
|
|
||||||
struct EMail {
|
struct EMail {
|
||||||
|
|
||||||
/// The url to the root of the server
|
/// The url to the root of the server
|
||||||
@ -30,9 +23,39 @@ struct Configuration {
|
|||||||
/// The number of minutes until a password reset token is no longer valid
|
/// The number of minutes until a password reset token is no longer valid
|
||||||
let tokenExpiryDuration: Int
|
let tokenExpiryDuration: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
The folder where the data should be stored.
|
||||||
|
|
||||||
|
If the folder is set to `nil`, then the `Resources` folder is used.
|
||||||
|
*/
|
||||||
|
let dataDirectory: String?
|
||||||
|
|
||||||
/// The authentication tokens to access the metrics
|
/// The authentication tokens to access the metrics
|
||||||
let monitoringTokens: Set<String>
|
let monitoringTokens: Set<String>
|
||||||
|
|
||||||
|
/// The path to the folder where the metric logs are stored
|
||||||
|
///
|
||||||
|
/// If no path is provided, then a folder `logs` in the resources directory is created
|
||||||
|
/// If the path is relative, then it is assumed relative to the resources directory
|
||||||
|
let logPath: String?
|
||||||
|
|
||||||
|
func logURL(possiblyRelativeTo resourcesDirectory: URL) -> URL {
|
||||||
|
guard let logPath else {
|
||||||
|
return resourcesDirectory.appendingPathComponent("logs")
|
||||||
|
}
|
||||||
|
guard !logPath.hasPrefix("/") else {
|
||||||
|
return .init(fileURLWithPath: logPath)
|
||||||
|
}
|
||||||
|
return resourcesDirectory.appendingPathComponent(logPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func customDataDirectory(or publicDirectory: String) -> URL {
|
||||||
|
guard let dataDirectory else {
|
||||||
|
return URL(fileURLWithPath: publicDirectory)
|
||||||
|
}
|
||||||
|
return URL(fileURLWithPath: dataDirectory)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Configuration {
|
extension Configuration {
|
||||||
|
@ -50,6 +50,13 @@ actor SQLiteDatabase {
|
|||||||
private let mail: MailConfig?
|
private let mail: MailConfig?
|
||||||
|
|
||||||
private let registeredPlayerCountMetric: Metric<Int>
|
private let registeredPlayerCountMetric: Metric<Int>
|
||||||
|
|
||||||
|
private let dateFormatter = DateFormatter()
|
||||||
|
|
||||||
|
func printServerStartMessage() async {
|
||||||
|
let players = await registeredPlayerCountMetric.lastValue()?.value ?? 0
|
||||||
|
log("[\(dateFormatter.string(from: Date()))] Server started (\(players) players, \(tables.tableCount) tables)")
|
||||||
|
}
|
||||||
|
|
||||||
init(database: Database, mail: Configuration.EMail?) async throws {
|
init(database: Database, mail: Configuration.EMail?) async throws {
|
||||||
self.registeredPlayerCountMetric = Metric(
|
self.registeredPlayerCountMetric = Metric(
|
||||||
@ -60,6 +67,9 @@ actor SQLiteDatabase {
|
|||||||
self.mail = mail?.mailConfig
|
self.mail = mail?.mailConfig
|
||||||
|
|
||||||
await updateRegisteredPlayerCount(from: database)
|
await updateRegisteredPlayerCount(from: database)
|
||||||
|
|
||||||
|
dateFormatter.dateStyle = .short
|
||||||
|
dateFormatter.timeStyle = .short
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerPlayer(named name: PlayerName, hash: PasswordHash, email: String?, in database: Database) async throws -> SessionToken {
|
func registerPlayer(named name: PlayerName, hash: PasswordHash, email: String?, in database: Database) async throws -> SessionToken {
|
||||||
@ -86,10 +96,14 @@ actor SQLiteDatabase {
|
|||||||
await updateRegisteredPlayerCount(from: database)
|
await updateRegisteredPlayerCount(from: database)
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func playerCount(in database: Database) async throws -> Int {
|
||||||
|
try await User.query(on: database).count()
|
||||||
|
}
|
||||||
|
|
||||||
func updateRegisteredPlayerCount(from database: Database) async {
|
func updateRegisteredPlayerCount(from database: Database) async {
|
||||||
do {
|
do {
|
||||||
let count = try await User.query(on: database).count()
|
let count = try await playerCount(in: database)
|
||||||
_ = try? await registeredPlayerCountMetric.update(count)
|
_ = try? await registeredPlayerCountMetric.update(count)
|
||||||
} catch {
|
} catch {
|
||||||
log("Failed to update player count metric: \(error)")
|
log("Failed to update player count metric: \(error)")
|
||||||
@ -320,7 +334,12 @@ actor SQLiteDatabase {
|
|||||||
return try await tables.play(card: card, player: player, in: database)
|
return try await tables.play(card: card, player: player, in: database)
|
||||||
}
|
}
|
||||||
|
|
||||||
func disconnectAllSockets() async {
|
func shutdown() async {
|
||||||
|
await disconnectAllSockets()
|
||||||
|
log("[\(dateFormatter.string(from: Date()))] Server shutdown")
|
||||||
|
}
|
||||||
|
|
||||||
|
private func disconnectAllSockets() async {
|
||||||
await tables.disconnectAllSockets()
|
await tables.disconnectAllSockets()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,10 @@ final class TableManagement {
|
|||||||
|
|
||||||
/// The metric describing the number of players currently connected via a websocket
|
/// The metric describing the number of players currently connected via a websocket
|
||||||
private let connectedPlayerCountMetric: Metric<Int>
|
private let connectedPlayerCountMetric: Metric<Int>
|
||||||
|
|
||||||
|
var tableCount: Int {
|
||||||
|
tables.count
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Load the tables from a file in the storage folder
|
Load the tables from a file in the storage folder
|
||||||
@ -58,7 +62,6 @@ final class TableManagement {
|
|||||||
let id = table.id!
|
let id = table.id!
|
||||||
self.tables[id] = WaitingTable(id: id, name: table.name, isPublic: table.isPublic, players: table.players)
|
self.tables[id] = WaitingTable(id: id, name: table.name, isPublic: table.isPublic, players: table.players)
|
||||||
}
|
}
|
||||||
log("\(tables.count) tables loaded")
|
|
||||||
await logTableCount()
|
await logTableCount()
|
||||||
await logPlayingPlayerCount()
|
await logPlayingPlayerCount()
|
||||||
await logConnectedPlayerCount()
|
await logConnectedPlayerCount()
|
||||||
|
@ -8,29 +8,17 @@ var server: SQLiteDatabase!
|
|||||||
|
|
||||||
private var provider: VaporMetricProvider! = nil
|
private var provider: VaporMetricProvider! = nil
|
||||||
private var status: Metric<ServerStatus>!
|
private var status: Metric<ServerStatus>!
|
||||||
private let scheduler = EventLoopScheduler()
|
private let scheduler = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
||||||
private var configurationError: Error? = nil
|
|
||||||
|
|
||||||
public func configure(_ app: Application) throws {
|
func configure(_ app: Application) async throws {
|
||||||
let semaphore = DispatchSemaphore(value: 0)
|
let resourceFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
scheduler.schedule {
|
let publicDirectory = app.directory.publicDirectory
|
||||||
do {
|
|
||||||
try await configureAsync(app)
|
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
||||||
} catch {
|
.appendingPathComponent("config.json")
|
||||||
configurationError = error
|
let configuration = try Configuration(loadFromUrl: configPath)
|
||||||
}
|
|
||||||
semaphore.signal()
|
|
||||||
}
|
|
||||||
semaphore.wait()
|
|
||||||
if let configurationError {
|
|
||||||
throw configurationError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func configureAsync(_ app: Application) async throws {
|
let logFolder = configuration.logURL(possiblyRelativeTo: resourceFolder)
|
||||||
let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
||||||
|
|
||||||
let logFolder = storageFolder.appendingPathComponent("logs")
|
|
||||||
let monitor = MetricObserver(
|
let monitor = MetricObserver(
|
||||||
logFileFolder: logFolder,
|
logFileFolder: logFolder,
|
||||||
logMetricId: "schafkopf.log")
|
logMetricId: "schafkopf.log")
|
||||||
@ -41,37 +29,22 @@ private func configureAsync(_ app: Application) async throws {
|
|||||||
name: "Status",
|
name: "Status",
|
||||||
description: "The main status of the server")
|
description: "The main status of the server")
|
||||||
|
|
||||||
_ = try? await status.update(.initializing)
|
try await status.update(.initializing)
|
||||||
|
|
||||||
let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory)
|
|
||||||
.appendingPathComponent("config.json")
|
|
||||||
let configuration: Configuration
|
|
||||||
|
|
||||||
do {
|
|
||||||
configuration = try Configuration(loadFromUrl: configPath)
|
|
||||||
} catch {
|
|
||||||
_ = try? await status.update(.initializationFailure)
|
|
||||||
await monitor.log("Failed to read configuration: \(error)")
|
|
||||||
// Note: If configuration can't be loaded, then the server will run on the wrong port
|
|
||||||
// and access to metrics is impossible, since no tokens are loaded
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
app.http.server.configuration.port = configuration.serverPort
|
app.http.server.configuration.port = configuration.serverPort
|
||||||
|
|
||||||
// Set target environment
|
switch app.environment {
|
||||||
app.environment = .production
|
case .production:
|
||||||
|
|
||||||
if !configuration.production {
|
|
||||||
app.logger.logLevel = .info
|
|
||||||
log("[DEVELOPMENT] Using in-memory database")
|
|
||||||
app.databases.use(.sqlite(.memory), as: .sqlite)
|
|
||||||
} else {
|
|
||||||
app.logger.logLevel = .notice
|
app.logger.logLevel = .notice
|
||||||
let dbFile = storageFolder.appendingPathComponent("db.sqlite").path
|
let dataDirectory = configuration.customDataDirectory(or: publicDirectory)
|
||||||
|
let dbFile = dataDirectory.appendingPathComponent("db.sqlite").path
|
||||||
log("[PRODUCTION] Using database at \(dbFile)")
|
log("[PRODUCTION] Using database at \(dbFile)")
|
||||||
app.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
|
app.databases.use(.sqlite(.file(dbFile)), as: .sqlite)
|
||||||
|
default:
|
||||||
|
log("[DEVELOPMENT] Using in-memory database")
|
||||||
|
app.databases.use(.sqlite(.memory), as: .sqlite)
|
||||||
}
|
}
|
||||||
|
|
||||||
app.migrations.add(UserTableMigration())
|
app.migrations.add(UserTableMigration())
|
||||||
app.migrations.add(PasswordResetMigration())
|
app.migrations.add(PasswordResetMigration())
|
||||||
|
|
||||||
@ -79,7 +52,7 @@ private func configureAsync(_ app: Application) async throws {
|
|||||||
try await app.autoMigrate()
|
try await app.autoMigrate()
|
||||||
} catch {
|
} catch {
|
||||||
await monitor.log("Failed to migrate database: \(error)")
|
await monitor.log("Failed to migrate database: \(error)")
|
||||||
_ = try? await status.update(.initializationFailure)
|
try await status.update(.initializationFailure)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,15 +62,6 @@ private func configureAsync(_ app: Application) async throws {
|
|||||||
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
|
let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())!
|
||||||
server = try await SQLiteDatabase(database: db, mail: configuration.mail)
|
server = try await SQLiteDatabase(database: db, mail: configuration.mail)
|
||||||
|
|
||||||
// Gracefully shut down by closing potentially open socket
|
|
||||||
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) {
|
|
||||||
_ = app.server.onShutdown.always { _ in
|
|
||||||
scheduler.schedule {
|
|
||||||
await server.disconnectAllSockets()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// register routes
|
// register routes
|
||||||
routes(app)
|
routes(app)
|
||||||
|
|
||||||
@ -105,8 +69,17 @@ private func configureAsync(_ app: Application) async throws {
|
|||||||
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
|
provider = .init(observer: monitor, accessManager: configuration.monitoringTokens)
|
||||||
provider.asyncScheduler = scheduler
|
provider.asyncScheduler = scheduler
|
||||||
provider.registerRoutes(app)
|
provider.registerRoutes(app)
|
||||||
|
monitor.saveCurrentListOfMetricsToLogFolder()
|
||||||
|
|
||||||
_ = try? await status.update(.nominal)
|
try await status.update(.nominal)
|
||||||
|
await server.printServerStartMessage()
|
||||||
|
}
|
||||||
|
|
||||||
|
func shutdown() {
|
||||||
|
Task {
|
||||||
|
await server.shutdown()
|
||||||
|
try await scheduler.shutdownGracefully()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func log(_ message: String) {
|
func log(_ message: String) {
|
||||||
|
43
Sources/App/entrypoint.swift
Normal file
43
Sources/App/entrypoint.swift
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import Vapor
|
||||||
|
import Dispatch
|
||||||
|
import Logging
|
||||||
|
|
||||||
|
/// 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")
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@main
|
||||||
|
enum Entrypoint {
|
||||||
|
static func main() async throws {
|
||||||
|
var env = try Environment.detect()
|
||||||
|
try LoggingSystem.bootstrap(from: &env)
|
||||||
|
|
||||||
|
let app = Application(env)
|
||||||
|
defer {
|
||||||
|
shutdown()
|
||||||
|
app.shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
try await configure(app)
|
||||||
|
} catch {
|
||||||
|
app.logger.report(error: error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
try await app.runFromAsyncMainEntrypoint()
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
import App
|
|
||||||
import Vapor
|
|
||||||
|
|
||||||
var env = try Environment.detect()
|
|
||||||
try LoggingSystem.bootstrap(from: &env)
|
|
||||||
let app = Application(env)
|
|
||||||
defer { app.shutdown() }
|
|
||||||
|
|
||||||
try configure(app)
|
|
||||||
try app.run()
|
|
@ -1,15 +0,0 @@
|
|||||||
@testable import App
|
|
||||||
import XCTVapor
|
|
||||||
|
|
||||||
final class AppTests: XCTestCase {
|
|
||||||
func testHelloWorld() throws {
|
|
||||||
let app = Application(.testing)
|
|
||||||
defer { app.shutdown() }
|
|
||||||
try configure(app)
|
|
||||||
|
|
||||||
try app.test(.GET, "hello", afterResponse: { res in
|
|
||||||
XCTAssertEqual(res.status, .ok)
|
|
||||||
XCTAssertEqual(res.body.string, "Hello, world!")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user