type.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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{
  55. Locations: make([]*NgxLocation, 0),
  56. Directives: make([]*NgxDirective, 0),
  57. commentQueue: &CommentQueue{linkedlistqueue.New()},
  58. }
  59. }
  60. func NewNgxConfig(filename string) *NgxConfig {
  61. return &NgxConfig{
  62. FileName: filename,
  63. commentQueue: &CommentQueue{linkedlistqueue.New()},
  64. Upstreams: make([]*NgxUpstream, 0),
  65. }
  66. }