templater.go 3.8 KB

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