watermelon

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: LGPL-3.0 Imports: 10 Imported by: 0

README

banner

Watermelon, A gRPC-service base tool

An unprecedented way of organizing middleware

service := &GreeterSrv{} // greeter srvice impl
srvBuilder := watermelon.NewServer()
srv := srvBuilder(func(srv *grpc.Server) {
    greeter.RegisterGreeterServer(srv, service)
})

a := srv.Group()
a.Use(func(ctx context.Context) error {
    if err := middleware.Next(ctx); err != nil {
        fmt.Println("The output can only be obtained when accessing the SayHelloAgain method", err)
        return err
    }
    fmt.Println("The output can only be obtained when accessing the SayHello method")
    return nil
})

a.Handler(service.SayHello)

b := a.Group()
b.Use(func(ctx context.Context) error {
    fullMethod := middleware.GetFullMethodFrom(ctx)
    if filepath.Base(fullMethod) == "SayHelloAgain" {
        return status.Error(codes.Aborted, "Don't say good things a second time")
    }
    return nil
})
b.Handler(service.SayHelloAgain)

srv.RunUntil(syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)

Customizable

Built entirely on generics, making it easier to extend.
Customizable service registration and discovery component.

See example/customize for more details.

// Server
type Server[T interface {
	WithMeta(register.NodeMeta) T
}] func(register func(srv *grpc.Server), opts ...infra.Option[T]) *infra.Srv[T]

// Client
type ClientConn[T infra.ClientServiceNameGenerator] func(serviceName string, opts ...infra.ClientOptions[T]) (*grpc.ClientConn, error)

// Register
func NewEtcdRegister[T Meta](client *clientv3.Client) register.ServiceRegister[T]

// Resolver
func NewEtcdResolver[T any](client *clientv3.Client, af AllowFuncType[T]) wresolver.Resolver

Chat & QA & News

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterETCDRegisterPrefixKey

func RegisterETCDRegisterPrefixKey(prefix string)

RegisterETCDRegisterPrefixKey a function to change default register(etcd) prefix key

func RegisterEtcdClient

func RegisterEtcdClient(etcdConfig clientv3.Config) error

ResolveEtcdClient a function to register etcd client to watermelon global

func RegisterRegionProxy

func RegisterRegionProxy(region, proxy string)

RegisterRegionProxy set region's proxy endpoint

func ResolveEtcdClient

func ResolveEtcdClient() *clientv3.Client

ResolveEtcdClient a function to get registed etcd client

func ResolveProxy

func ResolveProxy(region string) string

ResolveProxy return region's proxy, if it exist

Types

type ClientConn

type ClientConn[T infra.ClientServiceNameGenerator] func(serviceName string, opts ...infra.ClientOptions[T]) (*grpc.ClientConn, error)

ClientConn is a function to create grpc client connection

func NewClientConn

func NewClientConn() ClientConn[etcdresolver.ResolveMeta]

NewClientConn is a function to create a cc instance

func (*ClientConn[T]) WithDialTimeout

func (c *ClientConn[T]) WithDialTimeout(t time.Duration) infra.ClientOptions[T]

func (*ClientConn[T]) WithGrpcDialOptions

func (c *ClientConn[T]) WithGrpcDialOptions(opts ...grpc.DialOption) infra.ClientOptions[T]

func (*ClientConn[T]) WithNamespace

func (*ClientConn[T]) WithOrg

func (*ClientConn[T]) WithRegion

func (*ClientConn[T]) WithRegion(region string) infra.ClientOptions[etcdresolver.ResolveMeta]

func (*ClientConn[T]) WithServiceResolver

func (c *ClientConn[T]) WithServiceResolver(r resolver.Resolver) infra.ClientOptions[T]

type Server

type Server[T interface {
	WithMeta(register.NodeMeta) T
}] func(register func(srv *grpc.Server), opts ...infra.Option[T]) *infra.Srv[T]

Server is a function to build grpc service

func NewServer

func NewServer() Server[etcd.NodeMeta]

NewServer is a function to create a server instance

func (*Server[T]) WithAddress

func (*Server[T]) WithAddress(addr []infra.Address) infra.Option[T]

func (*Server[T]) WithGrpcServerOptions

func (s *Server[T]) WithGrpcServerOptions(opts ...grpc.ServerOption) infra.Option[T]

func (*Server[T]) WithHttpServer

func (*Server[T]) WithHttpServer(srv *http.Server) infra.Option[T]

func (*Server[T]) WithNamespace

func (s *Server[T]) WithNamespace(ns string) infra.Option[etcd.NodeMeta]

func (*Server[T]) WithOrg

func (s *Server[T]) WithOrg(org string) infra.Option[etcd.NodeMeta]

func (*Server[T]) WithRegion

func (s *Server[T]) WithRegion(region string) infra.Option[etcd.NodeMeta]

customized options

func (*Server[T]) WithServiceRegister

func (*Server[T]) WithServiceRegister(r register.ServiceRegister[etcd.NodeMeta]) infra.Option[etcd.NodeMeta]

copy infra options

func (*Server[T]) WithWeight

func (s *Server[T]) WithWeight(weight int32) infra.Option[etcd.NodeMeta]

Jump to

Keyboard shortcuts

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