type.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package nginx
  2. import (
  3. "github.com/emirpasic/gods/queues/linkedlistqueue"
  4. "strings"
  5. )
  6. type CommentQueue struct {
  7. *linkedlistqueue.Queue
  8. }
  9. type NgxConfig struct {
  10. FileName string `json:"file_name"`
  11. Upstreams []*NgxUpstream `json:"upstreams"`
  12. Servers []*NgxServer `json:"servers"`
  13. Custom string `json:"custom"`
  14. commentQueue *CommentQueue
  15. }
  16. type NgxServer struct {
  17. Directives []*NgxDirective `json:"directives"`
  18. Locations []*NgxLocation `json:"locations"`
  19. Comments string `json:"comments"`
  20. commentQueue *CommentQueue
  21. }
  22. type NgxUpstream struct {
  23. Name string `json:"name"`
  24. Directives []*NgxDirective `json:"directives"`
  25. Comments string `json:"comments"`
  26. }
  27. type NgxDirective struct {
  28. Directive string `json:"directive"`
  29. Params string `json:"params"`
  30. Comments string `json:"comments"`
  31. }
  32. type NgxLocation struct {
  33. Path string `json:"path"`
  34. Content string `json:"content"`
  35. Comments string `json:"comments"`
  36. }
  37. func (c *CommentQueue) DequeueAllComments() (comments string) {
  38. for !c.Empty() {
  39. comment, ok := c.Dequeue()
  40. if ok {
  41. comments += strings.TrimSpace(comment.(string)) + "\n"
  42. }
  43. }
  44. return
  45. }
  46. func (d *NgxDirective) Orig() string {
  47. return d.Directive + " " + d.Params
  48. }
  49. func (d *NgxDirective) TrimParams() {
  50. d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";")
  51. return
  52. }
  53. func NewNgxServer() *NgxServer {
  54. return &NgxServer{commentQueue: &CommentQueue{linkedlistqueue.New()}}
  55. }
  56. func NewNgxConfig(filename string) *NgxConfig {
  57. return &NgxConfig{FileName: filename, commentQueue: &CommentQueue{linkedlistqueue.New()}}
  58. }