import Vapor import Fluent var server: SQLiteDatabase! // configures your application public func configure(_ app: Application) throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) let configPath = URL(fileURLWithPath: app.directory.resourcesDirectory) .appendingPathComponent("config.json") let configuration = try Configuration(loadFromUrl: configPath) app.http.server.configuration.port = configuration.serverPort // Set target environment app.environment = .production if app.environment == .development { app.logger.logLevel = .info print("[DEVELOPMENT] Using in-memory database") app.databases.use(.sqlite(.memory), as: .sqlite) } else { app.logger.logLevel = .notice let dbFile = storageFolder.appendingPathComponent("db.sqlite").path print("[PRODUCTION] Using database at \(dbFile)") app.databases.use(.sqlite(.file(dbFile)), as: .sqlite) } app.migrations.add(UserTableMigration()) try app.autoMigrate().wait() // serve files from /Public folder app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) let db = app.databases.database(.sqlite, logger: .init(label: "Init"), on: app.databases.eventLoopGroup.next())! server = try SQLiteDatabase(db: db) // Gracefully shut down by closing potentially open socket DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + .seconds(5)) { _ = app.server.onShutdown.always { _ in server.disconnectAllSockets() } } // register routes try routes(app) }