123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package micro
- import (
- "context"
- "git.nspix.com/golang/micro/gateway/http"
- "git.nspix.com/golang/micro/gateway/rpc"
- "git.nspix.com/golang/micro/registry"
- )
- type (
- applicationKey struct {
- }
- HandleOptions struct {
- DisableRpc bool //禁用RPC功能
- DisableHttp bool //禁用HTTP功能
- HttpPath string //重定向HTTP路由
- HttpMethod string //HTTP路径
- }
- HandleOption func(o *HandleOptions)
- HandleFunc func(ctx Context) (err error)
- Application interface {
- Node() *registry.ServiceNode //获取节点信息
- HttpServe() *http.Server //获取HTTP实例
- RPCServe() *rpc.Server //获取RPC实例
- PeekService(name string) ([]*registry.ServiceNode, error) //选择一个服务
- Handle(method string, cb HandleFunc, opts ...HandleOption) //注册一个处理器
- NewRequest(name, method string, body interface{}) (req *Request, err error) //创建一个rpc请求
- }
- Server interface {
- Start(ctx context.Context) (err error)
- Stop() (err error)
- }
- )
- var (
- contextKey = applicationKey{}
- )
- func FromContext(ctx context.Context) Application {
- if v := ctx.Value(contextKey); v != nil {
- return v.(Application)
- }
- return nil
- }
- func WithContext(ctx context.Context, app Application) context.Context {
- return context.WithValue(ctx, contextKey, app)
- }
|