// // 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)
" } let response = "Url: \(request.url)
Method: \(request.method)
\(headersInfo)" 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("

Hello Swift

" + "
" + "

\(UIDevice().name), \(UIDevice().systemVersion)

")) } 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 = "Available services:
" return .OK(.RAW(listPage)) } var error: NSError? if !server.start(error: &error) { println("Server start error: \(error)") } return true } }