AppDelegate.swift 1.8 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. server["/"] = {
  16. return .OK("<html><body>Hello Swift</body></html>")
  17. }
  18. server["/redirect"] = {
  19. return .MovedPermanently("http://www.google.com")
  20. }
  21. server["/long"] = {
  22. var longResponse = ""
  23. for k in 0..1000 {
  24. longResponse += "(\(k)),->"
  25. }
  26. return .OK(longResponse)
  27. }
  28. server["/routes"] = {
  29. var listPage = "<html><body>Available services:<br><ul>"
  30. for item in self.server.routes() {
  31. listPage += "<li><a href=\"\(item)\">\(item)</a></li>"
  32. }
  33. listPage += "</ul></body></html>"
  34. return .OK(listPage)
  35. }
  36. server["/demo"] = {
  37. let demoPage =
  38. "<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>" +
  41. "<iframe src=\"/demo2\"></iframe><iframe src=\"/hello\"></iframe></body></html>"
  42. return .OK(demoPage)
  43. }
  44. var error: NSError?
  45. if !server.start(error: &error) {
  46. NSLog("Server start error: \(error)")
  47. }
  48. return true
  49. }
  50. }