github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/server.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/server/server.go
    16  
    17  package server
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/google/uuid"
    24  	"github.com/tickoalcantara12/micro/v3/service/registry"
    25  	"github.com/tickoalcantara12/micro/v3/util/codec"
    26  )
    27  
    28  // DefaultServer for the service
    29  var DefaultServer Server
    30  
    31  // Server is a simple micro server abstraction
    32  type Server interface {
    33  	// Initialise options
    34  	Init(...Option) error
    35  	// Retrieve the options
    36  	Options() Options
    37  	// Register a handler
    38  	Handle(Handler) error
    39  	// Create a new handler
    40  	NewHandler(interface{}, ...HandlerOption) Handler
    41  	// Create a new subscriber
    42  	NewSubscriber(string, interface{}, ...SubscriberOption) Subscriber
    43  	// Register a subscriber
    44  	Subscribe(Subscriber) error
    45  	// Start the server
    46  	Start() error
    47  	// Stop the server
    48  	Stop() error
    49  	// Server implementation
    50  	String() string
    51  }
    52  
    53  // Router handle serving messages
    54  type Router interface {
    55  	// ProcessMessage processes a message
    56  	ProcessMessage(context.Context, Message) error
    57  	// ServeRequest processes a request to completion
    58  	ServeRequest(context.Context, Request, Response) error
    59  }
    60  
    61  // Message is an async message interface
    62  type Message interface {
    63  	// Topic of the message
    64  	Topic() string
    65  	// The decoded payload value
    66  	Payload() interface{}
    67  	// The content type of the payload
    68  	ContentType() string
    69  	// The raw headers of the message
    70  	Header() map[string]string
    71  	// The raw body of the message
    72  	Body() []byte
    73  	// Codec used to decode the message
    74  	Codec() codec.Reader
    75  }
    76  
    77  // Request is a synchronous request interface
    78  type Request interface {
    79  	// Service name requested
    80  	Service() string
    81  	// The action requested
    82  	Method() string
    83  	// Endpoint name requested
    84  	Endpoint() string
    85  	// Content type provided
    86  	ContentType() string
    87  	// Header of the request
    88  	Header() map[string]string
    89  	// Body is the initial decoded value
    90  	Body() interface{}
    91  	// Read the undecoded request body
    92  	Read() ([]byte, error)
    93  	// The encoded message stream
    94  	Codec() codec.Reader
    95  	// Indicates whether its a stream
    96  	Stream() bool
    97  }
    98  
    99  // Response is the response writer for unencoded messages
   100  type Response interface {
   101  	// Encoded writer
   102  	Codec() codec.Writer
   103  	// Write the header
   104  	WriteHeader(map[string]string)
   105  	// write a response directly to the client
   106  	Write([]byte) error
   107  }
   108  
   109  // Stream represents a stream established with a client.
   110  // A stream can be bidirectional which is indicated by the request.
   111  // The last error will be left in Error().
   112  // EOF indicates end of the stream.
   113  type Stream interface {
   114  	Context() context.Context
   115  	Request() Request
   116  	Send(interface{}) error
   117  	Recv(interface{}) error
   118  	Error() error
   119  	Close() error
   120  }
   121  
   122  // Handler interface represents a request handler. It's generated
   123  // by passing any type of public concrete object with endpoints into server.NewHandler.
   124  // Most will pass in a struct.
   125  //
   126  // Example:
   127  //
   128  //      type Greeter struct {}
   129  //
   130  //      func (g *Greeter) Hello(context, request, response) error {
   131  //              return nil
   132  //      }
   133  //
   134  type Handler interface {
   135  	Name() string
   136  	Handler() interface{}
   137  	Endpoints() []*registry.Endpoint
   138  	Options() HandlerOptions
   139  }
   140  
   141  // Subscriber interface represents a subscription to a given topic using
   142  // a specific subscriber function or object with endpoints. It mirrors
   143  // the handler in its behaviour.
   144  type Subscriber interface {
   145  	Topic() string
   146  	Subscriber() interface{}
   147  	Endpoints() []*registry.Endpoint
   148  	Options() SubscriberOptions
   149  }
   150  
   151  type Option func(*Options)
   152  
   153  var (
   154  	DefaultAddress          = ":0"
   155  	DefaultName             = "go.micro.server"
   156  	DefaultVersion          = "latest"
   157  	DefaultId               = uuid.New().String()
   158  	DefaultRegisterCheck    = func(context.Context) error { return nil }
   159  	DefaultRegisterInterval = time.Second * 30
   160  	DefaultRegisterTTL      = time.Second * 90
   161  )
   162  
   163  // Register a handler
   164  func Handle(hdlr Handler) error {
   165  	return DefaultServer.Handle(hdlr)
   166  }
   167  
   168  // Create a new handler
   169  func NewHandler(hdlr interface{}) Handler {
   170  	return DefaultServer.NewHandler(hdlr)
   171  }
   172  
   173  // Create a new subscriber
   174  func NewSubscriber(topic string, hdlr interface{}) Subscriber {
   175  	return DefaultServer.NewSubscriber(topic, hdlr)
   176  }
   177  
   178  // Register a subscriber
   179  func Subscribe(sub Subscriber) error {
   180  	return DefaultServer.Subscribe(sub)
   181  }