This commit is contained in:
rogeecn
2025-03-22 16:39:52 +08:00
commit 306491168c
127 changed files with 17865 additions and 0 deletions

61
providers/cmux/config.go Normal file
View File

@@ -0,0 +1,61 @@
package cmux
import (
"fmt"
"quyun/providers/grpc"
"quyun/providers/http"
"github.com/soheilhy/cmux"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
"golang.org/x/sync/errgroup"
)
const DefaultPrefix = "Cmux"
func DefaultProvider() container.ProviderContainer {
return container.ProviderContainer{
Provider: Provide,
Options: []opt.Option{
opt.Prefix(DefaultPrefix),
},
}
}
type Config struct {
Host *string
Port uint
}
func (h *Config) Address() string {
if h.Host == nil {
return fmt.Sprintf(":%d", h.Port)
}
return fmt.Sprintf("%s:%d", *h.Host, h.Port)
}
type CMux struct {
Http *http.Service
Grpc *grpc.Grpc
Mux cmux.CMux
}
func (c *CMux) Serve() error {
// grpcL := c.Mux.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
// httpL := c.Mux.Match(cmux.HTTP1Fast())
// httpL := c.Mux.Match(cmux.Any())
httpL := c.Mux.Match(cmux.HTTP1Fast())
grpcL := c.Mux.Match(cmux.Any())
var eg errgroup.Group
eg.Go(func() error {
return c.Grpc.ServeWithListener(grpcL)
})
eg.Go(func() error {
return c.Http.Listener(httpL)
})
return c.Mux.Serve()
}

View File

@@ -0,0 +1,32 @@
package cmux
import (
"net"
"quyun/providers/grpc"
"quyun/providers/http"
"github.com/soheilhy/cmux"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
)
func Provide(opts ...opt.Option) error {
o := opt.New(opts...)
var config Config
if err := o.UnmarshalConfig(&config); err != nil {
return err
}
return container.Container.Provide(func(http *http.Service, grpc *grpc.Grpc) (*CMux, error) {
l, err := net.Listen("tcp", config.Address())
if err != nil {
return nil, err
}
return &CMux{
Http: http,
Grpc: grpc,
Mux: cmux.New(l),
}, nil
}, o.DiOptions()...)
}