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.
68 lines
1.6 KiB
68 lines
1.6 KiB
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() |
|
gpcGenericClientFactory := generic.NewGpcGenericClientFactory( |
|
discovery.ConsulSchema, |
|
grpc.WithTransportCredentials(insecure.NewCredentials()), |
|
grpc.WithResolvers(resolver), |
|
) |
|
if err := gpcGenericClientFactory.Init(); err != nil { |
|
panic(err) |
|
} |
|
if err := dis.WatchServices(gpcGenericClientFactory.RefreshService); err != nil { |
|
panic(err) |
|
} |
|
|
|
sigServerUrl, err := url.Parse(gateConf.SigServer) |
|
if err != nil { |
|
panic(err) |
|
} |
|
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() |
|
}
|
|
|