server

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package server 提供 HTTP/gRPC 服务器抽象。

核心功能:

  • HTTP 服务器(基于 Gin)
  • gRPC 服务器
  • 多路复用(同端口同时支持 HTTP/gRPC)
  • 服务器组管理

使用示例:

http := server.NewHTTP(cfg)
grpc := server.NewGRPC(cfg)
group := server.NewGroup(http, grpc)
group.Start(ctx)

Package server 提供统一的服务器抽象

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GRPCOption

type GRPCOption func(*GRPCOptions)

GRPCOption gRPC 配置选项

func WithGRPCAddr

func WithGRPCAddr(addr string) GRPCOption

WithGRPCAddr 设置地址

func WithMaxRecvMsgSize

func WithMaxRecvMsgSize(size int) GRPCOption

WithMaxRecvMsgSize 设置最大接收消息大小

func WithMaxSendMsgSize

func WithMaxSendMsgSize(size int) GRPCOption

WithMaxSendMsgSize 设置最大发送消息大小

func WithReflection

func WithReflection(enable bool) GRPCOption

WithReflection 启用 reflection

func WithStreamInterceptor

func WithStreamInterceptor(i grpc.StreamServerInterceptor) GRPCOption

WithStreamInterceptor 添加 Stream 拦截器

func WithUnaryInterceptor

func WithUnaryInterceptor(i grpc.UnaryServerInterceptor) GRPCOption

WithUnaryInterceptor 添加 Unary 拦截器

type GRPCOptions

type GRPCOptions struct {
	Options
	MaxRecvMsgSize     int
	MaxSendMsgSize     int
	EnableReflection   bool
	Interceptors       []grpc.UnaryServerInterceptor
	StreamInterceptors []grpc.StreamServerInterceptor
}

GRPCOptions gRPC 服务器配置

func DefaultGRPCOptions

func DefaultGRPCOptions() GRPCOptions

DefaultGRPCOptions 默认 gRPC 配置

type GRPCServer

type GRPCServer struct {
	// contains filtered or unexported fields
}

GRPCServer gRPC 服务器

func NewGRPCServer

func NewGRPCServer(opts ...GRPCOption) *GRPCServer

NewGRPCServer 创建 gRPC 服务器

func (*GRPCServer) Addr

func (s *GRPCServer) Addr() string

Addr 监听地址

func (*GRPCServer) HealthServer

func (s *GRPCServer) HealthServer() *health.Server

HealthServer 返回健康检查服务器

func (*GRPCServer) Name

func (s *GRPCServer) Name() string

Name 服务器名称

func (*GRPCServer) RegisterService

func (s *GRPCServer) RegisterService(fn func(*grpc.Server))

RegisterService 注册服务

func (*GRPCServer) Server

func (s *GRPCServer) Server() *grpc.Server

Server 返回底层 gRPC Server

func (*GRPCServer) Start

func (s *GRPCServer) Start(ctx context.Context) error

Start 启动服务器

func (*GRPCServer) Stop

func (s *GRPCServer) Stop(ctx context.Context) error

Stop 停止服务器

type Group

type Group struct {
	// contains filtered or unexported fields
}

Group 服务器组,管理多个服务器的生命周期

func NewGroup

func NewGroup(servers ...Server) *Group

NewGroup 创建服务器组

func (*Group) Add

func (g *Group) Add(s Server)

Add 添加服务器

func (*Group) Servers

func (g *Group) Servers() []Server

Servers 返回所有服务器

func (*Group) Start

func (g *Group) Start(ctx context.Context) error

Start 启动所有服务器

func (*Group) Stop

func (g *Group) Stop(ctx context.Context) error

Stop 停止所有服务器(逆序)

type HTTPServer

type HTTPServer struct {
	// contains filtered or unexported fields
}

HTTPServer HTTP 服务器

func NewHTTPServer

func NewHTTPServer(handler http.Handler, opts ...Option) *HTTPServer

NewHTTPServer 创建 HTTP 服务器

func (*HTTPServer) Addr

func (s *HTTPServer) Addr() string

Addr 监听地址

func (*HTTPServer) Handler

func (s *HTTPServer) Handler() http.Handler

Handler 返回 HTTP Handler

func (*HTTPServer) Name

func (s *HTTPServer) Name() string

Name 服务器名称

func (*HTTPServer) Start

func (s *HTTPServer) Start(ctx context.Context) error

Start 启动服务器

func (*HTTPServer) Stop

func (s *HTTPServer) Stop(ctx context.Context) error

Stop 停止服务器

type MultiplexServer

type MultiplexServer struct {
	// contains filtered or unexported fields
}

MultiplexServer 多协议复用服务器 (HTTP + gRPC 同端口)

func NewMultiplexServer

func NewMultiplexServer(httpHandler http.Handler, grpcServer *grpc.Server, opts ...Option) *MultiplexServer

NewMultiplexServer 创建多协议复用服务器

func (*MultiplexServer) Addr

func (s *MultiplexServer) Addr() string

Addr 监听地址

func (*MultiplexServer) Name

func (s *MultiplexServer) Name() string

Name 服务器名称

func (*MultiplexServer) Start

func (s *MultiplexServer) Start(ctx context.Context) error

Start 启动服务器

func (*MultiplexServer) Stop

func (s *MultiplexServer) Stop(ctx context.Context) error

Stop 停止服务器

type Option

type Option func(*Options)

Option 配置选项函数

func WithAddr

func WithAddr(addr string) Option

WithAddr 设置监听地址

func WithName

func WithName(name string) Option

WithName 设置服务器名称

func WithReadTimeout

func WithReadTimeout(d time.Duration) Option

WithReadTimeout 设置读超时

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout 设置关闭超时

func WithWriteTimeout

func WithWriteTimeout(d time.Duration) Option

WithWriteTimeout 设置写超时

type Options

type Options struct {
	Name            string
	Addr            string
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	IdleTimeout     time.Duration
	ShutdownTimeout time.Duration
	MaxHeaderBytes  int
}

Options 通用服务器配置

func ApplyOptions

func ApplyOptions(opts ...Option) Options

ApplyOptions 应用配置选项

func DefaultOptions

func DefaultOptions() Options

DefaultOptions 默认配置

type Runnable

type Runnable interface {
	Start(ctx context.Context) error
	Stop(ctx context.Context) error
}

Runnable 可运行组件接口(兼容 runtime)

type Server

type Server interface {
	// Start 启动服务器(非阻塞)
	Start(ctx context.Context) error
	// Stop 停止服务器
	Stop(ctx context.Context) error
	// Name 服务器名称
	Name() string
	// Addr 监听地址
	Addr() string
}

Server 服务器接口

Jump to

Keyboard shortcuts

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