AppDelegate.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // AppDelegate.swift
  3. // TestSwift
  4. //
  5. // Created by Damian Kolakowski on 05/06/14.
  6. // Copyright (c) 2014 Damian Kołakowski. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. @UIApplicationMain
  11. class AppDelegate: UIResponder, UIApplicationDelegate {
  12. var window: UIWindow?
  13. let server: HttpServer = HttpServer()
  14. func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
  15. // if let resDir = NSBundle.mainBundle().resourcePath {
  16. // server["/resources/(.+)"] = resDir
  17. // }
  18. server["/test"] = { request in
  19. var headersInfo = ""
  20. for (name, value) in request.headers {
  21. headersInfo += "\(name) : \(value)<br>"
  22. }
  23. let response = "<html><body>Url: \(request.url)<br>Method: \(request.method)<br>\(headersInfo)</body></html>"
  24. return .OK(.RAW(response))
  25. }
  26. server["/json"] = { request in
  27. return .OK(.JSON(["posts" : [[ "id" : 1, "message" : "hello world"],[ "id" : 2, "message" : "sample message"]], "new_updates" : false]))
  28. }
  29. server["/redirect"] = { request in
  30. return .MovedPermanently("http://www.google.com")
  31. }
  32. server["/long"] = { request in
  33. var longResponse = ""
  34. for k in 0..<1000 { longResponse += "(\(k)),->" }
  35. return .OK(.RAW(longResponse))
  36. }
  37. server["/demo"] = { request in
  38. return .OK(.RAW("<html><body><center><h2>Hello Swift</h2>" +
  39. "<img src=\"https://devimages.apple.com.edgekey.net/swift/images/swift-hero_2x.png\"/><br>" +
  40. "<h4>\(UIDevice().name), \(UIDevice().systemVersion)</h4></center></body></html>"))
  41. }
  42. server["/"] = { request in
  43. var listPage = "<html><body>Available services:<br><ul>"
  44. for item in self.server.routes() {
  45. listPage += "<li><a href=\"\(item)\">\(item)</a></li>"
  46. }
  47. listPage += "</ul></body></html>"
  48. return .OK(.RAW(listPage))
  49. }
  50. var error: NSError?
  51. if !server.start(error: &error) {
  52. println("Server start error: \(error)")
  53. }
  54. return true
  55. }
  56. }