feat(config): add grpc config. (#255)

This commit is contained in:
Bo-Yi Wu 2017-07-24 18:58:30 +08:00 committed by GitHub
parent 9a52f8f2b5
commit 41a8a609be
6 changed files with 35 additions and 3 deletions

View File

@ -86,6 +86,10 @@ core:
folder: ".cache" # folder for storing TLS certificates folder: ".cache" # folder for storing TLS certificates
host: "" # which domains the Let's Encrypt will attempt host: "" # which domains the Let's Encrypt will attempt
grpc:
enabled: false
port: 50051
api: api:
push_uri: "/api/push" push_uri: "/api/push"
stat_go_uri: "/api/stat/go" stat_go_uri: "/api/stat/go"

View File

@ -15,6 +15,7 @@ type ConfYaml struct {
Ios SectionIos `yaml:"ios"` Ios SectionIos `yaml:"ios"`
Log SectionLog `yaml:"log"` Log SectionLog `yaml:"log"`
Stat SectionStat `yaml:"stat"` Stat SectionStat `yaml:"stat"`
GRPC SectionGRPC `yaml:"grpc"`
} }
// SectionCore is sub section of config. // SectionCore is sub section of config.
@ -115,6 +116,12 @@ type SectionPID struct {
Override bool `yaml:"override"` Override bool `yaml:"override"`
} }
// SectionGRPC is sub section of config.
type SectionGRPC struct {
Enabled bool `yaml:"enabled"`
Port string `yaml:"port"`
}
// BuildDefaultPushConf is default config setting. // BuildDefaultPushConf is default config setting.
func BuildDefaultPushConf() ConfYaml { func BuildDefaultPushConf() ConfYaml {
var conf ConfYaml var conf ConfYaml
@ -165,17 +172,19 @@ func BuildDefaultPushConf() ConfYaml {
conf.Log.ErrorLevel = "error" conf.Log.ErrorLevel = "error"
conf.Log.HideToken = true conf.Log.HideToken = true
// Stat Engine
conf.Stat.Engine = "memory" conf.Stat.Engine = "memory"
conf.Stat.Redis.Addr = "localhost:6379" conf.Stat.Redis.Addr = "localhost:6379"
conf.Stat.Redis.Password = "" conf.Stat.Redis.Password = ""
conf.Stat.Redis.DB = 0 conf.Stat.Redis.DB = 0
conf.Stat.BoltDB.Path = "bolt.db" conf.Stat.BoltDB.Path = "bolt.db"
conf.Stat.BoltDB.Bucket = "gorush" conf.Stat.BoltDB.Bucket = "gorush"
conf.Stat.BuntDB.Path = "bunt.db" conf.Stat.BuntDB.Path = "bunt.db"
conf.Stat.LevelDB.Path = "level.db" conf.Stat.LevelDB.Path = "level.db"
// gRPC Server
conf.GRPC.Enabled = false
conf.GRPC.Port = "50051"
return conf return conf
} }

View File

@ -18,6 +18,10 @@ core:
folder: ".cache" # folder for storing TLS certificates folder: ".cache" # folder for storing TLS certificates
host: "" # which domains the Let's Encrypt will attempt host: "" # which domains the Let's Encrypt will attempt
grpc:
enabled: false
port: 50051
api: api:
push_uri: "/api/push" push_uri: "/api/push"
stat_go_uri: "/api/stat/go" stat_go_uri: "/api/stat/go"

View File

@ -111,6 +111,10 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "bunt.db", suite.ConfGorushDefault.Stat.BuntDB.Path) assert.Equal(suite.T(), "bunt.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorushDefault.Stat.LevelDB.Path) assert.Equal(suite.T(), "level.db", suite.ConfGorushDefault.Stat.LevelDB.Path)
// gRPC
assert.Equal(suite.T(), false, suite.ConfGorushDefault.GRPC.Enabled)
assert.Equal(suite.T(), "50051", suite.ConfGorushDefault.GRPC.Port)
} }
func (suite *ConfigTestSuite) TestValidateConf() { func (suite *ConfigTestSuite) TestValidateConf() {
@ -171,6 +175,10 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "bunt.db", suite.ConfGorush.Stat.BuntDB.Path) assert.Equal(suite.T(), "bunt.db", suite.ConfGorush.Stat.BuntDB.Path)
assert.Equal(suite.T(), "level.db", suite.ConfGorush.Stat.LevelDB.Path) assert.Equal(suite.T(), "level.db", suite.ConfGorush.Stat.LevelDB.Path)
// gRPC
assert.Equal(suite.T(), false, suite.ConfGorush.GRPC.Enabled)
assert.Equal(suite.T(), "50051", suite.ConfGorush.GRPC.Port)
} }
func TestConfigTestSuite(t *testing.T) { func TestConfigTestSuite(t *testing.T) {

View File

@ -11,6 +11,7 @@ import (
// RunHTTPServer provide run http or https protocol. // RunHTTPServer provide run http or https protocol.
func RunHTTPServer() (err error) { func RunHTTPServer() (err error) {
LogAccess.Debug("HTTPD server is running on " + PushConf.Core.Port + " port.")
if PushConf.Core.AutoTLS.Enabled { if PushConf.Core.AutoTLS.Enabled {
err = gracehttp.Serve(autoTLSServer()) err = gracehttp.Serve(autoTLSServer())
} else if PushConf.Core.SSL && PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" { } else if PushConf.Core.SSL && PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {

View File

@ -45,7 +45,12 @@ func (s *server) Send(ctx context.Context, in *pb.NotificationRequest) (*pb.Noti
// RunGRPCServer run gorush grpc server // RunGRPCServer run gorush grpc server
func RunGRPCServer() error { func RunGRPCServer() error {
lis, err := net.Listen("tcp", port) if !gorush.PushConf.GRPC.Enabled {
gorush.LogAccess.Debug("gRPC server is disabled.")
return nil
}
lis, err := net.Listen("tcp", ":"+gorush.PushConf.GRPC.Port)
if err != nil { if err != nil {
gorush.LogError.Error("failed to listen: %v", err) gorush.LogError.Error("failed to listen: %v", err)
return err return err
@ -54,6 +59,7 @@ func RunGRPCServer() error {
pb.RegisterGorushServer(s, &server{}) pb.RegisterGorushServer(s, &server{})
// Register reflection service on gRPC server. // Register reflection service on gRPC server.
reflection.Register(s) reflection.Register(s)
gorush.LogAccess.Debug("gRPC server is running on " + gorush.PushConf.GRPC.Port + " port.")
if err := s.Serve(lis); err != nil { if err := s.Serve(lis); err != nil {
gorush.LogError.Error("failed to serve: %v", err) gorush.LogError.Error("failed to serve: %v", err)
return err return err