예제 #1
0
func Post(w http.ResponseWriter, r *http.Request) (*models.Page, int) {

	vars := mux.Vars(r)
	category_url, post_url := vars["category"], vars["post_url"]

	_, category_exist := models.GetPageByUrl(category_url)

	if !category_exist {
		return nil, 404
	}

	post_page, post_exist := models.GetPageByUrl(post_url)
	if !post_exist {
		return nil, 404
	}

	if post_page.GetTemplate().ID == 5 { // redirect
		return post_page, 301
	}

	cook, cookie_err := r.Cookie("av")
	if cookie_err != nil && cook == nil {
		post_page.ViewedCount++
		post_page.Save()
		http.SetCookie(w, &http.Cookie{Name: "av", Value: "1", Expires: time.Now().AddDate(1, 0, 0)})
	}

	w.Header().Add("Last-Modified", post_page.Updated_at.Format(time.RFC1123))

	return post_page, 200
}
예제 #2
0
func Api(w http.ResponseWriter, r *http.Request) (*models.Page, int) {
	err := r.ParseMultipartForm(1000)
	if err != nil {
		fmt.Println("Error when parse POST data: " + err.Error())
	}

	user_cookie, err := r.Cookie("UserID")
	if err != nil {
		fmt.Println("Trying to save data with API from non-user. Error: " + err.Error())
		return nil, 404
	}

	user_id, _ := strconv.Atoi(user_cookie.Value)
	user, _ := models.GetUserByID(user_id)
	if user.Role != "admin" {
		fmt.Println("Trying to save data with API from not admin user. Error: " + err.Error())
		return nil, 404
	}

	url_parts := strings.Split(strings.Trim(r.Referer(), "/"), "/")
	post, _ := models.GetPageByUrl(url_parts[len(url_parts)-1])
	post.Content = r.MultipartForm.Value["content"][0]
	post.Title = r.MultipartForm.Value["title"][0]
	post.Description = r.MultipartForm.Value["description"][0]
	post.Save()

	return nil, 200
}
예제 #3
0
func Category(w http.ResponseWriter, r *http.Request) (*models.Page, int) {

	vars := mux.Vars(r)

	page, exist := models.GetPageByUrl(vars["category"])

	if !exist {
		return nil, 404
	}

	return page, 200
}
예제 #4
0
func Root(w http.ResponseWriter, r *http.Request) (*models.Page, int) {

	homepage, homepage_exist := models.GetPageByUrl("/")

	if !homepage_exist {
		return homepage, 404
	}

	//	cook, err := r.Cookie("last-viewed")
	//	if err == nil && cook != nil {
	//		lv_date, lv_err := time.Parse(time.RFC3339, cook.Value)
	//		if lv_err != nil {
	//			fmt.Println(lv_err.Error())
	//		}
	//		homepage.LastViewedDate = lv_date
	//	} else {
	//		homepage.LastViewedDate = time.Now() // by default
	//	}

	http.SetCookie(w, &http.Cookie{Name: "last-viewed", Value: time.Now().Format(time.RFC3339), Expires: time.Now().AddDate(1, 0, 0)})

	return homepage, 200
}