pyanfield 11 роки тому
батько
коміт
7de0bd9b96
4 змінених файлів з 68 додано та 60 видалено
  1. 30 0
      Common/DemoServer.swift
  2. 34 0
      Resources/login.html
  3. 4 0
      Swifter.xcodeproj/project.pbxproj
  4. 0 60
      www/views/login.html

+ 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 */,
 			);

+ 0 - 60
www/views/login.html

@@ -1,60 +0,0 @@
-<!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.2.0/css/bootstrap.min.css" rel="stylesheet" />
-
-    	<title>Login</title>
-  	</head>
-	
-	<body>
-		<div class="container" style="width: 500px;">
-			<form class="form-horizontal" method="POST" action="/login">
-			  <div class="form-group">
-			    <label class="col-lg-4 control-label">Username:</label>
-			    <div class="col-lg-6">
-			      <input id="uname" class="form-control" name="uname" placeholder="Account">
-			    </div>
-			  </div>
-
-			  <div class="form-group">
-			    <label class="col-lg-4 control-label">Password:</label>
-			    <div class="col-lg-6">
-			      <input id="pwd" type="password" class="form-control" name="pwd" placeholder="Password">
-			    </div>
-			  </div>
-
-			  <div class="form-group">
-			    <div class="col-lg-offset-2 col-lg-10">
-			    	<a href="/login"><button type="submit" class="btn btn-default" onclick="return checkInput();">Login</button></a>
-			      	<button class="btn btn-default" onclick="return backToHome();">Go To Home</button>
-			      	<script type="text/javascript">
-				      	function backToHome() {
-				      		window.location.href = "/";
-				      		return false;
-				      	}
-
-				      	function checkInput() {
-				      		var uname = document.getElementById("uname");
-				      		if (uname.value.length == 0) {
-				      			alert("Please Input Your Account");
-				      			return false;
-				      		}
-
-				      		var pwd = document.getElementById("pwd");
-				      		if (pwd.value.length == 0) {
-				      			alert("Please Input Your Password");
-				      			return false;
-				      		}
-				      	}
-			      	</script>
-			    </div>
-			  </div>
-			</form>
-		</div>
-	</body>
-</html>