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.

108 lines
2.0 KiB

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