template.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package service
  2. import (
  3. "bufio"
  4. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  5. "github.com/0xJacky/Nginx-UI/template"
  6. "github.com/pkg/errors"
  7. "github.com/tufanbarisyildirim/gonginx/parser"
  8. "io"
  9. "path/filepath"
  10. "regexp"
  11. "strings"
  12. )
  13. type ConfigInfoItem struct {
  14. Name string `json:"name"`
  15. Description map[string]string `json:"description"`
  16. Author string `json:"author"`
  17. Filename string `json:"filename"`
  18. }
  19. func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
  20. configListItem = ConfigInfoItem{
  21. Description: make(map[string]string),
  22. Filename: name,
  23. }
  24. file, _ := template.DistFS.Open(filepath.Join(path, name))
  25. defer file.Close()
  26. r := bufio.NewReader(file)
  27. bytes, _, err := r.ReadLine()
  28. if err == io.EOF {
  29. return
  30. }
  31. line := strings.TrimSpace(string(bytes))
  32. if line != "# Nginx UI Template Start" {
  33. return
  34. }
  35. var content string
  36. for {
  37. bytes, _, err = r.ReadLine()
  38. if err == io.EOF {
  39. break
  40. }
  41. line = strings.TrimSpace(string(bytes))
  42. if line == "# Nginx UI Template End" {
  43. break
  44. }
  45. content += line + "\n"
  46. }
  47. re := regexp.MustCompile(`# (\S+): (.*)`)
  48. matches := re.FindAllStringSubmatch(content, -1)
  49. for _, match := range matches {
  50. if len(match) < 3 {
  51. continue
  52. }
  53. key := match[1]
  54. switch {
  55. case key == "Name":
  56. configListItem.Name = match[2]
  57. case key == "Author":
  58. configListItem.Author = match[2]
  59. case strings.Contains(key, "Description"):
  60. re = regexp.MustCompile(`(\w+)\[(\w+)\]`)
  61. matches = re.FindAllStringSubmatch(key, -1)
  62. for _, m := range matches {
  63. if len(m) < 3 {
  64. continue
  65. }
  66. // lang => description
  67. configListItem.Description[m[2]] = match[2]
  68. }
  69. }
  70. }
  71. return
  72. }
  73. type ConfigDetail struct {
  74. Custom string `json:"custom"`
  75. nginx.NgxServer
  76. }
  77. func ParseTemplate(path, name string) (c ConfigDetail, err error) {
  78. file, err := template.DistFS.Open(filepath.Join(path, name))
  79. if err != nil {
  80. err = errors.Wrap(err, "error tokenized template")
  81. return
  82. }
  83. defer file.Close()
  84. r := bufio.NewReader(file)
  85. var flag bool
  86. custom := ""
  87. content := ""
  88. for {
  89. bytes, _, err := r.ReadLine()
  90. if err == io.EOF {
  91. break
  92. }
  93. orig := string(bytes)
  94. line := strings.TrimSpace(orig)
  95. switch {
  96. case line == "# Nginx UI Custom Start":
  97. flag = true
  98. case line == "# Nginx UI Custom End":
  99. flag = false
  100. case flag == true:
  101. custom += orig + "\n"
  102. case flag == false:
  103. content += orig + "\n"
  104. }
  105. }
  106. p := parser.NewStringParser(content)
  107. config := p.Parse()
  108. c.Custom = custom
  109. for _, d := range config.GetDirectives() {
  110. switch d.GetName() {
  111. case nginx.Location:
  112. l := &nginx.NgxLocation{
  113. Path: strings.Join(d.GetParameters(), " "),
  114. }
  115. l.ParseLocation(d, 0)
  116. c.NgxServer.Locations = append(c.NgxServer.Locations, l)
  117. default:
  118. dir := &nginx.NgxDirective{
  119. Directive: d.GetName(),
  120. }
  121. dir.ParseDirective(d, 0)
  122. c.NgxServer.Directives = append(c.NgxServer.Directives, dir)
  123. }
  124. }
  125. return
  126. }
  127. func GetTemplateList(path string) (configList []ConfigInfoItem, err error) {
  128. configs, err := template.DistFS.ReadDir(path)
  129. if err != nil {
  130. err = errors.Wrap(err, "error get template list")
  131. return
  132. }
  133. for _, config := range configs {
  134. configList = append(configList, GetTemplateInfo(path, config.Name()))
  135. }
  136. return
  137. }