github.com/annwntech/go-micro/v2@v2.9.5/micro.go (about) 1 // Package micro is a pluggable framework for microservices 2 package micro 3 4 import ( 5 "context" 6 7 "github.com/annwntech/go-micro/v2/client" 8 "github.com/annwntech/go-micro/v2/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 /* 46 // Type Event is a future type for acting on asynchronous events 47 type Event interface { 48 // Publish publishes a message to the event topic 49 Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error 50 // Subscribe to the event 51 Subscribe(ctx context.Context, v in 52 } 53 54 // Resource is a future type for defining dependencies 55 type Resource interface { 56 // Name of the resource 57 Name() string 58 // Type of resource 59 Type() string 60 // Method of creation 61 Create() error 62 } 63 */ 64 65 // Event is used to publish messages to a topic 66 type Event interface { 67 // Publish publishes a message to the event topic 68 Publish(ctx context.Context, msg interface{}, opts ...client.PublishOption) error 69 } 70 71 // Publisher Type alias to satisfy the deprecation 72 type Publisher = Event 73 74 type Option func(*Options) 75 76 var ( 77 HeaderPrefix = "Micro-" 78 ) 79 80 // NewService creates and returns a new Service based on the packages within. 81 func NewService(opts ...Option) Service { 82 return newService(opts...) 83 } 84 85 // FromContext retrieves a Service from the Context. 86 func FromContext(ctx context.Context) (Service, bool) { 87 s, ok := ctx.Value(serviceKey{}).(Service) 88 return s, ok 89 } 90 91 // NewContext returns a new Context with the Service embedded within it. 92 func NewContext(ctx context.Context, s Service) context.Context { 93 return context.WithValue(ctx, serviceKey{}, s) 94 } 95 96 // NewFunction returns a new Function for a one time executing Service 97 func NewFunction(opts ...Option) Function { 98 return newFunction(opts...) 99 } 100 101 // NewEvent creates a new event publisher 102 func NewEvent(topic string, c client.Client) Event { 103 if c == nil { 104 c = client.NewClient() 105 } 106 return &event{c, topic} 107 } 108 109 // Deprecated: NewPublisher returns a new Publisher 110 func NewPublisher(topic string, c client.Client) Event { 111 return NewEvent(topic, c) 112 } 113 114 // RegisterHandler is syntactic sugar for registering a handler 115 func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOption) error { 116 return s.Handle(s.NewHandler(h, opts...)) 117 } 118 119 // RegisterSubscriber is syntactic sugar for registering a subscriber 120 func RegisterSubscriber(topic string, s server.Server, h interface{}, opts ...server.SubscriberOption) error { 121 return s.Subscribe(s.NewSubscriber(topic, h, opts...)) 122 }