Beispiel #1
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
}
Beispiel #2
0
func Route(pattern string, action func(http.ResponseWriter, *http.Request) (*models.Page, int)) {
	router.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {

		CurrentUser = nil

		page_model, code := action(w, r)

		if code == 404 {
			http.NotFound(w, r)
			return
		}

		if code == 301 {
			http.Redirect(w, r, "/"+page_model.Parent().Permalink()+"/", 301)
			return
		}

		if page_model != nil {
			if cook, err := r.Cookie("UserID"); err == nil {
				uid, _ := strconv.Atoi(cook.Value)
				CurrentUser, _ = models.GetUserByID(uid)
			}

			render(page_model, page_model.GetTemplate(), w)
		}
	})
}