本文整理匯總了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)
}
示例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)
}
示例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)
}
示例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()
}
示例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)
}
示例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)
}
}
示例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)
}
示例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)
}
示例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)
}
示例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)
}
}
示例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)
}
}
}
示例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)
}
}
示例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)
}
示例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)
}
示例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)
}