golang 简单的登录操作 http

主函数 main.go

// testHtmlLogin project main.go
package main

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

	. "github.com/soekchl/myUtils"
)

var (
	change = make(map[string][]byte)
)

func init() {
	loadHtml("login", "login.html")
	loadHtml("home", "home.html")
}

func main() {

	http.HandleFunc("/", home)
	http.HandleFunc("/login", Login)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		Error(err)
	}
}

func home(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "%s", change["home"])
}

// 控制各个模块的 log输出开关
func Login(w http.ResponseWriter, r *http.Request) {
	user := r.FormValue("user")
	passwd := r.FormValue("passwd")
	Notice("user=", user, " passwd=", passwd)
	if len(user) == 0 || len(passwd) == 0 {
		fmt.Fprintf(w, "%s", change["login"])
	} else {
		http.Redirect(w, r, "/", http.StatusFound) // 跳转回主页
	}
}

func loadHtml(key, file_name string) {
	info, err := readFile(file_name)
	if err != nil {
		Error(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)
}

启动程序先加载 html内容


home.html




	
		
		主页
	

	
		
		登录
		
	
 


login.html




	
		
		登录
	

	
		
		
用户名:
密码:


启动程序  访问  http://localhost:8080/ 

你可能感兴趣的:(技术,html5,golang)