main.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Foundation
  2. import Hummingbird
  3. import PostgresNIO
  4. // postgresql.conf specifies max_connections = 2000
  5. // https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Environment#citrine-self-hosted
  6. // https://github.com/TechEmpower/FrameworkBenchmarks/blob/master/toolset/databases/postgres/postgresql.conf#L64
  7. extension Int {
  8. func bound(_ minValue: Int, _ maxValue: Int) -> Int {
  9. return Swift.min(maxValue, Swift.max(minValue, self))
  10. }
  11. }
  12. struct TechFrameworkRequestContext: RequestContext {
  13. var coreContext: CoreRequestContextStorage
  14. init(source: ApplicationRequestContextSource) {
  15. self.coreContext = CoreRequestContextStorage(source: source)
  16. }
  17. }
  18. func runApp() async throws {
  19. let env = Environment()
  20. let serverHostName = env.get("SERVER_HOSTNAME") ?? "127.0.0.1"
  21. let serverPort = env.get("SERVER_PORT", as: Int.self) ?? 8080
  22. var postgresConfiguration = PostgresClient.Configuration(
  23. host: "tfb-database",
  24. username: "benchmarkdbuser",
  25. password: "benchmarkdbpass",
  26. database: "hello_world",
  27. tls: .disable
  28. )
  29. postgresConfiguration.options.maximumConnections = 200
  30. let postgresClient = PostgresClient(
  31. configuration: postgresConfiguration,
  32. eventLoopGroup: MultiThreadedEventLoopGroup.singleton
  33. )
  34. let router = Router(context: TechFrameworkRequestContext.self)
  35. router.addRoutes(WorldController(postgresClient: postgresClient).routes)
  36. router.addRoutes(FortunesController(postgresClient: postgresClient).routes)
  37. var app = Application(
  38. router: router,
  39. configuration: .init(
  40. address: .hostname(serverHostName, port: serverPort),
  41. serverName: "HB2",
  42. backlog: 8192
  43. )
  44. )
  45. app.addServices(postgresClient)
  46. try await app.runService()
  47. }
  48. try await runApp()