Browse Source

暴露服务接口

lxg 4 years ago
parent
commit
440f7abf45
4 changed files with 27 additions and 22 deletions
  1. 2 4
      .idea/workspace.xml
  2. 16 5
      micro.go
  3. 7 12
      options.go
  4. 2 1
      service.go

+ 2 - 4
.idea/workspace.xml

@@ -2,12 +2,10 @@
 <project version="4">
   <component name="ChangeListManager">
     <list default="true" id="cd58867b-089e-4508-9033-393b8939261c" name="Default Changelist" comment="">
-      <change afterPath="$PROJECT_DIR$/context.go" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/request.go" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/response.go" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/micro.go" beforeDir="false" afterPath="$PROJECT_DIR$/micro.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/options.go" beforeDir="false" afterPath="$PROJECT_DIR$/options.go" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/service.go" beforeDir="false" afterPath="$PROJECT_DIR$/service.go" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />

+ 16 - 5
micro.go

@@ -5,6 +5,8 @@ import (
 )
 
 type (
+	applicationKey struct {
+	}
 
 	HandleOptions struct {
 		HttpMethod  string
@@ -17,8 +19,8 @@ type (
 	HandleFunc func(ctx Context) (err error)
 
 	Application interface {
-		Handle(method string, cb HandleFunc) (err error)                    //注册一个处理器
-		NewRequest(service, method string, payload interface{}) (err error) //创建一个rpc请求
+		Handle(method string, cb HandleFunc, opts ...HandleOption)                     //注册一个处理器
+		CreateRequest(name, method string, body interface{}) (req *Request, err error) //创建一个rpc请求
 	}
 
 	Server interface {
@@ -27,8 +29,17 @@ type (
 	}
 )
 
-func WithHttpMethod(method string) HandleOption {
-	return func(o *HandleOptions) {
-		o.HttpMethod = method
+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)
 }

+ 7 - 12
options.go

@@ -16,41 +16,36 @@ type (
 		Server     Server
 		Context    context.Context
 	}
-	OptionMiddleware func(o *Options)
+	
+	Option func(o *Options)
 )
 
-func WithName(name string, version string) OptionMiddleware {
+func WithName(name string, version string) Option {
 	return func(o *Options) {
 		o.Name = name
 		o.Version = version
 	}
 }
 
-func WithRegistry(r registry.Registry) OptionMiddleware {
+func WithRegistry(r registry.Registry) Option {
 	return func(o *Options) {
 		o.registry = r
 	}
 }
 
-func WithContext(c context.Context) OptionMiddleware {
-	return func(o *Options) {
-		o.Context = c
-	}
-}
-
-func WithServer(s Server) OptionMiddleware {
+func WithServer(s Server) Option {
 	return func(o *Options) {
 		o.Server = s
 	}
 }
 
-func WithoutHttp() OptionMiddleware {
+func WithoutHttp() Option {
 	return func(o *Options) {
 		o.EnableHttp = false
 	}
 }
 
-func WithoutRPC() OptionMiddleware {
+func WithoutRPC() Option {
 	return func(o *Options) {
 		o.EnableRPC = false
 	}

+ 2 - 1
service.go

@@ -136,6 +136,7 @@ func (svr *Service) generateInstance() {
 
 func (svr *Service) prepare() (err error) {
 	log.Prefix(svr.opts.Name)
+	svr.ctx = WithContext(svr.ctx,svr)
 	if svr.listener, err = net.ListenTCP("tcp", nil); err != nil {
 		return
 	}
@@ -231,7 +232,7 @@ func (svr *Service) Run() (err error) {
 	return svr.destroy()
 }
 
-func New(opts ...OptionMiddleware) *Service {
+func New(opts ...Option) *Service {
 	o := NewOptions()
 	for _, opt := range opts {
 		opt(o)