|
| 1 | +package parrot |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// Cage is a container for all routes and sub-cages to handle routing and wildcard matching for a parrot |
| 11 | +// Note: Should only be used internally by the parrot. |
| 12 | +type Cage struct { |
| 13 | + *CageLevel |
| 14 | +} |
| 15 | + |
| 16 | +// CageLevel holds a single level of routes and further sub cages |
| 17 | +// Note: Should only be used internally by the parrot. |
| 18 | +type CageLevel struct { |
| 19 | + // Routes contains all of the plain routes at this current cage level |
| 20 | + // path -> route |
| 21 | + Routes map[string]*Route `json:"routes"` |
| 22 | + routesRWMu sync.RWMutex // sync.Map might be better here, but eh |
| 23 | + // WildCardRoutes contains all the wildcard routes at this current cage level |
| 24 | + // path -> route |
| 25 | + WildCardRoutes map[string]*Route `json:"wild_card_routes"` |
| 26 | + wildCardRoutesRWMu sync.RWMutex |
| 27 | + // SubCages contains sub cages at this current cage level |
| 28 | + // cage name -> cage level |
| 29 | + SubCages map[string]*CageLevel `json:"sub_cages"` |
| 30 | + subCagesRWMu sync.RWMutex |
| 31 | + // WildCardSubCages contains wildcard sub cages at this current cage level |
| 32 | + // cage name -> cage level |
| 33 | + WildCardSubCages map[string]*CageLevel `json:"wild_card_sub_cages"` |
| 34 | + wildCardSubCagesRWMu sync.RWMutex |
| 35 | +} |
| 36 | + |
| 37 | +// newCage creates a new cage with an empty cage level for a new parrot instance |
| 38 | +func newCage() *Cage { |
| 39 | + return &Cage{ |
| 40 | + CageLevel: newCageLevel(), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// newCageLevel creates a new cageLevel with empty maps |
| 45 | +func newCageLevel() *CageLevel { |
| 46 | + return &CageLevel{ |
| 47 | + Routes: make(map[string]*Route), |
| 48 | + WildCardRoutes: make(map[string]*Route), |
| 49 | + SubCages: make(map[string]*CageLevel), |
| 50 | + WildCardSubCages: make(map[string]*CageLevel), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// cageLevel searches for a cage level based on the path provided |
| 55 | +// If createMode is true, it will create any cage levels if they don't exist |
| 56 | +func (c *Cage) cageLevel(path string, createMode bool) (cageLevel *CageLevel, routeSegment string, err error) { |
| 57 | + splitPath := strings.Split(path, "/") |
| 58 | + routeSegment = splitPath[len(splitPath)-1] // The final path segment is the route |
| 59 | + splitPath = splitPath[:len(splitPath)-1] // Only looking for the cage level, exclude the route |
| 60 | + if splitPath[0] == "" { |
| 61 | + splitPath = splitPath[1:] // Remove the empty string at the beginning of the split |
| 62 | + } |
| 63 | + currentCageLevel := c.CageLevel |
| 64 | + |
| 65 | + for _, pathSegment := range splitPath { // Iterate through each path segment to look for matches |
| 66 | + cageLevel, found, err := currentCageLevel.subCageLevel(pathSegment, createMode) |
| 67 | + if err != nil { |
| 68 | + return nil, routeSegment, err |
| 69 | + } |
| 70 | + if found { |
| 71 | + currentCageLevel = cageLevel |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + if !found { |
| 76 | + return nil, routeSegment, newDynamicError(ErrCageNotFound, fmt.Sprintf("path: '%s'", path)) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return currentCageLevel, routeSegment, nil |
| 81 | +} |
| 82 | + |
| 83 | +// getRoute searches for a route based on the path provided |
| 84 | +func (c *Cage) getRoute(path string) (*Route, error) { |
| 85 | + cageLevel, routeSegment, err := c.cageLevel(path, false) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + route, found, err := cageLevel.route(routeSegment) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + if found { |
| 95 | + return route, nil |
| 96 | + } |
| 97 | + |
| 98 | + return nil, ErrRouteNotFound |
| 99 | +} |
| 100 | + |
| 101 | +// newRoute creates a new route, creating new cages if necessary |
| 102 | +func (c *Cage) newRoute(route *Route) error { |
| 103 | + cageLevel, routeSegment, err := c.cageLevel(route.Path, true) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if strings.Contains(routeSegment, "*") { |
| 109 | + cageLevel.wildCardRoutesRWMu.Lock() |
| 110 | + defer cageLevel.wildCardRoutesRWMu.Unlock() |
| 111 | + cageLevel.WildCardRoutes[routeSegment] = route |
| 112 | + } else { |
| 113 | + cageLevel.routesRWMu.Lock() |
| 114 | + defer cageLevel.routesRWMu.Unlock() |
| 115 | + cageLevel.Routes[routeSegment] = route |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// deleteRoute deletes a route based on the path provided |
| 122 | +func (c *Cage) deleteRoute(route *Route) error { |
| 123 | + cageLevel, routeSegment, err := c.cageLevel(route.Path, true) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if strings.Contains(routeSegment, "*") { |
| 129 | + cageLevel.wildCardRoutesRWMu.Lock() |
| 130 | + defer cageLevel.wildCardRoutesRWMu.Unlock() |
| 131 | + delete(cageLevel.WildCardRoutes, routeSegment) |
| 132 | + } else { |
| 133 | + cageLevel.routesRWMu.Lock() |
| 134 | + defer cageLevel.routesRWMu.Unlock() |
| 135 | + delete(cageLevel.Routes, routeSegment) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +// route searches for a route based on the route segment provided |
| 142 | +func (cl *CageLevel) route(routeSegment string) (route *Route, found bool, err error) { |
| 143 | + // First check for an exact match |
| 144 | + cl.routesRWMu.Lock() |
| 145 | + if route, found = cl.Routes[routeSegment]; found { |
| 146 | + defer cl.routesRWMu.Unlock() |
| 147 | + return route, true, nil |
| 148 | + } |
| 149 | + cl.routesRWMu.Unlock() |
| 150 | + |
| 151 | + // if not, look for wildcard routes |
| 152 | + cl.wildCardRoutesRWMu.Lock() |
| 153 | + defer cl.wildCardRoutesRWMu.Unlock() |
| 154 | + for wildCardPattern, route := range cl.WildCardRoutes { |
| 155 | + match, err := filepath.Match(wildCardPattern, routeSegment) |
| 156 | + if err != nil { |
| 157 | + return nil, false, newDynamicError(ErrInvalidPath, err.Error()) |
| 158 | + } |
| 159 | + if match { |
| 160 | + return route, true, nil |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return nil, false, nil |
| 165 | +} |
| 166 | + |
| 167 | +// subCageLevel searches for a sub cage level based on the segment provided |
| 168 | +// if createMode is true, it will create the cage level if it doesn't exist |
| 169 | +func (cl *CageLevel) subCageLevel(subCageSegment string, createMode bool) (cageLevel *CageLevel, found bool, err error) { |
| 170 | + // First check for an exact match |
| 171 | + cl.subCagesRWMu.RLock() |
| 172 | + if cageLevel, exists := cl.SubCages[subCageSegment]; exists { |
| 173 | + defer cl.subCagesRWMu.RUnlock() |
| 174 | + return cageLevel, true, nil |
| 175 | + } |
| 176 | + cl.subCagesRWMu.RUnlock() |
| 177 | + |
| 178 | + // if not, look for wildcard cages |
| 179 | + cl.wildCardSubCagesRWMu.RLock() |
| 180 | + for wildCardPattern, cageLevel := range cl.WildCardSubCages { |
| 181 | + match, err := filepath.Match(wildCardPattern, subCageSegment) |
| 182 | + if err != nil { |
| 183 | + cl.wildCardSubCagesRWMu.RUnlock() |
| 184 | + return nil, false, newDynamicError(ErrInvalidPath, err.Error()) |
| 185 | + } |
| 186 | + if match { |
| 187 | + cl.wildCardSubCagesRWMu.RUnlock() |
| 188 | + return cageLevel, true, nil |
| 189 | + } |
| 190 | + } |
| 191 | + cl.wildCardSubCagesRWMu.RUnlock() |
| 192 | + |
| 193 | + // We didn't find a match, so we'll create a new cage level if we're in create mode |
| 194 | + if createMode { |
| 195 | + newCage := newCageLevel() |
| 196 | + if strings.Contains(subCageSegment, "*") { |
| 197 | + cl.wildCardSubCagesRWMu.Lock() |
| 198 | + defer cl.wildCardSubCagesRWMu.Unlock() |
| 199 | + cl.WildCardSubCages[subCageSegment] = newCage |
| 200 | + } else { |
| 201 | + cl.subCagesRWMu.Lock() |
| 202 | + defer cl.subCagesRWMu.Unlock() |
| 203 | + cl.SubCages[subCageSegment] = newCage |
| 204 | + } |
| 205 | + return newCage, true, nil |
| 206 | + } |
| 207 | + |
| 208 | + return nil, false, nil |
| 209 | +} |
0 commit comments