feat: add grpc

This commit is contained in:
Rogee
2024-12-31 17:57:06 +08:00
parent daf39f7255
commit 64c08249df
8 changed files with 221 additions and 59 deletions

View File

@@ -0,0 +1,48 @@
package grpc
import (
"fmt"
"net"
"git.ipao.vip/rogeecn/atom/container"
"git.ipao.vip/rogeecn/atom/utils/opt"
"google.golang.org/grpc"
)
const DefaultPrefix = "Grpc"
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 Grpc struct {
Server *grpc.Server
config *Config
}
// Serve
func (g *Grpc) Serve() error {
l, err := net.Listen("tcp", g.config.Address())
if err != nil {
return err
}
return g.Server.Serve(l)
}