AppDelegate.swift 2.2 KB

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