| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- //
- // AppDelegate.swift
- // TestSwift
- //
- // Created by Damian Kolakowski on 05/06/14.
- // Copyright (c) 2014 Damian Kołakowski. All rights reserved.
- //
- import Foundation
- import UIKit
- @UIApplicationMain
- class AppDelegate: UIResponder, UIApplicationDelegate {
-
- var window: UIWindow?
- let server: HttpServer = HttpServer()
-
- func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
- if let resDir = NSBundle.mainBundle().resourcePath {
- server["/resources/(.+)"] = resDir
- }
- server["/test"] = { request in
- var headersInfo = ""
- for (name, value) in request.headers {
- headersInfo += "\(name) : \(value)<br>"
- }
- let response = "<html><body>Url: \(request.url)<br>Method: \(request.method)<br>\(headersInfo)</body></html>"
- return .OK(.RAW(response))
- }
- server["/json"] = { request in
- return .OK(.JSON(["posts" : [[ "id" : 1, "message" : "hello world"],[ "id" : 2, "message" : "sample message"]], "new_updates" : false]))
- }
- server["/redirect"] = { request in
- return .MovedPermanently("http://www.google.com")
- }
- server["/long"] = { request in
- var longResponse = ""
- for k in 0..<1000 { longResponse += "(\(k)),->" }
- return .OK(.RAW(longResponse))
- }
- server["/demo"] = { request in
- return .OK(.RAW("<html><body><center><h2>Hello Swift</h2>" +
- "<img src=\"https://devimages.apple.com.edgekey.net/swift/images/swift-hero_2x.png\"/><br>" +
- "<h4>\(UIDevice().name), \(UIDevice().systemVersion)</h4></center></body></html>"))
- }
- server["/login"] = { request in
- println(">> request method:\(request.method.uppercaseString)")
- var method = request.method.uppercaseString
- if method == "GET" {
- //TODO: should copy the file to document path of app's
- //TODO: should not use the absolute path
- if let html = String.stringWithContentsOfFile("/Users/wshan/Workspace/swifter/www/views/login.html", encoding: NSUTF8StringEncoding, error: nil){
- return .OK(.RAW(html))
- }else{
- return .NotFound
- }
- }else if method == "POST"{
- println(">> post data: \(NSString(data:request.responseData!,encoding:NSUTF8StringEncoding))")
- return .MovedPermanently("http://github.com")
- }
-
- return .NotFound
- }
- server["/"] = { request in
- var listPage = "<html><body>Available services:<br><ul>"
- for item in self.server.routes() {
- listPage += "<li><a href=\"\(item)\">\(item)</a></li>"
- }
- listPage += "</ul></body></html>"
- return .OK(.RAW(listPage))
- }
-
- var error: NSError?
- if !server.start(error: &error) {
- println("Server start error: \(error)")
- }
- return true
- }
- }
|