main.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. static let jsonEncoder = JSONEncoder()
  14. static let jsonDecoder = JSONDecoder()
  15. var coreContext: Hummingbird.CoreRequestContextStorage
  16. // Use a global JSON Encoder
  17. var responseEncoder: JSONEncoder { Self.jsonEncoder }
  18. // Use a global JSON Decoder
  19. var requestDecoder: JSONDecoder { Self.jsonDecoder }
  20. init(source: ApplicationRequestContextSource) {
  21. self.init(channel: source.channel, logger: source.logger)
  22. }
  23. init(channel: any Channel, logger: Logger) {
  24. self.coreContext = CoreRequestContextStorage(
  25. source: ApplicationRequestContextSource(channel: channel, logger: logger)
  26. )
  27. }
  28. }
  29. func runApp() async throws {
  30. let env = Environment()
  31. let serverHostName = env.get("SERVER_HOSTNAME") ?? "127.0.0.1"
  32. let serverPort = env.get("SERVER_PORT", as: Int.self) ?? 8080
  33. var postgresConfiguration = PostgresClient.Configuration(
  34. host: "tfb-database",
  35. username: "benchmarkdbuser",
  36. password: "benchmarkdbpass",
  37. database: "hello_world",
  38. tls: .disable
  39. )
  40. postgresConfiguration.options.maximumConnections = 100
  41. let postgresClient = PostgresClient(
  42. configuration: postgresConfiguration,
  43. eventLoopGroup: MultiThreadedEventLoopGroup.singleton
  44. )
  45. let router = Router(context: TechFrameworkRequestContext.self)
  46. router.addRoutes(WorldController(postgresClient: postgresClient).routes)
  47. router.addRoutes(FortunesController(postgresClient: postgresClient).routes)
  48. var app = Application(
  49. router: router,
  50. configuration: .init(
  51. address: .hostname(serverHostName, port: serverPort),
  52. serverName: "HB2",
  53. backlog: 8192
  54. )
  55. )
  56. app.addServices(postgresClient)
  57. try await app.runService()
  58. }
  59. try await runApp()