Example #1
0
func ArticlePostHandler(w http.ResponseWriter, r *http.Request, action string) {
	if r.Method == "GET" {
		model := make(map[string]interface{})
		model["title"] = "Post"
		model["action"] = "post"
		ExecuteTemplate(w, "post", model)
		return
	}

	if r.Method != "POST" {
		return
	}

	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.NotFound(w, r)
		return
	}

	var dat map[string]interface{}
	err = json.Unmarshal(body, &dat)
	if err != nil {
		http.NotFound(w, r)
		return
	}

	title := fmt.Sprint(dat["title"])
	content := fmt.Sprint(dat["content"])
	keywords := fmt.Sprint(dat["keywords"])
	description := fmt.Sprint(dat["description"])
	lang := fmt.Sprint(dat["lang"])
	tag := fmt.Sprint(dat["tag"])
	status := fmt.Sprint(dat["status"])

	var article entity.Article
	article.Title = title
	article.Content = content
	article.Keywords = keywords
	article.Description = description
	article.Lang = lang
	article.Status = status
	article.Tag = tag

	service.SaveArticle(article)

	tmp := time.Unix(time.Now().Unix(), 0).Format("20060102")
	tmp2 := strings.Replace(title, " ", "-", -1)
	filename := "posted/" + tmp + "-" + tmp2 + ".txt"
	ioutil.WriteFile(filename, []byte(content), 0600)

	http.Redirect(w, r, "/articles", http.StatusFound)
}
Example #2
0
func ArticleSaveHandler(w http.ResponseWriter, r *http.Request, action string) {
	if action != "post" {
		http.NotFound(w, r)
		return
	}

	title := r.FormValue("title")
	content := r.FormValue("content")
	keywords := r.FormValue("keywords")
	description := r.FormValue("description")
	lang := r.FormValue("lang")
	tag := r.FormValue("tag")
	status := r.FormValue("status")

	if utils.IsEmpty(title) {
		OnResponse(w, 201, "标题不能为空", nil)
		return
	}
	if utils.IsEmpty(content) {
		OnResponse(w, 202, "内容不能为空", nil)
		return
	}

	var article entity.Article
	article.Title = title
	article.Content = content
	article.Keywords = keywords
	article.Description = description
	article.Lang = lang
	article.Status = status
	article.Tag = tag

	service.SaveArticle(article)

	tmp := time.Unix(time.Now().Unix(), 0).Format("20060102")
	tmp2 := strings.Replace(title, " ", "-", -1)
	filename := "posted/" + tmp + "-" + tmp2 + ".txt"
	ioutil.WriteFile(filename, []byte(content), 0600)

	http.Redirect(w, r, "/articles", http.StatusFound)
}