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.
57 lines
1.1 KiB
57 lines
1.1 KiB
3 years ago
|
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"])
|
||
|
fmt.Println("password:", r.Form["password"])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
http.HandleFunc("/", sayHelloName)
|
||
|
http.HandleFunc("/login", login)
|
||
|
err := http.ListenAndServe(":9090", nil)
|
||
|
if err != nil {
|
||
|
log.Fatal("ListenAddServer: ", err)
|
||
|
}
|
||
|
}
|