main.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright IBM Corporation 2018
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Kitura
  17. import LoggerAPI
  18. import HeliumLogger
  19. import MongoKitten
  20. import KituraStencil
  21. import Stencil
  22. //import KituraMustache
  23. import TechEmpowerCommon
  24. Log.logger = HeliumLogger(.info)
  25. // Stencil stuff
  26. let ext = Extension()
  27. // Stencil does not yet support automatic HTML escaping:
  28. // https://github.com/kylef/Stencil/pull/80
  29. //
  30. ext.registerFilter("htmlencode") { (value: Any?) in
  31. if let value = value as? String {
  32. return value
  33. .replacingOccurrences(of: "&", with: "&")
  34. .replacingOccurrences(of: "<", with: "&lt;")
  35. .replacingOccurrences(of: ">", with: "&gt;")
  36. .replacingOccurrences(of: "'", with: "&apos;")
  37. .replacingOccurrences(of: "\"", with: "&quot;")
  38. }
  39. return value
  40. }
  41. let router = Router()
  42. router.add(templateEngine: StencilTemplateEngine(extension: ext))
  43. //router.add(templateEngine: MustacheTemplateEngine())
  44. //
  45. // TechEmpower test 2: Single database query (raw, no ORM)
  46. //
  47. router.get("/db") {
  48. request, response, next in
  49. response.headers["Server"] = "Kitura"
  50. let dict = try getRandomRow()
  51. try response.status(.OK).send(json: dict).end()
  52. }
  53. //
  54. // TechEmpower test 3: Multiple database queries (raw, no ORM)
  55. // Get param provides number of queries: /queries?queries=N
  56. //
  57. router.get("/queries") {
  58. request, response, next in
  59. response.headers["Server"] = "Kitura"
  60. let queriesParam = request.queryParameters["queries"] ?? "1"
  61. let numQueries = max(1, min(Int(queriesParam) ?? 1, 500)) // Snap to range of 1-500 as per test spec
  62. var results: [[String:Int]] = []
  63. for _ in 1...numQueries {
  64. let dict = try getRandomRow()
  65. results.append(dict)
  66. }
  67. // Return JSON representation of array of results
  68. try response.status(.OK).send(json: results).end()
  69. }
  70. //
  71. // TechEmpower test 4: fortunes (raw, no ORM)
  72. //
  73. router.get("/fortunes") {
  74. request, response, next in
  75. response.headers["Server"] = "Kitura"
  76. response.headers["Content-Type"] = "text/html; charset=UTF-8"
  77. var fortunes = try getFortunes()
  78. fortunes.append(Fortune(id: 0, message: "Additional fortune added at request time."))
  79. try response.render("fortunes.stencil", context: ["fortunes": fortunes.sorted()]).end()
  80. //try response.render("fortunes.mustache", context: ["fortunes": fortunes.sorted()]).end()
  81. }
  82. //
  83. // TechEmpower test 5: updates (raw, no ORM)
  84. //
  85. router.get("/updates") {
  86. request, response, next in
  87. response.headers["Server"] = "Kitura"
  88. let queriesParam = request.queryParameters["queries"] ?? "1"
  89. let numQueries = max(1, min(Int(queriesParam) ?? 1, 500)) // Snap to range of 1-500 as per test spec
  90. var results: [[String:Int]] = []
  91. for _ in 1...numQueries {
  92. let dict = try updateRandomRow()
  93. results.append(dict)
  94. }
  95. // Return JSON representation of array of results
  96. try response.status(.OK).send(json: results).end()
  97. }
  98. // Initialize MongoDB connection
  99. do {
  100. try connectToDB()
  101. } catch {
  102. print("Error connecting to database: \(error)")
  103. }
  104. Kitura.addHTTPServer(onPort: 8080, with: router)
  105. Kitura.run()