Explorar o código

add a test login page to test post method

pyanfield %!s(int64=11) %!d(string=hai) anos
pai
achega
6e69220d8d
Modificáronse 2 ficheiros con 80 adicións e 0 borrados
  1. 20 0
      Swifter/AppDelegate.swift
  2. 60 0
      www/views/login.html

+ 20 - 0
Swifter/AppDelegate.swift

@@ -43,6 +43,24 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
                 "<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() {
@@ -51,6 +69,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
             listPage += "</ul></body></html>"
             return .OK(.RAW(listPage))
         }
+
+        
         var error: NSError?
         if !server.start(error: &error) {
             println("Server start error: \(error)")

+ 60 - 0
www/views/login.html

@@ -0,0 +1,60 @@
+<!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>