endpoints.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package dynamicproxy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. )
  7. /*
  8. endpoint.go
  9. author: tobychui
  10. This script handle the proxy endpoint object actions
  11. so proxyEndpoint can be handled like a proper oop object
  12. Most of the functions are implemented in dynamicproxy.go
  13. */
  14. // Get virtual directory handler from given URI
  15. func (ep *ProxyEndpoint) GetVirtualDirectoryHandlerFromRequestURI(requestURI string) *VirtualDirectoryEndpoint {
  16. for _, vdir := range ep.VirtualDirectories {
  17. if strings.HasPrefix(requestURI, vdir.MatchingPath) {
  18. return vdir
  19. }
  20. }
  21. return nil
  22. }
  23. // Get virtual directory handler by matching path (exact match required)
  24. func (ep *ProxyEndpoint) GetVirtualDirectoryRuleByMatchingPath(matchingPath string) *VirtualDirectoryEndpoint {
  25. for _, vdir := range ep.VirtualDirectories {
  26. if vdir.MatchingPath == matchingPath {
  27. return vdir
  28. }
  29. }
  30. return nil
  31. }
  32. // Delete a vdir rule by its matching path
  33. func (ep *ProxyEndpoint) RemoveVirtualDirectoryRuleByMatchingPath(matchingPath string) error {
  34. entryFound := false
  35. newVirtualDirectoryList := []*VirtualDirectoryEndpoint{}
  36. for _, vdir := range ep.VirtualDirectories {
  37. if vdir.MatchingPath == matchingPath {
  38. entryFound = true
  39. } else {
  40. newVirtualDirectoryList = append(newVirtualDirectoryList, vdir)
  41. }
  42. }
  43. if entryFound {
  44. //Update the list of vdirs
  45. ep.VirtualDirectories = newVirtualDirectoryList
  46. return nil
  47. }
  48. return errors.New("target virtual directory routing rule not found")
  49. }
  50. // Delete a vdir rule by its matching path
  51. func (ep *ProxyEndpoint) AddVirtualDirectoryRule(vdir *VirtualDirectoryEndpoint) (*ProxyEndpoint, error) {
  52. //Check for matching path duplicate
  53. if ep.GetVirtualDirectoryRuleByMatchingPath(vdir.MatchingPath) != nil {
  54. return nil, errors.New("rule with same matching path already exists")
  55. }
  56. //Append it to the list of virtual directory
  57. ep.VirtualDirectories = append(ep.VirtualDirectories, vdir)
  58. //Prepare to replace the current routing rule
  59. parentRouter := ep.parent
  60. readyRoutingRule, err := parentRouter.PrepareProxyRoute(ep)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if ep.ProxyType == ProxyType_Root {
  65. parentRouter.Root = readyRoutingRule
  66. } else if ep.ProxyType == ProxyType_Host {
  67. ep.Remove()
  68. parentRouter.AddProxyRouteToRuntime(readyRoutingRule)
  69. } else {
  70. return nil, errors.New("unsupported proxy type")
  71. }
  72. return readyRoutingRule, nil
  73. }
  74. // Create a deep clone object of the proxy endpoint
  75. // Note the returned object is not activated. Call to prepare function before pushing into runtime
  76. func (ep *ProxyEndpoint) Clone() *ProxyEndpoint {
  77. clonedProxyEndpoint := ProxyEndpoint{}
  78. js, _ := json.Marshal(ep)
  79. json.Unmarshal(js, &clonedProxyEndpoint)
  80. return &clonedProxyEndpoint
  81. }
  82. // Remove this proxy endpoint from running proxy endpoint list
  83. func (ep *ProxyEndpoint) Remove() error {
  84. ep.parent.ProxyEndpoints.Delete(ep.RootOrMatchingDomain)
  85. return nil
  86. }
  87. // Write changes to runtime without respawning the proxy handler
  88. // use prepare -> remove -> add if you change anything in the endpoint
  89. // that effects the proxy routing src / dest
  90. func (ep *ProxyEndpoint) UpdateToRuntime() {
  91. ep.parent.ProxyEndpoints.Store(ep.RootOrMatchingDomain, ep)
  92. }