You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
892 B

3 years ago
3 years ago
3 years ago
  1. // Copyright 2018 Lars Hoogestraat
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package handler
  5. import (
  6. "net/http"
  7. "strconv"
  8. "git.hoogi.eu/snafu/go-blog/logger"
  9. "github.com/gorilla/mux"
  10. )
  11. func getVar(r *http.Request, key string) string {
  12. v := mux.Vars(r)[key]
  13. if len(v) > 0 {
  14. return v
  15. }
  16. return r.Header.Get("X-Unit-Testing-Value-" + key)
  17. }
  18. func parseInt(v string) (int, error) {
  19. i, err := strconv.Atoi(v)
  20. if err != nil {
  21. return -1, err
  22. }
  23. return i, nil
  24. }
  25. func getPageParam(r *http.Request) int {
  26. var page = 1
  27. if len(getVar(r, "page")) > 0 {
  28. i, err := strconv.Atoi(getVar(r, "page"))
  29. if err != nil {
  30. logger.Log.Errorf("could not parse page number %v\n", err)
  31. return 1
  32. }
  33. page = i
  34. }
  35. return page
  36. }
  37. func convertCheckbox(r *http.Request, name string) bool {
  38. return r.FormValue(name) == "on"
  39. }