main.go

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/gorilla/mux"
)

var (
	change = make(map[string][]byte)
	//puser=make(map[int]string)
	//ppasswd=make(map[int]string)
	i, temp int = 0, 0
	userMap     = make(map[string]string)
)

type Cookie struct {
	Name       string
	Value      string
	Path       string
	Domain     string
	Expires    time.Time
	RawExpires string
	MaxAge     int
	Secure     bool
	HttpOnly   bool
	Raw        string
	Unparsed   []string
}

func init() {
	loadHtml("login", "/home/guaniu/code/src/http/login.html")
	loadHtml("home", "/home/guaniu/code/src/http/home.html")
	loadHtml("err", "/home/guaniu/code/src/http/err.html")
	loadHtml("reg", "/home/guaniu/code/src/http/reg.html")
	loadHtml("errtwo", "/home/guaniu/code/src/http/errtwo.html")
}
func main() {
	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/", Login)
	router.HandleFunc("/home", home)
	router.HandleFunc("/err", err)
	router.HandleFunc("/reg", reg)
	router.HandleFunc("/errtwo", errtwo)
	log.Fatal(http.ListenAndServe(":8080", router))
	/*
		http.HandleFunc("/", Login)
		http.HandleFunc("/home", home)
		http.HandleFunc("/err", err)
		err := http.ListenAndServe(":8080", nil)
		if err != nil {
			fmt.Print(err)
	} */
}
func Login(w http.ResponseWriter, r *http.Request) {
	user := r.FormValue("user")
	passwd := r.FormValue("passwd")

	if len(user) == 0 || len(passwd) == 0 {
		fmt.Fprintf(w, "%s", change["login"])
	} else {

		pw, ok := userMap[user]
		if ok == true {
			if pw == passwd {

				cookieUser := http.Cookie{Name: "user", Value: user, Path: "/", MaxAge: 86400}
				cookiePasswd := http.Cookie{Name: "password", Value: passwd, Path: "/", MaxAge: 86400}
				http.SetCookie(w, &cookieUser)
				http.SetCookie(w, &cookiePasswd)
				http.Redirect(w, r, "/home", http.StatusFound)

			} else {
				http.Redirect(w, r, "/err", http.StatusFound)
			}
		} else {
			http.Redirect(w, r, "/err", http.StatusFound)
		}

		// for j:=0; j<=i ;j++ {
		// 	if user==puser[j]&& passwd==ppasswd[j]{
		// 		temp=1
		// 		break
		// 	}
		// }
		// if temp==1{
		// 	cookieUser := http.Cookie{Name:"user",Value:user,Path:"/",MaxAge:86400}
		// 	cookiePasswd := http.Cookie{Name:"password",Value:passwd,Path:"/",MaxAge:86400}
		// 	http.SetCookie(w,&cookieUser)
		// 	http.SetCookie(w,&cookiePasswd)
		// 	http.Redirect(w, r, "/home", http.StatusFound)
		// 	temp=0
		// }else{
		// 	http.Redirect(w, r, "/err", http.StatusFound)
		// }
	}
}
func home(w http.ResponseWriter, r *http.Request) {
	//fmt.Fprintf(w, "%s", change["home"])
	cookieUser, err := r.Cookie("user")
	if err == nil {
		cookieUserValue := cookieUser.Value
		//将数据写入http连接中
		w.Write([]byte("用户名为:" + cookieUserValue))
	} else {
		w.Write([]byte("读取cookie信息出错:" + err.Error()))
	}
	cookiePasswd, err := r.Cookie("password")
	if err == nil {
		cookiePasswdValue := cookiePasswd.Value
		//将数据写入http连接中
		w.Write([]byte("密码为:" + cookiePasswdValue))

		// cookie := http.Cookie{Name: "testcookiename", Path: "/", MaxAge: -1}
		// http.SetCookie(w, &cookie)
	} else {
		w.Write([]byte("读取cookie信息出错:" + err.Error()))
	}
}
func err(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", change["err"])
}
func errtwo(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", change["errtwo"])
}
func reg(w http.ResponseWriter, r *http.Request) {
	ruser := r.FormValue("ruser")
	rpasswd := r.FormValue("rpasswd")
	rpasswdtwo := r.FormValue("rpasswdtwo")
	if len(ruser) == 0 || len(rpasswd) == 0 || len(rpasswdtwo) == 0 {
		fmt.Fprintf(w, "%s", change["reg"])
	} else {
		if rpasswd != rpasswdtwo {
			http.Redirect(w, r, "/errtwo", http.StatusFound)
		} else {
			// puser[i]=ruser
			// ppasswd[i]=rpasswd
			// i++

			userMap[ruser] = rpasswd

			http.Redirect(w, r, "/", http.StatusFound)
		}
	}
}
func loadHtml(key, file_name string) {
	info, err := readFile(file_name)
	if err != nil {
		fmt.Print(err)
		return
	}
	change[key] = info
}
func readFile(file_name string) ([]byte, error) {
	fi, err := os.Open(file_name)
	if err != nil {
		panic(err)
	}
	defer fi.Close()
	return ioutil.ReadAll(fi)
}

reg.html

<!DOCTYPE html>
 
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<title>注册</title>
	</head>
 
	<body background=/home/guaniu/code/src/http/background/Firefox_wallpaper.png>
        <form id="form" action="" method="POST">
            <br> <br>
            <table border="dotted" align="center" style="width:300px; height:60px;">
                <tr>
                    <th colspan="2" style="font-family:'华文仿宋';font-size:30px;";>注册</th>
                </tr>
                <tr align="center">
                    <td height="60">
                            <br>
                                 用户名  :<input type="text" name="ruser"><br><br>
                                 密     码 :<input type="password" name="rpasswd"><br><br>
                            确认密码:<input type="password" name="rpasswdtwo"><br><br>
                            <input type="submit" name="res" value="注 册">
                            <!--<a href="/reg"><input type="submit"  value="注 册"></a>-->
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html> 

login.html

<!DOCTYPE html>
 
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<title>登录</title>
	</head>
 
	<body background=https://img-blog.csdnimg.cn/img_convert/332508b648e09bc0c109983059a2cd8e.png;
        <form id="form" action="" method="POST">
            <br> <br>
            <table border="dotted" align="center" style="width:300px; height:60px;">
                <tr>
                    <th colspan="2" style="font-family:'华文仿宋';font-size:30px;";>首页</th>
                </tr>
                <tr align="center">
                    <td height="60">
                            <br>
                            用户名:<input type="text" name="user"><br><br>
			                密     码: <input type="password" name="passwd"><br><br>
                            <input type="submit" value="登 录">
                            <style type="text/css">a {text-decoration: none}</style>
                            <a href="/reg"><input type="button"  value="注 册"></a>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html> 

home.html

<!DOCTYPE html>
 
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<title>home</title>
	</head>
 
	<body>
		
		<a href="/">welcome!</a>
		
	</body>
</html> 

err.html

<!DOCTYPE html>
 
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<title>error</title>
	</head>
 
	<body>
		
		<a href="/">密码或用户名错误!</a>
		
	</body>
</html> 

errtwo.html

<!DOCTYPE html>
 
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<title>error</title>
	</head>
 
	<body>
		
		<a href="/reg">两次输入的密码不一致!</a>
		
	</body>
</html> 
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