gateway

package
v1.4.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2022 License: MIT Imports: 16 Imported by: 0

README

Gateway

Usage

  • main.go
var configFile = flag.String("f", "config.yaml", "config file")

func main() {
    flag.Parse()

    var c gateway.GatewayConf
    conf.MustLoad(*configFile, &c)
    gw := gateway.MustNewServer(c)
    defer gw.Stop()
    gw.Start()
}
  • config.yaml
Name: demo-gateway
Host: localhost
Port: 8888
Upstreams:
  - Grpc:
      Etcd:
        Hosts:
          - localhost:2379
        Key: hello.rpc
    # protoset mode
    ProtoSets:
      - hello.pb
    # Mappings can also be written in proto options
    Mappings:
      - Method: get
        Path: /pingHello/:ping
        RpcPath: hello.Hello/Ping
  - Grpc:
      Endpoints:
        - localhost:8081
    # reflection mode, no ProtoSet settings
    Mappings:
      - Method: post
        Path: /pingWorld
        RpcPath: world.World/Ping

Generate ProtoSet files

  • example command without external imports
protoc --descriptor_set_out=hello.pb hello.proto
  • example command with external imports
protoc --include_imports --proto_path=. --descriptor_set_out=hello.pb hello.proto

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithHeaderProcessor

func WithHeaderProcessor(processHeader func(http.Header) []string) func(*Server)

WithHeaderProcessor sets a processor to process request headers. The returned headers are used as metadata to invoke the RPC.

Types

type GatewayConf

type GatewayConf struct {
	rest.RestConf
	Upstreams []Upstream
	Timeout   time.Duration `json:",default=5s"`
}

GatewayConf is the configuration for gateway.

type Option

type Option func(svr *Server)

Option defines the method to customize Server.

type RouteMapping

type RouteMapping struct {
	// Method is the HTTP method, like GET, POST, PUT, DELETE.
	Method string
	// Path is the HTTP path.
	Path string
	// RpcPath is the gRPC rpc method, with format of package.service/method
	RpcPath string
}

RouteMapping is a mapping between a gateway route and an upstream rpc method.

type Server

type Server struct {
	*rest.Server
	// contains filtered or unexported fields
}

Server is a gateway server.

func MustNewServer

func MustNewServer(c GatewayConf, opts ...Option) *Server

MustNewServer creates a new gateway server.

func (*Server) Start

func (s *Server) Start()

Start starts the gateway server.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the gateway server.

type Upstream

type Upstream struct {
	// Name is the name of the upstream.
	Name string `json:",optional"`
	// Grpc is the target of the upstream.
	Grpc zrpc.RpcClientConf
	// ProtoSets is the file list of proto set, like [hello.pb].
	// if your proto file import another proto file, you need to write multi-file slice,
	// like [hello.pb, common.pb].
	ProtoSets []string `json:",optional"`
	// Mappings is the mapping between gateway routes and Upstream rpc methods.
	// Keep it blank if annotations are added in rpc methods.
	Mappings []RouteMapping `json:",optional"`
}

Upstream is the configuration for an upstream.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL