type.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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:"ngx_server"`
  13. commentQueue *CommentQueue
  14. }
  15. type NgxServer struct {
  16. ServerName string `json:"server_name"`
  17. Directives NgxDirectives `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 NgxDirectives `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 NgxDirectives map[string][]NgxDirective
  33. type NgxLocation struct {
  34. Path string `json:"path"`
  35. Content string `json:"content"`
  36. Comments string `json:"comments"`
  37. }
  38. func (c *CommentQueue) DequeueAllComments() (comments string) {
  39. for !c.Empty() {
  40. comment, ok := c.Dequeue()
  41. if ok {
  42. comments += strings.TrimSpace(comment.(string)) + "\n"
  43. }
  44. }
  45. return
  46. }
  47. func (d *NgxDirective) Orig() string {
  48. return d.Directive + " " + d.Params
  49. }
  50. func (d *NgxDirective) TrimParams() {
  51. d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";")
  52. return
  53. }
  54. func NewNgxServer() *NgxServer {
  55. return &NgxServer{commentQueue: &CommentQueue{linkedlistqueue.New()}}
  56. }
  57. func NewNgxConfig(filename string) *NgxConfig {
  58. return &NgxConfig{FileName: filename, commentQueue: &CommentQueue{linkedlistqueue.New()}}
  59. }