main.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 KituraMustache
  20. import TechEmpowerCommon
  21. import KueryPostgresRaw
  22. Log.logger = HeliumLogger(.info)
  23. let router = Router()
  24. router.add(templateEngine: MustacheTemplateEngine())
  25. //
  26. // TechEmpower test 4: fortunes (raw, no ORM)
  27. //
  28. router.get("/fortunes") {
  29. request, response, next in
  30. response.headers["Server"] = "Kitura"
  31. response.headers["Content-Type"] = "text/html; charset=UTF-8"
  32. getFortunes { (fortunes, err) in
  33. guard var fortunes = fortunes else {
  34. guard let err = err else {
  35. Log.error("Unknown Error")
  36. try? response.status(.badRequest).send("Unknown error").end()
  37. return
  38. }
  39. Log.error("\(err)")
  40. try? response.status(.badRequest).send("Error: \(err)").end()
  41. return
  42. }
  43. fortunes.append(Fortune(id: 0, message: "Additional fortune added at request time."))
  44. do {
  45. try response.render("fortunes.mustache", context: ["fortunes": fortunes.sorted()]).end()
  46. } catch {
  47. print("Error: \(error)")
  48. }
  49. }
  50. }
  51. Kitura.addHTTPServer(onPort: 8080, with: router)
  52. Kitura.run()