templater.go 5.9 KB

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