package main import ( "fmt" "net/url" "sig-pub/internal/gateway" "sig-pub/pkg/config" "sig-pub/pkg/grpc/discovery" "sig-pub/pkg/grpc/generic" "sig-pub/pkg/utils/exit" "github.com/hashicorp/consul/api" _ "github.com/mostynb/go-grpc-compression/snappy" // 注册grpc snappy compress "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) type GateConf struct { HttpAddr string WsAddr string SigServer string } // http 网关 func main() { // load config conf := config.MustLoadConfig(new(config.Configuration), "config/config.toml") gateConf := config.MustLoadConfig(new(GateConf), "config/gateway.toml") // consul 配置 cc := api.DefaultConfig() cc.Address = conf.Consul.Address client, err := api.NewClient(cc) if err != nil { panic(fmt.Errorf("consul client error: %v", err)) } // consul service discovery dis := discovery.NewConsulDiscovery(client) resolver := dis.Resolver() grpcGenericClientFactory := generic.NewGrpcGenericClientFactory( discovery.ConsulSchema, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolver), ) if err := grpcGenericClientFactory.Init(); err != nil { panic(err) } if err := dis.WatchServices(grpcGenericClientFactory.RefreshService); err != nil { panic(err) } sigServerUrl, err := url.Parse(gateConf.SigServer) if err != nil { panic(err) } gateServer := gateway.NewFastGatewayServer(sigServerUrl, grpcGenericClientFactory) // gateServer := gateway.NewGateServer(sigServerUrl, gpcGenericClientFactory) if err := gateServer.Init(); err != nil { panic(err) } go func() { if err := gateServer.Run(gateConf.HttpAddr); err != nil { panic(err) } }() exit.Await() }