You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

91 lines
2.1 KiB

package main
import (
"context"
"net"
"sig-pub/api/pb"
"sig-pub/internal/market"
"sig-pub/pkg/config"
"sig-pub/pkg/grpc/discovery"
"sig-pub/pkg/grpc/interceptor"
"sig-pub/pkg/storage/rdb"
"sig-pub/pkg/utils/exit"
"sig-pub/pkg/zlog"
clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
type MarketConf struct {
Register discovery.Server
GrpcReflection bool
}
func main() {
// load config
conf := config.MustLoadConfig(new(config.Configuration), "config/config.toml")
marketConf := config.MustLoadConfig(new(MarketConf), "config/market.toml")
// etcd discovery
etcdClient, err := clientv3.New(conf.Etcd)
if err != nil {
panic(err)
}
exit.AddHook(func() { _ = etcdClient.Close() }, exit.WithOrderTail())
registry := discovery.NewEtcdDiscovery(etcdClient)
// database
db, err := conf.Database.Postgres.NewGormDB()
if err != nil {
panic(err)
}
rdb := rdb.NewRDB(db)
if err := rdb.Init(); err != nil {
panic(err)
}
tradeInstanceService := market.NewTradeInstanceService(rdb)
if err := tradeInstanceService.Init(); err != nil {
panic(err)
}
marketGrpcServer := market.NewMarketGrpcServer(tradeInstanceService)
grpcServer := grpc.NewServer(config.GetGrpcOptions(
conf.Grpc,
grpc.UnaryInterceptor(interceptor.RecoverInterceptor))...,
)
if marketConf.GrpcReflection {
// 注册反射服务
reflection.Register(grpcServer)
}
pb.RegisterMarketServer(grpcServer, marketGrpcServer)
register := marketConf.Register
if register.Name == "" {
register.Name = pb.Market_ServiceDesc.ServiceName
}
ctx, cancel := context.WithCancel(context.Background())
exit.AddHook(cancel, exit.WithOrderFront())
if err := registry.Registry(ctx, register); err != nil {
panic(err)
}
exit.AddHook(grpcServer.GracefulStop, exit.WithOrderFront())
// run grpc server
go func() {
listen, err := net.Listen("tcp", marketConf.Register.Addr)
if err != nil {
panic(err)
}
zlog.Infof("%s grpc server running %s\n", register.Name, listen.Addr().String())
if err := grpcServer.Serve(listen); err != nil {
panic(err)
}
}()
exit.Await()
}