feat: Update project structure and configuration files

This commit is contained in:
rogeecn
2025-03-24 09:29:38 +08:00
parent ea15a51556
commit 8de43c2861
159 changed files with 498 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package grpc
import (
"fmt"
"net"
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/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)
}
func (g *Grpc) ServeWithListener(ln net.Listener) error {
return g.Server.Serve(ln)
}

View File

@@ -0,0 +1,27 @@
package grpc
import (
"go.ipao.vip/atom/container"
"go.ipao.vip/atom/opt"
"google.golang.org/grpc"
)
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() (*Grpc, error) {
server := grpc.NewServer()
grpc := &Grpc{
Server: server,
config: &config,
}
container.AddCloseAble(grpc.Server.GracefulStop)
return grpc, nil
}, o.DiOptions()...)
}