parse.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package nginx
  2. import (
  3. "github.com/pkg/errors"
  4. "github.com/tufanbarisyildirim/gonginx"
  5. "github.com/tufanbarisyildirim/gonginx/parser"
  6. "strings"
  7. )
  8. const (
  9. Server = "server"
  10. Location = "location"
  11. Upstream = "upstream"
  12. )
  13. func (s *NgxServer) ParseServer(directive gonginx.IDirective) {
  14. s.parseServer(directive)
  15. }
  16. func (s *NgxServer) parseServer(directive gonginx.IDirective) {
  17. if directive.GetBlock() == nil {
  18. return
  19. }
  20. for _, d := range directive.GetBlock().GetDirectives() {
  21. switch d.GetName() {
  22. case Location:
  23. location := &NgxLocation{
  24. Path: strings.Join(d.GetParameters(), " "),
  25. Comments: buildComment(d.GetComment()),
  26. }
  27. location.parseLocation(d, 0)
  28. s.Locations = append(s.Locations, location)
  29. default:
  30. dir := &NgxDirective{
  31. Directive: d.GetName(),
  32. Comments: buildComment(d.GetComment()),
  33. }
  34. dir.parseDirective(d, 0)
  35. s.Directives = append(s.Directives, dir)
  36. }
  37. }
  38. }
  39. func (l *NgxLocation) ParseLocation(directive gonginx.IDirective, deep int) {
  40. l.parseLocation(directive, deep)
  41. }
  42. func (l *NgxLocation) parseLocation(directive gonginx.IDirective, deep int) {
  43. if directive.GetBlock() == nil {
  44. return
  45. }
  46. for _, location := range directive.GetBlock().GetDirectives() {
  47. if len(location.GetComment()) > 0 {
  48. for _, c := range location.GetComment() {
  49. l.Content += strings.Repeat("\t", deep) + c + "\n"
  50. }
  51. }
  52. l.Content += strings.Repeat("\t", deep) + location.GetName() + " " + strings.Join(location.GetParameters(), " ")
  53. if location.GetBlock() != nil && location.GetBlock().GetDirectives() != nil {
  54. l.Content += " { \n"
  55. l.parseLocation(location, deep+1)
  56. l.Content += " } \n"
  57. } else {
  58. l.Content += ";\n"
  59. }
  60. }
  61. }
  62. func (d *NgxDirective) ParseDirective(directive gonginx.IDirective, deep int) {
  63. d.parseDirective(directive, deep)
  64. }
  65. func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) {
  66. if directive.GetBlock() != nil {
  67. d.Params += directive.GetName() + " "
  68. d.Directive = ""
  69. }
  70. d.Params += strings.Join(directive.GetParameters(), " ")
  71. if directive.GetBlock() != nil {
  72. d.Params += " {\n"
  73. for _, location := range directive.GetBlock().GetDirectives() {
  74. if len(location.GetComment()) > 0 {
  75. for _, c := range location.GetComment() {
  76. d.Params += strings.Repeat("\t", deep) + c + "\n"
  77. }
  78. }
  79. d.Params += strings.Repeat("\t", deep+1) + location.GetName() + " " +
  80. strings.Join(location.GetParameters(), " ") + ";\n"
  81. // d.parseDirective(location, deep+1)
  82. if location.GetBlock() == nil {
  83. continue
  84. }
  85. for _, v := range location.GetBlock().GetDirectives() {
  86. d.parseDirective(v, deep+1)
  87. }
  88. }
  89. d.Params += "}\n"
  90. return
  91. }
  92. }
  93. func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) {
  94. if directive.GetBlock() == nil {
  95. return
  96. }
  97. for _, us := range directive.GetBlock().GetDirectives() {
  98. d := &NgxDirective{
  99. Directive: us.GetName(),
  100. Params: strings.Join(us.GetParameters(), " "),
  101. Comments: buildComment(us.GetComment()),
  102. }
  103. u.Directives = append(u.Directives, d)
  104. }
  105. }
  106. func (c *NgxConfig) parseCustom(directive gonginx.IDirective) {
  107. if directive.GetBlock() == nil {
  108. return
  109. }
  110. c.Custom += "{\n"
  111. for _, v := range directive.GetBlock().GetDirectives() {
  112. c.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  113. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + ";\n"
  114. }
  115. c.Custom += "}\n"
  116. }
  117. func buildComment(c []string) string {
  118. return strings.ReplaceAll(strings.Join(c, "\n"), "#", "")
  119. }
  120. func parse(block gonginx.IBlock, ngxConfig *NgxConfig) (err error) {
  121. if block == nil {
  122. err = errors.New("block is nil")
  123. return
  124. }
  125. for _, v := range block.GetDirectives() {
  126. comments := buildComment(v.GetComment())
  127. switch v.GetName() {
  128. case Server:
  129. server := NewNgxServer()
  130. server.Comments = comments
  131. server.parseServer(v)
  132. ngxConfig.Servers = append(ngxConfig.Servers, server)
  133. case Upstream:
  134. upstream := &NgxUpstream{
  135. Name: strings.Join(v.GetParameters(), " "),
  136. }
  137. upstream.Comments = comments
  138. upstream.parseUpstream(v)
  139. ngxConfig.Upstreams = append(ngxConfig.Upstreams, upstream)
  140. default:
  141. ngxConfig.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  142. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + "\n"
  143. ngxConfig.parseCustom(v)
  144. }
  145. }
  146. custom, err := FmtCode(ngxConfig.Custom)
  147. if err != nil {
  148. return
  149. }
  150. ngxConfig.Custom = custom
  151. return
  152. }
  153. func ParseNgxConfigByContent(content string) (ngxConfig *NgxConfig, err error) {
  154. p := parser.NewStringParser(content, parser.WithSkipValidDirectivesErr())
  155. c, err := p.Parse()
  156. if err != nil {
  157. return
  158. }
  159. ngxConfig = NewNgxConfig("")
  160. ngxConfig.c = c
  161. err = parse(c.Block, ngxConfig)
  162. return
  163. }
  164. func ParseNgxConfig(filename string) (ngxConfig *NgxConfig, err error) {
  165. p, err := parser.NewParser(filename, parser.WithSkipValidDirectivesErr())
  166. if err != nil {
  167. return nil, errors.Wrap(err, "error ParseNgxConfig")
  168. }
  169. c, err := p.Parse()
  170. if err != nil {
  171. return
  172. }
  173. ngxConfig = NewNgxConfig(filename)
  174. ngxConfig.c = c
  175. err = parse(c.Block, ngxConfig)
  176. return
  177. }