templater.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 main
  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. )
  36. type Templater struct {
  37. indexTemplate *template.Template
  38. mailListTemplate *template.Template
  39. }
  40. type Index struct {
  41. Folders template.HTML
  42. MailList template.HTML
  43. Version template.HTML
  44. }
  45. func NewTemplater(templatesPath string) (t *Templater) {
  46. t = nil
  47. index, err := parseTemplate(templatesPath + "/" + IndexTemplateName)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. maillist, err := parseTemplate(templatesPath + "/" + MailListTemplateName)
  52. if err != nil {
  53. log.Fatal(err)
  54. }
  55. t = &Templater{
  56. indexTemplate: index,
  57. mailListTemplate: maillist,
  58. }
  59. return
  60. }
  61. func parseTemplate(path string) (*template.Template, error) {
  62. content, err := ioutil.ReadFile(path)
  63. if err != nil {
  64. log.Fatal(err)
  65. }
  66. return template.New("Index").Parse(string(content))
  67. }
  68. func (t *Templater) ExecuteIndex(content interface{}) string {
  69. return executeTemplateCommon(t.indexTemplate, content)
  70. }
  71. func (t *Templater) ExecuteMailList(mailList interface{}) string {
  72. return executeTemplateCommon(t.mailListTemplate, mailList)
  73. }
  74. func executeTemplateCommon(t *template.Template, values interface{}) string {
  75. buffer := &bytes.Buffer{}
  76. err := t.Execute(buffer, values)
  77. if err != nil {
  78. log.Printf("Could not execute template: %s", err)
  79. }
  80. return buffer.String()
  81. }