AppDelegate.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. var c: AnyClass?
  16. server["/json"] = { (method, headers) in
  17. return .OK(.JSON(["posts" : [[ "id" : 1, "message" : "hello world"],[ "id" : 2, "message" : "sample message"]], "new_updates" : false]))
  18. }
  19. server["/redirect"] = { (method, headers) in
  20. return .MovedPermanently("http://www.google.com")
  21. }
  22. server["/long"] = { (method, headers) in
  23. var longResponse = ""
  24. for k in 0..1000 { longResponse += "(\(k)),->" }
  25. return .OK(.RAW(longResponse))
  26. }
  27. server["/routes"] = { (method, headers) in
  28. var listPage = "<html><body>Available services:<br><ul>"
  29. for item in self.server.routes() {
  30. listPage += "<li><a href=\"\(item)\">\(item)</a></li>"
  31. }
  32. listPage += "</ul></body></html>"
  33. return .OK(.RAW(listPage))
  34. }
  35. server["/demo"] = { (method, headers) in
  36. let demoPage =
  37. "<html><body><center><h2>Hello Swift</h2>" +
  38. "<img src=\"https://devimages.apple.com.edgekey.net/swift/images/swift-hero_2x.png\"/><br>" +
  39. "<h4>\(UIDevice().name), \(UIDevice().systemVersion)</h4></center>" +
  40. "<iframe src=\"/routes\"></iframe><iframe src=\"/hello\"></iframe></body></html>"
  41. return .OK(.RAW(demoPage))
  42. }
  43. var error: NSError?
  44. if !server.start(error: &error) {
  45. println("Server start error: \(error)")
  46. }
  47. return true
  48. }
  49. }