main.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import Fluent
  2. import FluentPostgresDriver
  3. import Vapor
  4. var env = try Environment.detect()
  5. try LoggingSystem.bootstrap(from: &env)
  6. let app = Application(env)
  7. defer { app.shutdown() }
  8. app.middleware.use(ServerMiddleware())
  9. app.logger.notice("💧 VAPOR")
  10. app.logger.notice("System.coreCount: \(System.coreCount)")
  11. app.logger.notice("System.maxConnectionsPerEventLoop: \(System.maxConnectionsPerEventLoop)")
  12. app.databases.use(.postgres(
  13. hostname: "tfb-database",
  14. username: "benchmarkdbuser",
  15. password: "benchmarkdbpass",
  16. database: "hello_world",
  17. maxConnectionsPerEventLoop: System.maxConnectionsPerEventLoop
  18. ), as: .psql)
  19. app.get("plaintext") { req in
  20. "Hello, world!"
  21. }
  22. app.get("json") { req in
  23. ["message": "Hello, world!"]
  24. }
  25. app.get("db") { req in
  26. World.find(.random(in: 1...10_000), on: req.db)
  27. .unwrap(or: Abort(.notFound))
  28. }
  29. app.get("queries") { req -> EventLoopFuture<[World]> in
  30. let queries = (req.query["queries"] ?? 1).bounded(to: 1...500)
  31. return (0 ..< queries).map { _ -> EventLoopFuture<World> in
  32. World.find(.random(in: 1...10_000), on: req.db)
  33. .unwrap(or: Abort(.notFound))
  34. }.flatten(on: req.eventLoop)
  35. }
  36. try app.run()