parse.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if directive.GetBlock() == nil {
  15. return
  16. }
  17. for _, d := range directive.GetBlock().GetDirectives() {
  18. switch d.GetName() {
  19. case Location:
  20. location := &NgxLocation{
  21. Path: strings.Join(d.GetParameters(), " "),
  22. Comments: buildComment(d.GetComment()),
  23. }
  24. location.parseLocation(d, 0)
  25. s.Locations = append(s.Locations, location)
  26. default:
  27. dir := &NgxDirective{
  28. Directive: d.GetName(),
  29. Comments: buildComment(d.GetComment()),
  30. }
  31. dir.parseDirective(d, 0)
  32. s.Directives = append(s.Directives, dir)
  33. }
  34. }
  35. }
  36. func (l *NgxLocation) parseLocation(directive gonginx.IDirective, deep int) {
  37. if directive.GetBlock() == nil {
  38. return
  39. }
  40. for _, location := range directive.GetBlock().GetDirectives() {
  41. if len(location.GetComment()) > 0 {
  42. for _, c := range location.GetComment() {
  43. l.Content += strings.Repeat("\t", deep) + c + "\n"
  44. }
  45. }
  46. l.Content += strings.Repeat("\t", deep) + location.GetName() + " " + strings.Join(location.GetParameters(), " ") + ";\n"
  47. l.parseLocation(location, deep+1)
  48. }
  49. }
  50. func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) {
  51. if directive.GetBlock() != nil {
  52. d.Params += directive.GetName() + " "
  53. d.Directive = ""
  54. }
  55. d.Params += strings.Join(directive.GetParameters(), " ")
  56. if directive.GetBlock() != nil {
  57. d.Params += " {\n"
  58. for _, location := range directive.GetBlock().GetDirectives() {
  59. if len(location.GetComment()) > 0 {
  60. for _, c := range location.GetComment() {
  61. d.Params += strings.Repeat("\t", deep) + c + "\n"
  62. }
  63. }
  64. d.Params += strings.Repeat("\t", deep+1) + location.GetName() + " " +
  65. strings.Join(location.GetParameters(), " ") + ";\n"
  66. // d.parseDirective(location, deep+1)
  67. if location.GetBlock() == nil {
  68. continue
  69. }
  70. for _, v := range location.GetBlock().GetDirectives() {
  71. d.parseDirective(v, deep+1)
  72. }
  73. }
  74. d.Params += "}\n"
  75. return
  76. }
  77. }
  78. func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) {
  79. if directive.GetBlock() == nil {
  80. return
  81. }
  82. for _, us := range directive.GetBlock().GetDirectives() {
  83. d := &NgxDirective{
  84. Directive: us.GetName(),
  85. Params: strings.Join(us.GetParameters(), " "),
  86. Comments: buildComment(us.GetComment()),
  87. }
  88. u.Directives = append(u.Directives, d)
  89. }
  90. }
  91. func (c *NgxConfig) parseCustom(directive gonginx.IDirective) {
  92. if directive.GetBlock() == nil {
  93. return
  94. }
  95. c.Custom += "{\n"
  96. for _, v := range directive.GetBlock().GetDirectives() {
  97. c.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  98. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + ";\n"
  99. }
  100. c.Custom += "}\n"
  101. }
  102. func buildComment(c []string) string {
  103. return strings.ReplaceAll(strings.Join(c, "\n"), "#", "")
  104. }
  105. func parse(block gonginx.IBlock, ngxConfig *NgxConfig) {
  106. if block == nil {
  107. return
  108. }
  109. for _, v := range block.GetDirectives() {
  110. comments := buildComment(v.GetComment())
  111. switch v.GetName() {
  112. case Server:
  113. server := NewNgxServer()
  114. server.Comments = comments
  115. server.parseServer(v)
  116. ngxConfig.Servers = append(ngxConfig.Servers, server)
  117. case Upstream:
  118. upstream := &NgxUpstream{}
  119. upstream.Comments = comments
  120. upstream.parseUpstream(v)
  121. ngxConfig.Upstreams = append(ngxConfig.Upstreams, upstream)
  122. default:
  123. ngxConfig.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
  124. v.GetName() + " " + strings.Join(v.GetParameters(), " ") + "\n"
  125. ngxConfig.parseCustom(v)
  126. }
  127. }
  128. }
  129. func ParseNgxConfigByContent(content string) (ngxConfig *NgxConfig) {
  130. p := parser.NewStringParser(content)
  131. c := p.Parse()
  132. ngxConfig = NewNgxConfig("")
  133. ngxConfig.c = c
  134. parse(c.Block, ngxConfig)
  135. return
  136. }
  137. func ParseNgxConfig(filename string) (ngxConfig *NgxConfig, err error) {
  138. p, err := parser.NewParser(filename)
  139. if err != nil {
  140. return nil, errors.Wrap(err, "error ParseNgxConfig")
  141. }
  142. c := p.Parse()
  143. ngxConfig = NewNgxConfig(filename)
  144. ngxConfig.c = c
  145. parse(c.Block, ngxConfig)
  146. return
  147. }