feat: Update project structure and configuration files
This commit is contained in:
53
backend/providers/grpc/config.go
Normal file
53
backend/providers/grpc/config.go
Normal 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)
|
||||
}
|
||||
27
backend/providers/grpc/provider.go
Normal file
27
backend/providers/grpc/provider.go
Normal 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()...)
|
||||
}
|
||||
Reference in New Issue
Block a user