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.

77 lines
1.5 KiB

  1. package Webserver
  2. import (
  3. "net/http"
  4. "path"
  5. "text/template"
  6. "PersonalWebsite/Database"
  7. "PersonalWebsite/Helper"
  8. )
  9. type Unauthenticated struct {
  10. FlashMsg string
  11. }
  12. var (
  13. partials = []string{
  14. "html/header.gohtml",
  15. "html/sidebar.gohtml",
  16. "html/index-intro.gohtml",
  17. "html/index-post-list.gohtml",
  18. "html/post-list.gohtml",
  19. "html/post.gohtml",
  20. }
  21. )
  22. func ServeTemplate(w http.ResponseWriter, r *http.Request, mainFile string, v map[string]interface{}) {
  23. var (
  24. tpl *template.Template
  25. files []string
  26. e error
  27. )
  28. if _, ok := v["Subject"]; !ok {
  29. v["Subject"] = ""
  30. }
  31. v["LastUpdatedAt"], e = Database.GetLastUpdatedAt()
  32. if e != nil {
  33. v["LastUpdatedAt"] = " - "
  34. }
  35. v["SidebarLinks"], e = Database.GetAllSidebarLinks()
  36. if e != nil {
  37. // TODO: Handle
  38. panic(e)
  39. }
  40. files = []string{webRootJoin(mainFile)}
  41. for _, p := range partials {
  42. files = append(files, webRootJoin(p))
  43. }
  44. tpl, e = template.New(path.Base(files[0])).Funcs(
  45. template.FuncMap{
  46. "FormatTimestamp": Helper.FormatTimestamp,
  47. "MinusInt64": Helper.MinusInt64,
  48. "MinusInt": Helper.MinusInt,
  49. "Minus": Helper.MinusInt,
  50. "PlusInt64": Helper.PlusInt64,
  51. "PlusInt": Helper.PlusInt,
  52. "Iterate": Helper.Iterate,
  53. "StrToLower": Helper.StrToLower,
  54. },
  55. ).ParseFiles(files...)
  56. if e != nil {
  57. // TODO: Handle this
  58. panic(e)
  59. }
  60. w.Header().Set("Content-type", "text/html")
  61. e = tpl.Execute(w, v)
  62. if e != nil {
  63. // TODO: Handle this
  64. panic(e)
  65. }
  66. }