You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.5 KiB

package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sayHelloName(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
return
}
fmt.Println(r.Form)
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key: ", k)
fmt.Println("val: ", strings.Join(v, ""))
}
_, err = fmt.Fprintf(w, "hello astaxie!")
if err != nil {
return
}
}
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
return
}
fmt.Println("method:", r.Method) //获取请求的方法
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
err := t.Execute(w, nil)
if err != nil {
return
}
} else {
//请求的是登陆数据,那么执行登陆的逻辑判断
fmt.Println("username:", r.Form["username"][0])
fmt.Println("password:", r.Form["password"][0])
fmt.Println("age:", r.Form["age"][0])
if len(r.Form["username"][0]) == 0 {
//为空的处理
fmt.Println("username is empty!!!")
}
//getint, err := strconv.Atoi()(r.Form.Get("age"))
if err != nil {
fmt.Println("this is an error")
}
//if getint > 100 {
// fmt.Println("too old")
//}
}
}
func main() {
http.HandleFunc("/", sayHelloName)
http.HandleFunc("/login", login)
fmt.Println("http://127.0.0.1:9090")
fmt.Println("http://127.0.0.1:9090/login")
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAddServer: ", err)
}
}