go.uber.org/yarpc@v1.72.1/api/middleware/router.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package middleware 22 23 import ( 24 "context" 25 26 "go.uber.org/yarpc/api/transport" 27 ) 28 29 // Router is a middleware for defining a customized routing experience for procedures 30 type Router interface { 31 // Procedures returns the list of procedures that can be called on this router. 32 // Procedures SHOULD call into router that is passed in. 33 Procedures(transport.Router) []transport.Procedure 34 35 // Choose returns a HandlerSpec for the given request and transport. 36 // If the Router cannot determine what to call it should call into the router that was 37 // passed in. 38 Choose(context.Context, *transport.Request, transport.Router) (transport.HandlerSpec, error) 39 } 40 41 // ApplyRouteTable applies the given Router middleware to the given Router. 42 func ApplyRouteTable(r transport.RouteTable, m Router) transport.RouteTable { 43 if m == nil { 44 return r 45 } 46 return routeTableWithMiddleware{r: r, m: m} 47 } 48 49 type routeTableWithMiddleware struct { 50 r transport.RouteTable 51 m Router 52 } 53 54 func (r routeTableWithMiddleware) Procedures() []transport.Procedure { 55 return r.m.Procedures(r.r) 56 } 57 58 func (r routeTableWithMiddleware) Choose(ctx context.Context, req *transport.Request) (transport.HandlerSpec, error) { 59 return r.m.Choose(ctx, req, r.r) 60 } 61 62 func (r routeTableWithMiddleware) Register(procedures []transport.Procedure) { 63 r.r.Register(procedures) 64 }