github.com/cloudwego/kitex@v0.9.0/pkg/endpoint/endpoint.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package endpoint 18 19 import "context" 20 21 // Endpoint represent one method for calling from remote. 22 type Endpoint func(ctx context.Context, req, resp interface{}) (err error) 23 24 // Middleware deal with input Endpoint and output Endpoint. 25 type Middleware func(Endpoint) Endpoint 26 27 // MiddlewareBuilder builds a middleware with information from a context. 28 type MiddlewareBuilder func(ctx context.Context) Middleware 29 30 // Chain connect middlewares into one middleware. 31 func Chain(mws ...Middleware) Middleware { 32 return func(next Endpoint) Endpoint { 33 for i := len(mws) - 1; i >= 0; i-- { 34 next = mws[i](next) 35 } 36 return next 37 } 38 } 39 40 // Build builds the given middlewares into one middleware. 41 func Build(mws []Middleware) Middleware { 42 if len(mws) == 0 { 43 return DummyMiddleware 44 } 45 return func(next Endpoint) Endpoint { 46 return mws[0](Build(mws[1:])(next)) 47 } 48 } 49 50 // DummyMiddleware is a dummy middleware. 51 func DummyMiddleware(next Endpoint) Endpoint { 52 return next 53 } 54 55 // DummyEndpoint is a dummy endpoint. 56 func DummyEndpoint(ctx context.Context, req, resp interface{}) (err error) { 57 return nil 58 } 59 60 type mwCtxKeyType int 61 62 // Keys for components attached to the context for a middleware builder. 63 const ( 64 CtxEventBusKey mwCtxKeyType = iota 65 CtxEventQueueKey 66 )