当前位置: 首页>>代码示例>>Golang>>正文


Golang util.GetResponseWriter函数代码示例

本文整理汇总了Golang中controllers/util.GetResponseWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang GetResponseWriter函数的具体用法?Golang GetResponseWriter怎么用?Golang GetResponseWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了GetResponseWriter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: get

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)
}
开发者ID:timchunght,项目名称:gocart,代码行数:7,代码来源:home_controller.go

示例2: get

func (this *categoryController) get(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	idRaw := vars["id"]
	id, err := strconv.Atoi(idRaw)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	category, err := models.GetCategoryById(id)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	vm := viewmodels.GetCategoryPage(category.Title())
	productVMs := []viewmodels.Product{}

	for _, product := range category.Products() {
		productVMs = append(productVMs, converters.ConvertProductToViewModel(product))
	}
	vm.Products = productVMs

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

	this.template.Execute(responseWriter, vm)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:30,代码来源:categories.go

示例3: login

func (this *loginController) login(w http.ResponseWriter, req *http.Request) {
	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	if req.Method == "POST" {
		email := req.FormValue("email")
		password := req.FormValue("password")

		member, err := models.GetMember(email, password)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		session, err := models.CreateSession(member)
		if err != nil {
			responseWriter.Write([]byte(err.Error()))
			return
		}

		var cookie http.Cookie
		cookie.Name = "sessionId"
		cookie.Value = session.SessionId()
		responseWriter.Header().Add("Set-Cookie", cookie.String())
	}
	vm := viewmodels.GetLogin()
	this.template.Execute(responseWriter, vm)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:29,代码来源:login.go

示例4: get

func (this *aboutController) get(w http.ResponseWriter, req *http.Request) {
	skills := models.GetSkills()
	skillsVM := []viewmodels.Skill{}

	for _, modelSkills := range skills {
		skillsVM = append(skillsVM, converter.ConvertSkillToViewModel(modelSkills))
	}

	vm := viewmodels.GetAbout()

	vm.Skills = skillsVM

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

	responseWriter := util.GetResponseWriter(w, req)

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

	this.template.Execute(responseWriter, vm)
	defer responseWriter.Close()
}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:26,代码来源:about.go

示例5: serveResource

func serveResource(w http.ResponseWriter, req *http.Request) {
	path := "public" + req.URL.Path
	var contentType string

	if strings.HasSuffix(path, ".css") {
		contentType = "text/css"
	} else if strings.HasSuffix(path, ".js") {
		contentType = "application/javascript"
	} else if strings.HasSuffix(path, ".png") {
		contentType = "image/png"
	} else if strings.HasSuffix(path, ".mp4") {
		contentType = "video/mp4"
	} else {
		contentType = "text/plain"
	}

	f, err := os.Open(path)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	defer f.Close()
	w.Header().Add("Content-Type", contentType)

	// add gzip compression if possible
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	br := bufio.NewReader(f)
	br.WriteTo(responseWriter)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:32,代码来源:main.go

示例6: get

func (this *categoryController) get(w http.ResponseWriter, req *http.Request) {

	_, er := req.Cookie("goSessionId")

	if er == nil {
		// This expects a map for the parameters caught by the current request
		vars := mux.Vars(req)

		idRaw := vars["id"]
		id, err := strconv.Atoi(idRaw)

		if err == nil && id < 4 {
			Products, _ := models.GetProductList(id)
			productsVM := []viewmodels.Product{}

			for _, modelProducts := range Products {
				productsVM = append(productsVM, converter.ConvertProductsToViewModel(modelProducts))
			}

			vm := viewmodels.GetProducts()
			vm.LoggedIn = true
			vm.Products = productsVM

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

			responseWriter := util.GetResponseWriter(w, req)
			defer responseWriter.Close()
			this.template.Execute(responseWriter, vm)
		} else {
			w.WriteHeader(404)
		}
	} else {
		http.Redirect(w, req, "/home", http.StatusFound)
	}
}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:35,代码来源:categories.go

示例7: get

func (this *productController) get(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	idRaw := vars["id"]
	id, err := strconv.Atoi(idRaw)
	if err != nil {
		w.WriteHeader(404)
		return
	}

	product, err := models.GetProductById(id)

	if err != nil {
		w.WriteHeader(404)
		return
	}

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

	vm := viewmodels.GetProductPage(product.Name())
	vm.Product = converters.ConvertProductToViewModel(product)
	this.template.Execute(responseWriter, vm)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:25,代码来源:product.go

示例8: get

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)

}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:30,代码来源:home.go

示例9: get

func (this *standLocatorController) get(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	vm := viewmodels.GetStandLocator()
	responseWriter.Header().Add("Content-Type", "text/html")
	this.template.Execute(responseWriter, vm)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:8,代码来源:stand_locator.go

示例10: apiSearch

func (this *standLocatorController) apiSearch(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	responseWriter.Header().Add("Content-Type", "application/json")

	vm := viewmodels.GetStandLocations()
	data, err := json.Marshal(vm)
	if err == nil {
		responseWriter.Write(data)
	} else {
		responseWriter.WriteHeader(404)
	}
}
开发者ID:brewinvaz,项目名称:mvc-webapp,代码行数:13,代码来源:stand_locator.go

示例11: login

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)
		}
	}
}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:50,代码来源:home.go

