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.

101 lines
2.0 KiB

  1. package Webserver
  2. import (
  3. "log"
  4. "net/http"
  5. "strconv"
  6. "PersonalWebsite/Database"
  7. "github.com/gorilla/mux"
  8. )
  9. func ViewPost(w http.ResponseWriter, r *http.Request) {
  10. var (
  11. v = make(map[string]interface{})
  12. urlParams map[string]string
  13. post Database.Post
  14. e error
  15. )
  16. urlParams = mux.Vars(r)
  17. post, e = Database.GetPostById(urlParams["id"])
  18. if e != nil {
  19. // TODO: Forward 404
  20. log.Println("Could not get post")
  21. http.Error(w, "Error", http.StatusInternalServerError)
  22. return
  23. }
  24. v["Subject"] = post.Subject
  25. v["PageView"] = "post.gohtml"
  26. v["Post"] = post
  27. ServeTemplate(w, r, "html/index.gohtml", v)
  28. }
  29. func ViewPostList(w http.ResponseWriter, r *http.Request, subject string) {
  30. var (
  31. limit int = 2
  32. v = make(map[string]interface{})
  33. posts []Database.Post
  34. postsCount int64
  35. pageCount int
  36. keys []string
  37. page int
  38. offset int
  39. x, y int
  40. ok bool
  41. e error
  42. )
  43. keys, ok = r.URL.Query()["page"]
  44. if !ok || len(keys[0]) < 1 {
  45. page = 0
  46. } else {
  47. page, e = strconv.Atoi(keys[0])
  48. if e != nil {
  49. // TODO: Handle this
  50. http.Error(w, "Error", http.StatusInternalServerError)
  51. return
  52. }
  53. }
  54. offset = limit * page
  55. posts, e = Database.GetPostsListBySubject(limit, offset, subject)
  56. if e != nil {
  57. // TODO: Handle this
  58. http.Error(w, "Error", http.StatusInternalServerError)
  59. return
  60. }
  61. postsCount, e = Database.GetPostCountBySubject(subject)
  62. if e != nil {
  63. // TODO: Handle this
  64. http.Error(w, "Error", http.StatusInternalServerError)
  65. return
  66. }
  67. x, y = int(postsCount), limit
  68. pageCount = (x + y - 1) / y
  69. v["PageView"] = "post-list.gohtml"
  70. v["Subject"] = subject
  71. v["Posts"] = posts
  72. v["Page"] = page
  73. v["PageCount"] = pageCount
  74. ServeTemplate(w, r, "html/index.gohtml", v)
  75. }
  76. func ViewPostListProgramming(w http.ResponseWriter, r *http.Request) {
  77. ViewPostList(w, r, "Programming")
  78. }
  79. func ViewPostListPentesting(w http.ResponseWriter, r *http.Request) {
  80. ViewPostList(w, r, "Pentesting")
  81. }