快速上手go Web服务器端
任务目标
- 熟悉 go 服务器工作原理
- 基于现有 web 库,编写一个简单 web 应用类似 cloudgo。
- 使用 curl 工具访问 web 程序
- 对 web 执行压力测试
welcome to drop by my gitee
https://gitee.com/woodx9/go/tree/master/webService
效果图
登录界面
服务器端代码
service.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」