gateway

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 20 Imported by: 9

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.

func WithMiddleware added in v1.9.0

func WithMiddleware(middlewares ...rest.Middleware) func(*Server)

WithMiddleware adds one or more middleware functions to process HTTP requests. Multiple middlewares will be executed in the order they were passed (like an onion model).

Types

type GatewayConf

type GatewayConf struct {
	rest.RestConf
	Upstreams []Upstream
}

GatewayConf is the configuration for gateway.

type HttpClientConf added in v1.8.0

type HttpClientConf struct {
	Target  string
	Prefix  string `json:",optional"`
	Timeout int64  `json:",default=3000"`
}

HttpClientConf is the configuration for an HTTP client.

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, optional.
	// If the mapping is for HTTP, it's not necessary.
	RpcPath string `json:",optional"`
}

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. To get a graceful shutdown, it stops the HTTP server first, then closes gRPC connections.

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 `json:",optional"`
	// Http is the target of the upstream.
	Http *HttpClientConf `json:",optional=!grpc"`
	// 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 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