快速上手go Web服务器端

任务目标

  1. 熟悉 go 服务器工作原理
  2. 基于现有 web 库,编写一个简单 web 应用类似 cloudgo。
  3. 使用 curl 工具访问 web 程序
  4. 对 web 执行压力测试

 

welcome to drop by my gitee

https://gitee.com/woodx9/go/tree/master/webService

 

效果图

登录界面

 

 

 

 

服务器端代码

service.go

package main
 
import (
	"fmt"
	"html/template"
	"log"
	"net/http"
)

type Information struct {
	Username string
	Password string
}
 
func TestHandle(res http.ResponseWriter, req *http.Request) {
	admin := req.FormValue("admin")
	password := req.FormValue("password")
 
	fmt.Println("account: " + admin)
	fmt.Println("password: " + password + "\nregisted")
 
	if admin != "woodx" || password != "123456" {
		res.Write([]byte("Login Fail,Please Try Again!"))
	} else {
		info := new(Information)
		info.Username = admin;
		info.Password = password;
		t, err := template.ParseFiles("./static/login.html")
		if err != nil {
			http.Error(res, err.Error(), http.StatusInternalServerError)
			return 
		}
		t.Execute(res, info)
		return
	}
}
 
func main() {
	port := "3000"
 
	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		t, err := template.ParseFiles("./static/index.html")
		if err != nil {
			log.Println("err")
		}
		t.Execute(res, nil)
	})
 
	http.HandleFunc("/Login", TestHandle)
 
	fmt.Println("start http server at:", port)
	http.ListenAndServe(":"+port, nil)
}

 

static/index.html

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>登陆页面</title>
		<link rel="stylesheet" type="text/css" href="./mycss.css" />
	<script type="text/javascript" src="./myjs.js"> </script>
	</head>
	
	 <body>
		<h1>登陆页面</h1>
		<form method="post" action="/Login">
			<table>
				<tr>
				<td>用户名</td><td><input type="text" name="admin"></input></td>
				</tr>
				<tr>
				<td>密码</td><td><input type="password" name="password"></input></td>
				</tr>
				<tr>
					<td><input type="submit" value="登陆" style="width:100px;height:25px"></input></td>
				</tr>
			</table>
		</form>
	</body>
</html>

 

static/login.html

<html>
<head>
    <title>Login</title>
</head>
<body>
    <table>
        <tr>
            <th>Username</th>
            <th>Password</th>
        </tr>
        <tr>
            <th>{{.Username}}</th>
            <th>{{.Password}}</th>
        </tr>
    </table>

</body>
</html>

 

curl test

woodx@woodx-VirtualBox:~/Desktop$ curl -v localhost:3000
*   Trying 127.0.0.1:3000...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Mon, 23 Nov 2020 15:26:41 GMT
< Content-Length: 668
< Content-Type: text/html; charset=utf-8
< 
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>登陆页面</title>
		<link rel="stylesheet" type="text/css" href="./mycss.css" />
	<script type="text/javascript" src="./myjs.js"> </script>
	</head>
	
	 <body>
		<h1>登陆页面</h1>
		<form method="post" action="/Login">
			<table>
				<tr>
				<td>用户名</td><td><input type="text" name="admin"></input></td>
				</tr>
				<tr>
				<td>密码</td><td><input type="password" name="password"></input></td>
				</tr>
				<tr>
					<td><input type="submit" value="登陆" style="width:100px;height:25px"></input></td>
				</tr>
			</table>
		</form>
	</body>
</html>
* Connection #0 to host localhost left intact

 

woodx@woodx-VirtualBox:~/Desktop$ curl -v localhost:3000/Login
*   Trying 127.0.0.1:3000...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /Login HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Mon, 23 Nov 2020 15:29:11 GMT
< Content-Length: 28
< Content-Type: text/plain; charset=utf-8
< 
* Connection #0 to host localhost left intact
Login Fail,Please Try Again!

 

ab test

woodx@woodx-VirtualBox:~/Desktop$ ab -n 1000 -c 10 http://localhost:3000/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            3000

Document Path:          /
Document Length:        668 bytes

Concurrency Level:      10
Time taken for tests:   0.320 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      785000 bytes
HTML transferred:       668000 bytes
Requests per second:    3123.64 [#/sec] (mean)
Time per request:       3.201 [ms] (mean)
Time per request:       0.320 [ms] (mean, across all concurrent requests)
Transfer rate:          2394.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    3   2.5      2      18
Waiting:        0    2   2.1      1      18
Total:          0    3   2.5      2      18

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      3
  75%      4
  80%      4
  90%      6
  95%      7
  98%     10
  99%     17
 100%     18 (longest request)

 

posted @ 2020-11-23 23:36  woodx  阅读(588)  评论(0)    收藏  举报