Browse Source

Merge pull request #13 from pyanfield/master

add a login page as a sample code
Damian Kołakowski 11 years ago
parent
commit
d838697fd3
4 changed files with 94 additions and 0 deletions
  1. 26 0
      .gitignore
  2. 30 0
      Common/DemoServer.swift
  3. 34 0
      Resources/login.html
  4. 4 0
      Swifter.xcodeproj/project.pbxproj

+ 26 - 0
.gitignore

@@ -0,0 +1,26 @@
+# Xcode
+#
+build/
+*.pbxuser
+!default.pbxuser
+*.mode1v3
+!default.mode1v3
+*.mode2v3
+!default.mode2v3
+*.perspectivev3
+!default.perspectivev3
+xcuserdata
+*.xccheckout
+*.moved-aside
+DerivedData
+*.hmap
+*.ipa
+*.xcuserstate
+
+# CocoaPods
+#
+# We recommend against adding the Pods directory to your .gitignore. However
+# you should judge for yourself, the pros and cons are mentioned at:
+# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
+#
+# Pods/

+ 30 - 0
Common/DemoServer.swift

@@ -45,6 +45,36 @@ func demoServer(publicDir: String?) -> HttpServer {
             "<img src=\"https://devimages.apple.com.edgekey.net/swift/images/swift-hero_2x.png\"/><br>" +
             "</center></body></html>"))
     }
+    server["/login"] = { request in
+        println(">> request method:\(request.method.uppercaseString)")
+        var method = request.method.uppercaseString
+        if method == "GET" {
+            if let rootDir = publicDir{
+                if let html = String(contentsOfFile:"\(rootDir)/login.html", encoding: NSUTF8StringEncoding, error: nil){
+                    return .OK(.RAW(html))
+                }else{
+                    return .NotFound
+                }
+            }
+        }else if method == "POST"{
+            println(">> post data: \(NSString(data:request.body!,encoding:NSUTF8StringEncoding))")
+            var html = "<html><body>"
+            let body = NSString(data: request.body!, encoding: NSUTF8StringEncoding)
+            if let parameters = body?.componentsSeparatedByString("&"){
+                if let email = parameters[0] as? String{
+                    html += "\(email)<br>"
+                }
+                if let password = parameters[1] as? String{
+                    html += "\(password)"
+                }
+            }
+            html += "</body></html>"
+            return .OK(.RAW(html))
+            //return .MovedPermanently("http://github.com")
+        }
+        
+        return .NotFound
+    }
     server["/"] = { request in
         var listPage = "<html><body>Available services:<br><ul>"
         for item in server.routes() {

+ 34 - 0
Resources/login.html

@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html>
+  	<head>
+		<link rel="shortcut icon" href="/static/img/favicon.png" />
+    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+		<link href="http://cdn.staticfile.org/twitter-bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
+    	<title>Login</title>
+  	</head>
+	<body>
+        <div class="col-md-12">
+            <div class="modal-dialog" style="margin-bottom:0">
+                <div class="modal-content">
+                    <div class="panel-heading">
+                        <h3 class="panel-title">Sign In</h3>
+                    </div>
+                    <div class="panel-body">
+                        <form role="form" method="POST" action="/login">
+                            <fieldset>
+                                <div class="form-group">
+                                    <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus="">
+                                </div>
+                                <div class="form-group">
+                                    <input class="form-control" placeholder="Password" name="password" type="password" value="">
+                                </div>
+                                <a href="/login"><button type="submit" class="btn btn-default">Login</button></a>
+                            </fieldset>
+                        </form>
+                    </div>
+                </div>
+            </div>
+        </div>
+        <script type="text/javascript" src="http://cdn.staticfile.org/twitter-bootstrap/3.3.0/js/bootstrap.min.js"></script>
+	</body>
+</html>

+ 4 - 0
Swifter.xcodeproj/project.pbxproj

@@ -31,6 +31,7 @@
 		7CB102DD1A167FFA00CBA3B4 /* DemoServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CB102DC1A167FFA00CBA3B4 /* DemoServer.swift */; };
 		7CB102DE1A1680EA00CBA3B4 /* DemoServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CB102DC1A167FFA00CBA3B4 /* DemoServer.swift */; };
 		7CB102E01A17381D00CBA3B4 /* logo.png in Resources */ = {isa = PBXBuildFile; fileRef = 7CB102DF1A17381D00CBA3B4 /* logo.png */; };
+		98630C071A1C9A9D00478D08 /* login.html in Resources */ = {isa = PBXBuildFile; fileRef = 98630C061A1C9A9D00478D08 /* login.html */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -65,6 +66,7 @@
 		7CB102D91A1664B200CBA3B4 /* HttpHandlers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HttpHandlers.swift; sourceTree = "<group>"; };
 		7CB102DC1A167FFA00CBA3B4 /* DemoServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoServer.swift; sourceTree = "<group>"; };
 		7CB102DF1A17381D00CBA3B4 /* logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo.png; sourceTree = "<group>"; };
+		98630C061A1C9A9D00478D08 /* login.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = login.html; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -159,6 +161,7 @@
 		7CA4815619A2EF2B0030B30D /* Resources */ = {
 			isa = PBXGroup;
 			children = (
+				98630C061A1C9A9D00478D08 /* login.html */,
 				7CB102DF1A17381D00CBA3B4 /* logo.png */,
 				7CA4815719A2EF2B0030B30D /* test.json */,
 			);
@@ -245,6 +248,7 @@
 			files = (
 				7CB102E01A17381D00CBA3B4 /* logo.png in Resources */,
 				7C839B7919422CFF003A6950 /* Main.storyboard in Resources */,
+				98630C071A1C9A9D00478D08 /* login.html in Resources */,
 				7C839B7B19422CFF003A6950 /* Images.xcassets in Resources */,
 				7CA4815819A2EF2B0030B30D /* test.json in Resources */,
 			);