示例#1
0
// loginPage authenticates users
func loginPage(writer http.ResponseWriter, req *http.Request) {
	// here we define some static record for comparison
	valid_user := "******"
	valid_pass := "******"

	if req.FormValue("user") == valid_user && req.FormValue("pass") == valid_pass {
		sess, _ := mod_sessions.CheckSession(req, writer)
		sess.Values["session_id"] = sess.ID // we set this to indicate we're logged in.
		mod_sessions.Save(req, writer, sess)
		http.Redirect(writer, req, "/", http.StatusFound)
		return
	}
	http.Redirect(writer, req, "/?error=login", http.StatusFound)
	return
}
示例#2
0
// indexPage() is a handler which will load some template and send the result back to the client
func indexPage(writer http.ResponseWriter, req *http.Request) {
	tpl, err := gwp_template.Load(ctx, "index.html")
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}

	var displayContent bool

	// if session parameter "session_id" is present, we already have session set and we can show the content
	// otherwise, we will display login form
	sess, ok := mod_sessions.CheckSession(req, writer, "session_id")
	if ok {
		displayContent = true
	} else {
		displayContent = false
	}

	var s_id string
	if sid, ok := sess.Values["session_id"]; ok {
		s_id = sid.(string)
	} else {
		s_id = sess.ID
	}

	errmsg := req.FormValue("error")
	var msg string
	if errmsg == "login" {
		msg = "Invalid login"
	}

	mydata := Example{ID: s_id, Name: "Joe", LoggedIn: displayContent, ErrorMsg: msg}
	buff := new(bytes.Buffer)

	tpl.Execute(buff, mydata)
	writer.Write(buff.Bytes())
}