gitee.com/sasukebo/go-micro/v4@v4.7.1/micro.go (about) 1 // Package micro is a pluggable framework for microservices 2 package micro 3 4 import ( 5 "context" 6 7 "gitee.com/sasukebo/go-micro/v4/client" 8 "gitee.com/sasukebo/go-micro/v4/server" 9 ) 10 11 type serviceKey struct{} 12 13 // Service is an interface that wraps the lower level libraries 14 // within go-micro. Its a convenience method for building 15 // and initialising services. 16 type Service interface { 17 // The service name 18 Name() string 19 // Init initialises options 20 Init(...Option) 21 // Options returns the current options 22 Options() Options 23 // Client is used to call services 24 Client() client.Client 25 // Server is for handling requests and events 26 Server() server.Server 27 // Run the service 28 Run() error 29 // The service implementation 30 String() string 31 } 32 33 // Function is a one time executing Service 34 type Function interface { 35 // Inherits Service interface 36 Service 37 // Done signals to complete execution 38 Done() error 39 // Handle registers an RPC handler 40 Handle(v interface{}) error 41 // Subscribe registers a subscriber 42 Subscribe(topic string, v interface{}) error 43 } 44 45 // Event is used to publish messages to a topic 46 type Event interface { 47 // Publish publishes a message to the event topic 48 Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error 49 } 50 51 // Type alias to satisfy the deprecation 52 type Publisher = Event 53 54 type Option func(*Options) 55 56 // NewService creates and returns a new Service based on the packages within. 57 func NewService(opts ...Option) Service { 58 return newService(opts...) 59 } 60 61 // FromContext retrieves a Service from the Context. 62 func FromContext(ctx context.Context) (Service, bool) { 63 s, ok := ctx.Value(serviceKey{}).(Service) 64 return s, ok 65 } 66 67 // NewContext returns a new Context with the Service embedded within it. 68 func NewContext(ctx context.Context, s Service) context.Context { 69 return context.WithValue(ctx, serviceKey{}, s) 70 } 71 72 // NewFunction returns a new Function for a one time executing Service 73 func NewFunction(opts ...Option) Function { 74 return newFunction(opts...) 75 } 76 77 // NewEvent creates a new event publisher 78 func NewEvent(topic string, c client.Client) Event { 79 if c == nil { 80 c = client.NewClient() 81 } 82 return &event{c, topic} 83 } 84 85 // Deprecated: NewPublisher returns a new Publisher 86 func NewPublisher(topic string, c client.Client) Event { 87 return NewEvent(topic, c) 88 } 89 90 // RegisterHandler is syntactic sugar for registering a handler 91 func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error { 92 return s.Handle(s.NewHandler(h, opts...)) 93 } 94 95 // RegisterSubscriber is syntactic sugar for registering a subscriber 96 func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error { 97 return s.Subscribe(s.NewSubscriber(topic, h, opts...)) 98 }