本文整理汇总了Golang中github.com/chinakyc/AgBlog/app.ResponseWriter.WriteHeader方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.WriteHeader方法的具体用法?Golang ResponseWriter.WriteHeader怎么用?Golang ResponseWriter.WriteHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/chinakyc/AgBlog/app.ResponseWriter
的用法示例。
在下文中一共展示了ResponseWriter.WriteHeader方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetArticle
func GetArticle(w app.ResponseWriter, r *app.Request) {
article_id, err := strconv.Atoi(r.PathParams["article_id"])
if err == nil {
article := models.Article{}
article.Find(article_id)
if article.ID != 0 {
w.WriteJson(article)
return
}
}
logging.Logger.Error(fmt.Sprintf("Error: %s", err))
w.WriteHeader(404)
w.WriteJson(map[string]string{"error": "article no found"})
}
示例2: PostArticle
func PostArticle(w app.ResponseWriter, r *app.Request) {
user := r.Env["user"].(*models.User)
title, markdown, category, html := generateArticleContent(r)
if title == "" {
w.WriteHeader(400)
w.WriteJson(map[string]string{"error": "missing stuff"})
return
}
// create article
article := models.Article{Title: title, Content: string(html), Markdown: markdown}
user.AddArticle(&article)
category.AddArticle(&article)
article.Create()
w.WriteJson(article)
}
示例3: ModifyArticle
func ModifyArticle(w app.ResponseWriter, r *app.Request) {
article_id, err := strconv.Atoi(r.PathParams["article_id"])
if err == nil {
title, markdown, _, html := generateArticleContent(r)
article := models.Article{}
article.Find(article_id)
if article.ID != 0 {
article.Title = title
article.Markdown = markdown
article.Content = string(html)
// category.AddArticle(&article)
article.Save()
w.WriteJson(article)
return
}
}
logging.Logger.Error(fmt.Sprintf("Error: %s", err))
w.WriteHeader(404)
w.WriteJson(map[string]string{"error": "article no found"})
}
示例4: LoginController
func (self *authMiddware) LoginController(w app.ResponseWriter, r *app.Request) {
var tokenString string
data := jsonLoginDate{}
// Decode Json from request
err := r.DecodeJsonPayload(&data)
if err != nil {
logging.Logger.Error(fmt.Sprintf("Error: %s", err))
w.WriteHeader(500)
w.WriteJson(map[string]string{"error": fmt.Sprintf("Error: %s", err)})
return
}
// extract
email := data.Email
password := data.Password
// use email get user
user := models.User{}
user.Find(email)
// validate password generate jwt tokenString
// user jwt we can ignore CRSF
if user.Validate(password) {
user.Last_seen = time.Now().UTC()
user.Save()
token := jwt.New(jwt.SigningMethodHS256)
token.Claims["email"] = user.Email
token.Claims["role"] = user.Role
token.Claims["exp"] = time.Now().Add(time.Hour * 6).UTC().Unix()
tokenString, err = token.SignedString(self.signingKey)
if err != nil {
logging.Logger.Error(fmt.Sprintf("Error: %s", err))
w.WriteHeader(500)
w.WriteJson(map[string]string{"error": fmt.Sprintf("Error: %s", err)})
}
w.WriteJson(responseUserData{user.Nickname, tokenString, user.Role})
} else {
w.WriteHeader(400)
w.WriteJson(map[string]string{"error": "email or password incorrect"})
}
}