From bfd2340732f90b9b56382aa684be86506ed9ad60 Mon Sep 17 00:00:00 2001 From: GuerraMorgan <35589850+GuerraMorgan@users.noreply.github.com> Date: Wed, 12 Aug 2020 20:31:28 +0800 Subject: [PATCH] Add: Unix Socket support (#466) * Update conf.go * Update driver.go * Update session.go * Update defaults.go * Update main.go * Update conf.go * Update defaults.go --- main.go | 10 ++++++++++ middleware/session.go | 2 +- pkg/cache/driver.go | 2 +- pkg/conf/conf.go | 6 ++++++ pkg/conf/defaults.go | 5 +++++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index de6bce0..558a66a 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,16 @@ func main() { }() } + // 如果启用了Unix + if conf.UnixConfig.Listen != "" { + go func() { + util.Log().Info("开始监听 %s", conf.UnixConfig.Listen) + if err := api.RunUnix(conf.UnixConfig.Listen); err != nil { + util.Log().Error("无法监听[%s],%s", conf.UnixConfig.Listen, err) + } + }() + } + util.Log().Info("开始监听 %s", conf.SystemConfig.Listen) if err := api.Run(conf.SystemConfig.Listen); err != nil { util.Log().Error("无法监听[%s],%s", conf.SystemConfig.Listen, err) diff --git a/middleware/session.go b/middleware/session.go index 06b56e5..f89609d 100644 --- a/middleware/session.go +++ b/middleware/session.go @@ -18,7 +18,7 @@ func Session(secret string) gin.HandlerFunc { // Redis设置不为空,且非测试模式时使用Redis if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode { var err error - Store, err = redis.NewStoreWithDB(10, "tcp", conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret)) + Store, err = redis.NewStoreWithDB(10, conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, []byte(secret)) if err != nil { util.Log().Panic("无法连接到 Redis:%s", err) } diff --git a/pkg/cache/driver.go b/pkg/cache/driver.go index 0226531..07d7152 100644 --- a/pkg/cache/driver.go +++ b/pkg/cache/driver.go @@ -15,7 +15,7 @@ func Init() { if conf.RedisConfig.Server != "" && gin.Mode() != gin.TestMode { Store = NewRedisStore( 10, - "tcp", + conf.RedisConfig.Network, conf.RedisConfig.Server, conf.RedisConfig.Password, conf.RedisConfig.DB, diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index a19110b..43f2481 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -33,6 +33,10 @@ type ssl struct { Listen string `validate:"required"` } +type unix struct { + Listen string +} + // slave 作为slave存储端配置 type slave struct { Secret string `validate:"omitempty,gte=64"` @@ -57,6 +61,7 @@ type captcha struct { // redis 配置 type redis struct { + Network string Server string Password string DB string @@ -120,6 +125,7 @@ func Init(path string) { "Database": DatabaseConfig, "System": SystemConfig, "SSL": SSLConfig, + "Unix": UnixConfig, "Captcha": CaptchaConfig, "Redis": RedisConfig, "Thumbnail": ThumbConfig, diff --git a/pkg/conf/defaults.go b/pkg/conf/defaults.go index 406a203..3482cb8 100644 --- a/pkg/conf/defaults.go +++ b/pkg/conf/defaults.go @@ -4,6 +4,7 @@ import "github.com/mojocn/base64Captcha" // RedisConfig Redis服务器配置 var RedisConfig = &redis{ + Network: "tcp", Server: "", Password: "", DB: "0", @@ -65,3 +66,7 @@ var SSLConfig = &ssl{ CertPath: "", KeyPath: "", } + +var UnixConfig = &unix{ + Listen: "", +}