parse.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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(), " ") + ";\n"
  53. l.parseLocation(location, deep+1)
  54. }
  55. }
  56. func (d *NgxDirective) ParseDirective(directive gonginx.IDirective, deep int) {
  57. d.parseDirective(directive, deep)
  58. }
  59. func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) {
  60. if directive.GetBlock() != nil {
  61. d.Params += directive.GetName() + " "
  62. d.Directive = ""
  63. }
  64. d.Params += strings.Join(directive.GetParameters(), " ")
  65. if directive.GetBlock() != nil {
  66. d.Params += " {\n"
  67. for _, location := range directive.GetBlock().GetDirectives() {
  68. if len(location.GetComment()) > 0 {
  69. for _, c := range location.GetComment() {
  70. d.Params += strings.Repeat("\t", deep) + c + "\n"
  71. }
  72. }
  73. d.Params += strings.Repeat("\t", deep+1) + location.GetName() + " " +
  74. strings.Join(location.GetParameters(), " ") + ";\n"
  75. // d.parseDirective(location, deep+1)
  76. if location.GetBlock() == nil {
  77. continue
  78. }
  79. for _, v := range location.GetBlock().GetDirectives() {
  80. d.parseDirective(v, deep+1)
  81. }
  82. }
  83. d.Params += "}\n"
  84. return
  85. }
  86. }
  87. func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) {
  88. if directive.GetBlock() == nil {
  89. return
  90. }
  91. for _, us := range directive.GetBlock().GetDirectives() {
  92. d := &NgxDirective{
  93. Directive: us.GetName(),
  94. Params: strings.Join(us.GetParameters(), " "),
  95. Comments: buildComment(us.GetComment()),
  96. }
  97. u.Directives = append(u.Directives, d)
  98. }
  99. }
  100. func (c *NgxConfig) parseCustom(directive gonginx.IDirective) {
  101. if directive.GetBlock() == nil {
  102. return
  103. }
  104. c.Custom += "{\n"
  105. for _, v := range directive.GetBlock().GetDirectives() {
  106. c.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  107. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + ";\n"
  108. }
  109. c.Custom += "}\n"
  110. }
  111. func buildComment(c []string) string {
  112. return strings.ReplaceAll(strings.Join(c, "\n"), "#", "")
  113. }
  114. func parse(block gonginx.IBlock, ngxConfig *NgxConfig) {
  115. if block == nil {
  116. return
  117. }
  118. for _, v := range block.GetDirectives() {
  119. comments := buildComment(v.GetComment())
  120. switch v.GetName() {
  121. case Server:
  122. server := NewNgxServer()
  123. server.Comments = comments
  124. server.parseServer(v)
  125. ngxConfig.Servers = append(ngxConfig.Servers, server)
  126. case Upstream:
  127. upstream := &NgxUpstream{}
  128. upstream.Comments = comments
  129. upstream.parseUpstream(v)
  130. ngxConfig.Upstreams = append(ngxConfig.Upstreams, upstream)
  131. default:
  132. ngxConfig.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  133. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + "\n"
  134. ngxConfig.parseCustom(v)
  135. }
  136. }
  137. ngxConfig.Custom = FmtCode(ngxConfig.Custom)
  138. }
  139. func ParseNgxConfigByContent(content string) (ngxConfig *NgxConfig) {
  140. p := parser.NewStringParser(content)
  141. c := p.Parse()
  142. ngxConfig = NewNgxConfig("")
  143. ngxConfig.c = c
  144. parse(c.Block, ngxConfig)
  145. return
  146. }
  147. func ParseNgxConfig(filename string) (ngxConfig *NgxConfig, err error) {
  148. p, err := parser.NewParser(filename)
  149. if err != nil {
  150. return nil, errors.Wrap(err, "error ParseNgxConfig")
  151. }
  152. c := p.Parse()
  153. ngxConfig = NewNgxConfig(filename)
  154. ngxConfig.c = c
  155. parse(c.Block, ngxConfig)
  156. return
  157. }