templater.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package web
  26. import (
  27. "bytes"
  28. template "html/template"
  29. ioutil "io/ioutil"
  30. "log"
  31. )
  32. const (
  33. IndexTemplateName = "index.html"
  34. MailListTemplateName = "maillist.html"
  35. DetailsTemplateName = "details.html"
  36. ErrorTemplateName = "error.html"
  37. LoginTemplateName = "login.html"
  38. StatusLineTemplateName = "statusline.html"
  39. )
  40. type Templater struct {
  41. indexTemplate *template.Template
  42. mailListTemplate *template.Template
  43. detailsTemplate *template.Template
  44. errorTemplate *template.Template
  45. loginTemplate *template.Template
  46. statusLineTemplate *template.Template
  47. }
  48. type IndexTemplateData struct {
  49. Folders template.HTML
  50. MailList template.HTML
  51. Version template.HTML
  52. }
  53. type ErrorTemplateData struct {
  54. Code int
  55. Text string
  56. Version string
  57. }
  58. type LoginTemplateData struct {
  59. Version string
  60. }
  61. type StatusLineTemplateData struct {
  62. Name string
  63. Read int
  64. Unread int
  65. }
  66. func NewTemplater(templatesPath string) (t *Templater) {
  67. t = nil
  68. index, err := parseTemplate(templatesPath + "/" + IndexTemplateName)
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. maillist, err := parseTemplate(templatesPath + "/" + MailListTemplateName)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. details, err := parseTemplate(templatesPath + "/" + DetailsTemplateName)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. errors, err := parseTemplate(templatesPath + "/" + ErrorTemplateName)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. login, err := parseTemplate(templatesPath + "/" + LoginTemplateName)
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. statusLine, err := parseTemplate(templatesPath + "/" + StatusLineTemplateName)
  89. if err != nil {
  90. log.Fatal(err)
  91. }
  92. t = &Templater{
  93. indexTemplate: index,
  94. mailListTemplate: maillist,
  95. detailsTemplate: details,
  96. errorTemplate: errors,
  97. loginTemplate: login,
  98. statusLineTemplate: statusLine,
  99. }
  100. return
  101. }
  102. func parseTemplate(path string) (*template.Template, error) {
  103. content, err := ioutil.ReadFile(path)
  104. if err != nil {
  105. log.Fatal(err)
  106. }
  107. return template.New("Index").Parse(string(content))
  108. }
  109. func (t *Templater) ExecuteIndex(data interface{}) string {
  110. return executeTemplateCommon(t.indexTemplate, data)
  111. }
  112. func (t *Templater) ExecuteMailList(data interface{}) string {
  113. return executeTemplateCommon(t.mailListTemplate, data)
  114. }
  115. func (t *Templater) ExecuteDetails(data interface{}) string {
  116. return executeTemplateCommon(t.detailsTemplate, data)
  117. }
  118. func (t *Templater) ExecuteError(data interface{}) string {
  119. return executeTemplateCommon(t.errorTemplate, data)
  120. }
  121. func (t *Templater) ExecuteLogin(data interface{}) string {
  122. return executeTemplateCommon(t.loginTemplate, data)
  123. }
  124. func (t *Templater) ExecuteStatusLine(data interface{}) string {
  125. return executeTemplateCommon(t.statusLineTemplate, data)
  126. }
  127. func executeTemplateCommon(t *template.Template, values interface{}) string {
  128. buffer := &bytes.Buffer{}
  129. err := t.Execute(buffer, values)
  130. if err != nil {
  131. log.Printf("Could not execute template: %s", err)
  132. }
  133. return buffer.String()
  134. }