示例#1
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	// Since this controller method is only responsible for a single VIEW, its implementation becomes really simple

	// Grab a view model that is preconfigured with MOCK data using the VIEW model GETHOME
	vm := viewmodels.GetHome()

	// Tell the browser that we will be sending HTML by setting the content-type header on the response
	w.Header().Add("Content-Type", "text/html")

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	_, err := req.Cookie("goSessionId")
	if err == nil {
		vm.LoggedIn = true

		ckn, _ := req.Cookie("loggedName")

		vm.LoggedName = ckn.Value
	} else {
		vm.LoggedIn = false
	}

	/* We execute the controllers template field.
	This small/lite controlled funciton is important
	Due the controllers position in the MVC pattern it is aware of both of the other layer. This makes it easy to use the
	controller to handle things that really are the responsability of another portion of the application */
	this.template.Execute(responseWriter, vm)

}
示例#2
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetHome()
	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	this.template.Execute(responseWriter, vm)
}
示例#3
0
func (this *homeController) login(w http.ResponseWriter, req *http.Request) {

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	responseWriter.Header().Add("Content-Type", "text/html")

	vm := viewmodels.GetLogin()

	if req.FormValue("submit") == "signup" {
		http.Redirect(w, req, "/signup", http.StatusFound)
	} else {

		if req.Method == "POST" {

			email := req.FormValue("email")
			password := req.FormValue("password")

			member, err := models.GetMember(email, password)

			if err == nil {
				session, err := models.CreateSession(member)

				if err == nil {
					var cookie http.Cookie
					cookie.Name = "goSessionId"
					cookie.Expires = time.Now().Add(10 * time.Minute)
					cookie.Value = strconv.Itoa(session.MemberId())
					responseWriter.Header().Add("Set-Cookie", cookie.String())

					var cookie2 http.Cookie
					cookie2.Name = "loggedName"
					cookie2.Expires = time.Now().Add(10 * time.Minute)
					cookie2.Value = member.FirstName()
					responseWriter.Header().Add("Set-Cookie", cookie2.String())
				}
				vmh := viewmodels.GetHome()

				vmh.LoggedIn = true
				vmh.LoggedName = member.FirstName()

				this.template.Execute(responseWriter, vmh)
			} else {
				this.loginTemplate.Execute(responseWriter, vm)
			}
		} else {
			this.loginTemplate.Execute(responseWriter, vm)
		}
	}
}
示例#4
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetHome()

	/*
	 *sessionCookie, err := req.Cookie("sessionId")
	 *if err == nil {
	 *    member, err := models.GetMemberBySessionId(sessionCookie.Value)
	 *    if err == nil {
	 *        vm.Member.IsLoggedIn = true
	 *        vm.Member.FirstName = member.FirstName()
	 *    }
	 *}
	 */

	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	this.template.Execute(responseWriter, vm)
}
示例#5
0
func (this *homeController) get(w http.ResponseWriter, req *http.Request) {
	vm := viewmodels.GetHome()

	w.Header().Add("Content Type", "text/html")
	this.template.Execute(w, vm)
}