-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.go
68 lines (53 loc) · 1.47 KB
/
webserver.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type BaseJsonBean struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Message string `json:"message"`
}
func NewBaseJsonBean() *BaseJsonBean {
return &BaseJsonBean{}
}
func main() {
fmt.Println("This is webserver base!")
//第一个参数为客户端发起http请求时的接口名,第二个参数是一个func,负责处理这个请求。
http.HandleFunc("/login", loginTask)
//服务器要监听的主机地址和端口号
err := http.ListenAndServe(":8888", nil)
if err != nil {
fmt.Println("ListenAndServe error: ", err.Error())
}
}
func loginTask(w http.ResponseWriter, req *http.Request) {
fmt.Println("loginTask is running...")
//模拟延时
time.Sleep(time.Second * 2)
//获取客户端通过GET/POST方式传递的参数
req.ParseForm()
param_userName, found1 := req.Form["userName"]
param_password, found2 := req.Form["password"]
if !(found1 && found2) {
fmt.Fprint(w, "请勿非法访问")
return
}
result := NewBaseJsonBean()
userName := param_userName[0]
password := param_password[0]
s := "userName:" + userName + ",password:" + password
fmt.Println(s)
if userName == "zhangsan" && password == "123456" {
result.Code = 100
result.Message = "登录成功"
} else {
result.Code = 101
result.Message = "用户名或密码不正确"
}
//向客户端返回JSON数据
bytes, _ := json.Marshal(result)
fmt.Fprint(w, string(bytes))
}