templater.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. )
  39. type Templater struct {
  40. indexTemplate *template.Template
  41. mailListTemplate *template.Template
  42. detailsTemplate *template.Template
  43. errorTemplate *template.Template
  44. loginTemplate *template.Template
  45. }
  46. type IndexTemplateData struct {
  47. Folders template.HTML
  48. MailList template.HTML
  49. Version template.HTML
  50. }
  51. type ErrorTemplateData struct {
  52. Code int
  53. Text string
  54. Version string
  55. }
  56. type LoginTemplateData struct {
  57. Version string
  58. }
  59. func NewTemplater(templatesPath string) (t *Templater) {
  60. t = nil
  61. index, err := parseTemplate(templatesPath + "/" + IndexTemplateName)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. maillist, err := parseTemplate(templatesPath + "/" + MailListTemplateName)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. details, err := parseTemplate(templatesPath + "/" + DetailsTemplateName)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. errors, err := parseTemplate(templatesPath + "/" + ErrorTemplateName)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. login, err := parseTemplate(templatesPath + "/" + LoginTemplateName)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. t = &Templater{
  82. indexTemplate: index,
  83. mailListTemplate: maillist,
  84. detailsTemplate: details,
  85. errorTemplate: errors,
  86. loginTemplate: login,
  87. }
  88. return
  89. }
  90. func parseTemplate(path string) (*template.Template, error) {
  91. content, err := ioutil.ReadFile(path)
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. return template.New("Index").Parse(string(content))
  96. }
  97. func (t *Templater) ExecuteIndex(data interface{}) string {
  98. return executeTemplateCommon(t.indexTemplate, data)
  99. }
  100. func (t *Templater) ExecuteMailList(data interface{}) string {
  101. return executeTemplateCommon(t.mailListTemplate, data)
  102. }
  103. func (t *Templater) ExecuteDetails(data interface{}) string {
  104. return executeTemplateCommon(t.detailsTemplate, data)
  105. }
  106. func (t *Templater) ExecuteError(data interface{}) string {
  107. return executeTemplateCommon(t.errorTemplate, data)
  108. }
  109. func (t *Templater) ExecuteLogin(data interface{}) string {
  110. return executeTemplateCommon(t.loginTemplate, data)
  111. }
  112. func executeTemplateCommon(t *template.Template, values interface{}) string {
  113. buffer := &bytes.Buffer{}
  114. err := t.Execute(buffer, values)
  115. if err != nil {
  116. log.Printf("Could not execute template: %s", err)
  117. }
  118. return buffer.String()
  119. }