AppDelegate.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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["/login"] = { request in
  43. println(">> request method:\(request.method.uppercaseString)")
  44. var method = request.method.uppercaseString
  45. if method == "GET" {
  46. //TODO: should copy the file to document path of app's
  47. //TODO: should not use the absolute path
  48. if let html = String.stringWithContentsOfFile("/Users/wshan/Workspace/swifter/www/views/login.html", encoding: NSUTF8StringEncoding, error: nil){
  49. return .OK(.RAW(html))
  50. }else{
  51. return .NotFound
  52. }
  53. }else if method == "POST"{
  54. println(">> post data: \(NSString(data:request.responseData!,encoding:NSUTF8StringEncoding))")
  55. return .MovedPermanently("http://github.com")
  56. }
  57. return .NotFound
  58. }
  59. server["/"] = { request in
  60. var listPage = "<html><body>Available services:<br><ul>"
  61. for item in self.server.routes() {
  62. listPage += "<li><a href=\"\(item)\">\(item)</a></li>"
  63. }
  64. listPage += "</ul></body></html>"
  65. return .OK(.RAW(listPage))
  66. }
  67. var error: NSError?
  68. if !server.start(error: &error) {
  69. println("Server start error: \(error)")
  70. }
  71. return true
  72. }
  73. }