templater.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. FoldersTemplateName = "folders.html"
  40. MailNewTemplateName = "mailnew.html"
  41. MailTemplateName = "mailTemplate.eml"
  42. SignupTemplateName = "signup.html"
  43. RegisterTemplateName = "register.html"
  44. SettingsTemplateName = "settings.html"
  45. )
  46. type Templater struct {
  47. indexTemplate *template.Template
  48. mailListTemplate *template.Template
  49. detailsTemplate *template.Template
  50. errorTemplate *template.Template
  51. loginTemplate *template.Template
  52. signupTemplate *template.Template
  53. registerTemplate *template.Template
  54. statusLineTemplate *template.Template
  55. foldersTemaplate *template.Template
  56. mailNewTemplate *template.Template
  57. mailTemplate *template.Template
  58. settingsTemplate *template.Template
  59. }
  60. func NewTemplater(templatesPath string) (t *Templater) {
  61. t = nil
  62. index, err := parseTemplate(templatesPath + "/" + IndexTemplateName)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. maillist, err := parseTemplate(templatesPath + "/" + MailListTemplateName)
  67. if err != nil {
  68. log.Fatal(err)
  69. }
  70. details, err := parseTemplate(templatesPath + "/" + DetailsTemplateName)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. errors, err := parseTemplate(templatesPath + "/" + ErrorTemplateName)
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. login, err := parseTemplate(templatesPath + "/" + LoginTemplateName)
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. statusLine, err := parseTemplate(templatesPath + "/" + StatusLineTemplateName)
  83. if err != nil {
  84. log.Fatal(err)
  85. }
  86. folders, err := parseTemplate(templatesPath + "/" + FoldersTemplateName)
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. mailNew, err := parseTemplate(templatesPath + "/" + MailNewTemplateName)
  91. if err != nil {
  92. log.Fatal(err)
  93. }
  94. mail, err := parseTemplate(templatesPath + "/" + MailTemplateName)
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. signup, err := parseTemplate(templatesPath + "/" + SignupTemplateName)
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. register, err := parseTemplate(templatesPath + "/" + RegisterTemplateName)
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. settings, err := parseTemplate(templatesPath + "/" + SettingsTemplateName)
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. t = &Templater{
  111. indexTemplate: index,
  112. mailListTemplate: maillist,
  113. detailsTemplate: details,
  114. errorTemplate: errors,
  115. loginTemplate: login,
  116. statusLineTemplate: statusLine,
  117. foldersTemaplate: folders,
  118. mailNewTemplate: mailNew,
  119. mailTemplate: mail,
  120. signupTemplate: signup,
  121. registerTemplate: register,
  122. settingsTemplate: settings,
  123. }
  124. return
  125. }
  126. func parseTemplate(path string) (*template.Template, error) {
  127. content, err := ioutil.ReadFile(path)
  128. if err != nil {
  129. log.Fatal(err)
  130. }
  131. return template.New("Index").Parse(string(content))
  132. }
  133. func (t *Templater) ExecuteIndex(data interface{}) string {
  134. return executeTemplateCommon(t.indexTemplate, data)
  135. }
  136. func (t *Templater) ExecuteMailList(data interface{}) string {
  137. return executeTemplateCommon(t.mailListTemplate, data)
  138. }
  139. func (t *Templater) ExecuteDetails(data interface{}) string {
  140. return executeTemplateCommon(t.detailsTemplate, data)
  141. }
  142. func (t *Templater) ExecuteError(data interface{}) string {
  143. return executeTemplateCommon(t.errorTemplate, data)
  144. }
  145. func (t *Templater) ExecuteLogin(data interface{}) string {
  146. return executeTemplateCommon(t.loginTemplate, data)
  147. }
  148. func (t *Templater) ExecuteSignup(data interface{}) string {
  149. return executeTemplateCommon(t.signupTemplate, data)
  150. }
  151. func (t *Templater) ExecuteRegister(data interface{}) string {
  152. return executeTemplateCommon(t.registerTemplate, data)
  153. }
  154. func (t *Templater) ExecuteStatusLine(data interface{}) string {
  155. return executeTemplateCommon(t.statusLineTemplate, data)
  156. }
  157. func (t *Templater) ExecuteFolders(data interface{}) string {
  158. return executeTemplateCommon(t.foldersTemaplate, data)
  159. }
  160. func (t *Templater) ExecuteNewMail(data interface{}) string {
  161. return executeTemplateCommon(t.mailNewTemplate, data)
  162. }
  163. func (t *Templater) ExecuteMail(data interface{}) string {
  164. return executeTemplateCommon(t.mailTemplate, data)
  165. }
  166. func (t *Templater) ExecuteSettings(data interface{}) string {
  167. return executeTemplateCommon(t.settingsTemplate, data)
  168. }
  169. func executeTemplateCommon(t *template.Template, values interface{}) string {
  170. buffer := &bytes.Buffer{}
  171. err := t.Execute(buffer, values)
  172. if err != nil {
  173. log.Printf("Could not execute template: %s", err)
  174. }
  175. return buffer.String()
  176. }