gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/server/server.go (about) 1 // Package server is an interface for a micro server 2 package server 3 4 import ( 5 "context" 6 "os" 7 "os/signal" 8 "syscall" 9 "time" 10 11 "github.com/google/uuid" 12 "gitee.com/liuxuezhan/go-micro-v1.18.0/codec" 13 "gitee.com/liuxuezhan/go-micro-v1.18.0/registry" 14 log "gitee.com/liuxuezhan/go-micro-v1.18.0/util/log" 15 ) 16 17 // Server is a simple micro server abstraction 18 type Server interface { 19 Options() Options 20 Init(...Option) error 21 Handle(Handler) error 22 NewHandler(interface{}, ...HandlerOption) Handler 23 NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber 24 Subscribe(Subscriber) error 25 Start() error 26 Stop() error 27 String() string 28 } 29 30 // Router handle serving messages 31 type Router interface { 32 // ProcessMessage processes a message 33 ProcessMessage(context.Context, Message) error 34 // ServeRequest processes a request to completion 35 ServeRequest(context.Context, Request, Response) error 36 } 37 38 // Message is an async message interface 39 type Message interface { 40 // Topic of the message 41 Topic() string 42 // The decoded payload value 43 Payload() interface{} 44 // The content type of the payload 45 ContentType() string 46 // The raw headers of the message 47 Header() map[string]string 48 // The raw body of the message 49 Body() []byte 50 // Codec used to decode the message 51 Codec() codec.Reader 52 } 53 54 // Request is a synchronous request interface 55 type Request interface { 56 // Service name requested 57 Service() string 58 // The action requested 59 Method() string 60 // Endpoint name requested 61 Endpoint() string 62 // Content type provided 63 ContentType() string 64 // Header of the request 65 Header() map[string]string 66 // Body is the initial decoded value 67 Body() interface{} 68 // Read the undecoded request body 69 Read() ([]byte, error) 70 // The encoded message stream 71 Codec() codec.Reader 72 // Indicates whether its a stream 73 Stream() bool 74 } 75 76 // Response is the response writer for unencoded messages 77 type Response interface { 78 // Encoded writer 79 Codec() codec.Writer 80 // Write the header 81 WriteHeader(map[string]string) 82 // write a response directly to the client 83 Write([]byte) error 84 } 85 86 // Stream represents a stream established with a client. 87 // A stream can be bidirectional which is indicated by the request. 88 // The last error will be left in Error(). 89 // EOF indicates end of the stream. 90 type Stream interface { 91 Context() context.Context 92 Request() Request 93 Send(interface{}) error 94 Recv(interface{}) error 95 Error() error 96 Close() error 97 } 98 99 // Handler interface represents a request handler. It's generated 100 // by passing any type of public concrete object with endpoints into server.NewHandler. 101 // Most will pass in a struct. 102 // 103 // Example: 104 // 105 // type Greeter struct {} 106 // 107 // func (g *Greeter) Hello(context, request, response) error { 108 // return nil 109 // } 110 // 111 type Handler interface { 112 Name() string 113 Handler() interface{} 114 Endpoints() []*registry.Endpoint 115 Options() HandlerOptions 116 } 117 118 // Subscriber interface represents a subscription to a given topic using 119 // a specific subscriber function or object with endpoints. 120 type Subscriber interface { 121 Topic() string 122 Subscriber() interface{} 123 Endpoints() []*registry.Endpoint 124 Options() SubscriberOptions 125 } 126 127 type Option func(*Options) 128 129 var ( 130 DefaultAddress = ":0" 131 DefaultName = "go.micro.server" 132 DefaultVersion = time.Now().Format("2006.01.02.15.04") 133 DefaultId = uuid.New().String() 134 DefaultServer Server = newRpcServer() 135 DefaultRouter = newRpcRouter() 136 DefaultRegisterCheck = func(context.Context) error { return nil } 137 DefaultRegisterInterval = time.Second * 30 138 DefaultRegisterTTL = time.Minute 139 ) 140 141 // DefaultOptions returns config options for the default service 142 func DefaultOptions() Options { 143 return DefaultServer.Options() 144 } 145 146 // Init initialises the default server with options passed in 147 func Init(opt ...Option) { 148 if DefaultServer == nil { 149 DefaultServer = newRpcServer(opt...) 150 } 151 DefaultServer.Init(opt...) 152 } 153 154 // NewServer returns a new server with options passed in 155 func NewServer(opt ...Option) Server { 156 return newRpcServer(opt...) 157 } 158 159 // NewRouter returns a new router 160 func NewRouter() *router { 161 return newRpcRouter() 162 } 163 164 // NewSubscriber creates a new subscriber interface with the given topic 165 // and handler using the default server 166 func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber { 167 return DefaultServer.NewSubscriber(topic, h, opts...) 168 } 169 170 // NewHandler creates a new handler interface using the default server 171 // Handlers are required to be a public object with public 172 // endpoints. Call to a service endpoint such as Foo.Bar expects 173 // the type: 174 // 175 // type Foo struct {} 176 // func (f *Foo) Bar(ctx, req, rsp) error { 177 // return nil 178 // } 179 // 180 func NewHandler(h interface{}, opts ...HandlerOption) Handler { 181 return DefaultServer.NewHandler(h, opts...) 182 } 183 184 // Handle registers a handler interface with the default server to 185 // handle inbound requests 186 func Handle(h Handler) error { 187 return DefaultServer.Handle(h) 188 } 189 190 // Subscribe registers a subscriber interface with the default server 191 // which subscribes to specified topic with the broker 192 func Subscribe(s Subscriber) error { 193 return DefaultServer.Subscribe(s) 194 } 195 196 // Run starts the default server and waits for a kill 197 // signal before exiting. Also registers/deregisters the server 198 func Run() error { 199 if err := Start(); err != nil { 200 return err 201 } 202 203 ch := make(chan os.Signal, 1) 204 signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT) 205 log.Logf("Received signal %s", <-ch) 206 207 return Stop() 208 } 209 210 // Start starts the default server 211 func Start() error { 212 config := DefaultServer.Options() 213 log.Logf("Starting server %s id %s", config.Name, config.Id) 214 return DefaultServer.Start() 215 } 216 217 // Stop stops the default server 218 func Stop() error { 219 log.Logf("Stopping server") 220 return DefaultServer.Stop() 221 } 222 223 // String returns name of Server implementation 224 func String() string { 225 return DefaultServer.String() 226 }