type.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package nginx
  2. import (
  3. "github.com/tufanbarisyildirim/gonginx/config"
  4. "path"
  5. "strings"
  6. )
  7. type NgxConfig struct {
  8. FileName string `json:"file_name"`
  9. Name string `json:"name"`
  10. Upstreams []*NgxUpstream `json:"upstreams"`
  11. Servers []*NgxServer `json:"servers"`
  12. Custom string `json:"custom"`
  13. c *config.Config
  14. }
  15. type NgxServer struct {
  16. Directives []*NgxDirective `json:"directives"`
  17. Locations []*NgxLocation `json:"locations"`
  18. Comments string `json:"comments"`
  19. }
  20. type NgxUpstream struct {
  21. Name string `json:"name"`
  22. Directives []*NgxDirective `json:"directives"`
  23. Comments string `json:"comments"`
  24. }
  25. type NgxDirective struct {
  26. Directive string `json:"directive"`
  27. Params string `json:"params"`
  28. Comments string `json:"comments"`
  29. }
  30. type NgxLocation struct {
  31. Path string `json:"path"`
  32. Content string `json:"content"`
  33. Comments string `json:"comments"`
  34. }
  35. func (d *NgxDirective) Orig() string {
  36. return d.Directive + " " + d.Params
  37. }
  38. func (d *NgxDirective) TrimParams() {
  39. d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";")
  40. return
  41. }
  42. func NewNgxServer() *NgxServer {
  43. return &NgxServer{
  44. Locations: make([]*NgxLocation, 0),
  45. Directives: make([]*NgxDirective, 0),
  46. }
  47. }
  48. func NewNgxConfig(filename string) *NgxConfig {
  49. return &NgxConfig{
  50. FileName: filename,
  51. Upstreams: make([]*NgxUpstream, 0),
  52. Name: path.Base(filename),
  53. }
  54. }