示例12: get

func (this *productController) get(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	idRaw := vars["id"]

	id, err := strconv.Atoi(idRaw)
	if err == nil {
		vm := viewmodels.GetProduct(id)
		w.Header().Add("Content-Type", "text/html")
		responseWriter := util.GetResponseWriter(w, req)
		defer responseWriter.Close()
		this.template.Execute(responseWriter, vm)
	} else {
		w.WriteHeader(404)
	}
}
开发者ID:brewinvaz,项目名称:mvc-webapp-old,代码行数:15,代码来源:product.go

示例13: get

func (this *categoriesController) get(w http.ResponseWriter, req *http.Request) {
	categories := models.GetCategories()

	categoriesVM := []viewmodels.Category{}
	for _, category := range categories {
		categoriesVM = append(categoriesVM, converters.ConvertCategoryToViewModel(category))
	}

	vm := viewmodels.GetCategories()
	vm.Categories = categoriesVM

	w.Header().Add("Content-Type", "text/html")
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()
	this.template.Execute(responseWriter, vm)
}
开发者ID:brewinvaz,项目名称:mvc-webapp,代码行数:16,代码来源:categories.go

示例14: handle

func (this *profileController) handle(w http.ResponseWriter, req *http.Request) {
	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	vm := viewmodels.GetProfile()
	if req.Method == "POST" {
		vm.User.Email = req.FormValue("email")
		vm.User.FirstName = req.FormValue("firstName")
		vm.User.LastName = req.FormValue("lastName")
		vm.User.Stand.Address = req.FormValue("standAddress")
		vm.User.Stand.City = req.FormValue("standCity")
		vm.User.Stand.County = req.FormValue("standCounty")
		vm.User.Stand.Postcode = req.FormValue("standPostcode")
	}
	responseWriter.Header().Add("Content-Type", "text/html")
	this.template.Execute(responseWriter, vm)
}
开发者ID:obadonke,项目名称:psGoWebApp,代码行数:17,代码来源:profile.go

示例15: signup

func (this *homeController) signup(w http.ResponseWriter, req *http.Request) {

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

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

	if req.Method == "POST" {
		firstName := req.FormValue("firstName")
		email := req.FormValue("email")
		password := req.FormValue("password")

		err := models.InsertMember(firstName, email, password)

		if err == nil {
			member, _ := models.GetMember(email, password)

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

			if errr == 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())
				}

				http.Redirect(w, req, "/home", http.StatusFound)
			}
		}
	}

	vm := viewmodels.GetSignup()

	this.signupTemplate.Execute(responseWriter, vm)

}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:46,代码来源:home.go


注:本文中的controllers/util.GetResponseWriter函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。