From 6738df8913ec882f134eebb7189008c54ffe21e6 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sat, 10 Dec 2022 17:40:35 +0800 Subject: [PATCH 1/2] update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index abbc76d0..5c196532 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ PaoPao主要由以下优秀的开源项目/工具构建 * Redis * Zinc -\* Zinc是一款轻量级全文搜索引擎,可以查阅 安装 +> Zinc是一款轻量级全文搜索引擎,可以查阅 安装 以上环境版本为PaoPao官方的开发版本,仅供参考,其他版本的环境未进行充分测试 @@ -498,7 +498,7 @@ jc/alimy 代码结构比较简单,很方便扩展,开发文档请参阅[docs](docs '开发文档'). ## 👯‍♀️ 贡献 -paopao-ce 是一个利用 *业余时间* 本着 **"Just for fun just do it."** 的心态 *持续有序* **开发/优化/维护**的开源项目,没有KPI考核、没有Roadmap进度压力、没有技术支持日程安排,或许有些许不足之处,但是重在精神可嘉。 借用网络中的话 **"F\*k talk, f\*k of tech innovation, Shut up and give me your code."** 一切都因更好的体验,一切都是为了爱好,一切都在代码里;期待老铁们加入,一起开发、一起折腾、一起快乐。 +paopao-ce 是一个利用 *业余时间* 本着 **"Just for fun just do it."** 的心态 *持续有序* **开发/优化/维护**的开源项目,没有KPI考核、没有Roadmap进度压力、没有技术支持日程安排,或许有些许不足之处,但是重在精神可嘉。 借用网络中的话 **"F\*k talk, f\*k of tech innovation, Shut up and show me your code."** 一切都因更好的体验,一切都是为了爱好,一切都在代码里;期待老铁们加入,一起开发、一起折腾、一起快乐。 喜欢的朋友记得给个Star,欢迎贡献PR。 From 72136a22666583cc4a80ae219a378c2c4d2f7098 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Sun, 11 Dec 2022 11:44:42 +0800 Subject: [PATCH 2/2] use github.com/alimy/cfg as features configure help library --- go.mod | 1 + go.sum | 2 + internal/conf/conf.go | 4 +- internal/conf/db.go | 2 +- internal/conf/logger.go | 2 +- internal/dao/dao.go | 2 +- internal/dao/jinzhu/jinzhu.go | 2 +- internal/dao/storage/storage.go | 2 +- internal/middleware/priv.go | 2 +- internal/migration/migration.go | 2 +- internal/migration/migration_embed.go | 2 +- internal/routers/api/api.go | 2 +- internal/routers/docs_embed.go | 2 +- internal/routers/router.go | 2 +- internal/service/service.go | 2 +- pkg/cfg/cfg.go | 41 ------- pkg/cfg/cfg_test.go | 78 -------------- pkg/cfg/feature.go | 149 -------------------------- pkg/cfg/feature_test.go | 114 -------------------- 19 files changed, 17 insertions(+), 396 deletions(-) delete mode 100644 pkg/cfg/cfg.go delete mode 100644 pkg/cfg/cfg_test.go delete mode 100644 pkg/cfg/feature.go delete mode 100644 pkg/cfg/feature_test.go diff --git a/go.mod b/go.mod index 8f4c9435..0215af0f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/afocus/captcha v0.0.0-20191010092841-4bd1f21c8868 + github.com/alimy/cfg v0.1.0 github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible github.com/allegro/bigcache/v3 v3.0.2 github.com/bytedance/sonic v1.5.0 diff --git a/go.sum b/go.sum index 9221a97a..7262bc9a 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= github.com/alexflint/go-filemutex v1.1.0/go.mod h1:7P4iRhttt/nUvUOrYIhcpMzv2G6CY9UnI16Z+UJqRyk= +github.com/alimy/cfg v0.1.0 h1:J7KDLEoVIjEBMGlQJb2ljad/d49gJ4Tsl2ogZ9XNhaY= +github.com/alimy/cfg v0.1.0/go.mod h1:rOxbasTH2srl6StAjNF5Vyi8bfrdkl3fLGmOYtSw81c= github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible h1:9gWa46nstkJ9miBReJcN8Gq34cBFbzSpQZVVT9N09TM= github.com/aliyun/aliyun-oss-go-sdk v2.2.2+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 97512264..3ede82d6 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -5,7 +5,7 @@ import ( "sync" "time" - "github.com/rocboss/paopao-ce/pkg/cfg" + "github.com/alimy/cfg" ) var ( @@ -48,7 +48,7 @@ func setupSetting(suite []string, noDefault bool) error { // initialize features configure ss, kv := setting.featuresInfoFrom("Features") - cfg.Initialize(ss, kv) + cfg.Initial(ss, kv) if len(suite) > 0 { cfg.Use(suite, noDefault) } diff --git a/internal/conf/db.go b/internal/conf/db.go index ef4f7c92..6ad7cd6e 100644 --- a/internal/conf/db.go +++ b/internal/conf/db.go @@ -4,8 +4,8 @@ import ( "sync" "time" + "github.com/alimy/cfg" "github.com/go-redis/redis/v8" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" "gorm.io/driver/mysql" "gorm.io/driver/postgres" diff --git a/internal/conf/logger.go b/internal/conf/logger.go index c65612dc..40b3500b 100644 --- a/internal/conf/logger.go +++ b/internal/conf/logger.go @@ -3,7 +3,7 @@ package conf import ( "io" - "github.com/rocboss/paopao-ce/pkg/cfg" + "github.com/alimy/cfg" "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" ) diff --git a/internal/dao/dao.go b/internal/dao/dao.go index 6f4a776a..f0244d3b 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -3,13 +3,13 @@ package dao import ( "sync" + "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao/jinzhu" "github.com/rocboss/paopao-ce/internal/dao/sakila" "github.com/rocboss/paopao-ce/internal/dao/search" "github.com/rocboss/paopao-ce/internal/dao/slonik" "github.com/rocboss/paopao-ce/internal/dao/storage" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" ) diff --git a/internal/dao/jinzhu/jinzhu.go b/internal/dao/jinzhu/jinzhu.go index 9f5e1c66..b3eb550f 100644 --- a/internal/dao/jinzhu/jinzhu.go +++ b/internal/dao/jinzhu/jinzhu.go @@ -6,11 +6,11 @@ package jinzhu import ( "github.com/Masterminds/semver/v3" + "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao/cache" "github.com/rocboss/paopao-ce/internal/dao/security" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" ) diff --git a/internal/dao/storage/storage.go b/internal/dao/storage/storage.go index 8e124047..c25d2907 100644 --- a/internal/dao/storage/storage.go +++ b/internal/dao/storage/storage.go @@ -8,13 +8,13 @@ import ( "strconv" "time" + "github.com/alimy/cfg" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" "github.com/tencentyun/cos-go-sdk-v5" ) diff --git a/internal/middleware/priv.go b/internal/middleware/priv.go index fe31b008..e479c6b1 100644 --- a/internal/middleware/priv.go +++ b/internal/middleware/priv.go @@ -1,10 +1,10 @@ package middleware import ( + "github.com/alimy/cfg" "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/pkg/app" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/rocboss/paopao-ce/pkg/errcode" ) diff --git a/internal/migration/migration.go b/internal/migration/migration.go index 44c16fb4..9563f803 100644 --- a/internal/migration/migration.go +++ b/internal/migration/migration.go @@ -4,7 +4,7 @@ package migration import ( - "github.com/rocboss/paopao-ce/pkg/cfg" + "github.com/alimy/cfg" "github.com/sirupsen/logrus" ) diff --git a/internal/migration/migration_embed.go b/internal/migration/migration_embed.go index 84952538..c6a0ae34 100644 --- a/internal/migration/migration_embed.go +++ b/internal/migration/migration_embed.go @@ -6,6 +6,7 @@ package migration import ( "database/sql" + "github.com/alimy/cfg" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database" "github.com/golang-migrate/migrate/v4/database/mysql" @@ -14,7 +15,6 @@ import ( "github.com/golang-migrate/migrate/v4/source" "github.com/golang-migrate/migrate/v4/source/iofs" "github.com/rocboss/paopao-ce/internal/conf" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/rocboss/paopao-ce/scripts/migration" "github.com/sirupsen/logrus" ) diff --git a/internal/routers/api/api.go b/internal/routers/api/api.go index c722ac88..17d56d7c 100644 --- a/internal/routers/api/api.go +++ b/internal/routers/api/api.go @@ -1,10 +1,10 @@ package api import ( + "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" "github.com/smartwalle/alipay/v3" ) diff --git a/internal/routers/docs_embed.go b/internal/routers/docs_embed.go index e9c0d17d..443f5c0a 100644 --- a/internal/routers/docs_embed.go +++ b/internal/routers/docs_embed.go @@ -4,9 +4,9 @@ package routers import ( + "github.com/alimy/cfg" "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/docs/openapi" - "github.com/rocboss/paopao-ce/pkg/cfg" ) // registerDocs register docs asset route diff --git a/internal/routers/router.go b/internal/routers/router.go index 1a2b57f6..354b4abe 100644 --- a/internal/routers/router.go +++ b/internal/routers/router.go @@ -4,12 +4,12 @@ import ( "net/http" "path/filepath" + "github.com/alimy/cfg" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/middleware" "github.com/rocboss/paopao-ce/internal/routers/api" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" ) diff --git a/internal/service/service.go b/internal/service/service.go index 9d7d4c01..bb70e9f8 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,10 +1,10 @@ package service import ( + "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/internal/model" - "github.com/rocboss/paopao-ce/pkg/cfg" "github.com/sirupsen/logrus" ) diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go deleted file mode 100644 index 019deba9..00000000 --- a/pkg/cfg/cfg.go +++ /dev/null @@ -1,41 +0,0 @@ -package cfg - -var ( - _features = newEmptyFeatures() - - // Use alias of Features.Use func - Use = _features.Use - - // UseDeafult alias of Features.UseDefault func - UseDefault = _features.UseDefault - - // As alias of Features.Cfg func - As = _features.Cfg - - // If alias of Features.CfgIf func - If = _features.CfgIf - - // In alias of Features.CfgIn func - In = _features.CfgIn - - // Be alias of Feaures.CfgBe func - Be = _features.CfgBe - - // Not alias of Features.CfgNot func - Not = _features.CfgNot -) - -// Initialize initialize features in cfg pkg -func Initialize(suites map[string][]string, kv map[string]string) { - _features = NewFeatures(suites, kv) - { - // must re-assign variable below - Use = _features.Use - UseDefault = _features.UseDefault - As = _features.Cfg - If = _features.CfgIf - In = _features.CfgIn - Be = _features.CfgBe - Not = _features.CfgNot - } -} diff --git a/pkg/cfg/cfg_test.go b/pkg/cfg/cfg_test.go deleted file mode 100644 index 0fccbce2..00000000 --- a/pkg/cfg/cfg_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package cfg - -import ( - "testing" -) - -func TestCfg(t *testing.T) { - suites := map[string][]string{ - "default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"}, - "develop": {"Zinc", "MySQL", "AliOSS", "LogFile"}, - "slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"}, - } - kv := map[string]string{ - "sms": "SmsJuhe", - } - - Initialize(suites, kv) - UseDefault() - - if !If("Sms") { - t.Error(`want If("Sms") == true but not`) - } - - if v, exist := As("Sms"); exist && v != "SmsJuhe" { - t.Errorf(`want As("Sms") == "SmsJuhe", true but got: "%s", "%t"`, v, exist) - } - - matched := false - Be("Alipay", func() { - matched = true - }) - if !matched { - t.Error(`want Be("Alipay", ...) matched but not`) - } - - matched = false - Not("LogFile", func() { - matched = true - }) - if !matched { - t.Error(`want Not("LogFile", ...) matched but not`) - } - - var m1, m2, m3, m4 bool - In(Actions{ - "Sms": func() { - m1 = true - }, - "Alipay": func() { - m2 = true - }, - "Meili": func() { - m4 = true - }, - }, func() { - m3 = true - }) - if !m1 || !m2 || m3 || m4 { - t.Errorf(`In("Sms", "Alipay", "Meili", ...) not correct -> m1: %t m2:%t m3:%t m4:%t`, m1, m2, m3, m4) - } - - m1 = false - m2 = false - m3 = false - In(Actions{ - "LogFile": func() { - m1 = true - }, - "Meili": func() { - m2 = true - }, - }, func() { - m3 = true - }) - if m1 || m2 || !m3 { - t.Errorf(`In("Zinc", "MySQL", ...) not correct -> m1: %t m2:%t m3:%t`, m1, m2, m3) - } -} diff --git a/pkg/cfg/feature.go b/pkg/cfg/feature.go deleted file mode 100644 index a04d07ca..00000000 --- a/pkg/cfg/feature.go +++ /dev/null @@ -1,149 +0,0 @@ -package cfg - -import ( - "strings" - - "github.com/rocboss/paopao-ce/pkg/types" -) - -// Features fetures info struct -type Features struct { - kv map[string]string - suites map[string][]string - features map[string]string -} - -// Actions feature-func map alias type -type Actions map[string]types.Fn - -// NewFeatures create new Features instance -func NewFeatures(suites map[string][]string, kv map[string]string) *Features { - f := newEmptyFeatures() - for k, v := range suites { - if len(k) > 0 { - for i := 0; i < len(v); i++ { - // ignore empty string - if len(v[i]) == 0 { - lastIdx := len(v) - 1 - v[i] = v[lastIdx] - v = v[:lastIdx] - i-- - } - } - if len(v) > 0 { - f.suites[k] = v - } - } - } - for k, v := range kv { - if len(k) > 0 && len(v) > 0 { - f.kv[k] = v - } - } - f.UseDefault() - return f -} - -func newEmptyFeatures() *Features { - return &Features{ - suites: make(map[string][]string), - kv: make(map[string]string), - features: make(map[string]string), - } -} - -// UseDefault use default suite for features -func (f *Features) UseDefault() { - f.Use([]string{"default"}, true) -} - -// Use use custom suite for features -func (f *Features) Use(suite []string, noDefault bool) { - if noDefault && len(f.features) != 0 { - f.features = make(map[string]string) - } - features := f.flatFeatures(suite) - for _, feature := range features { - if len(feature) == 0 { - continue - } - f.features[feature] = f.kv[feature] - } -} - -func (f *Features) flatFeatures(suite []string) []string { - features := make([]string, 0, len(suite)+10) - for s := suite[:]; len(s) > 0; s = s[:len(s)-1] { - item := strings.TrimSpace(strings.ToLower(s[0])) - if len(item) > 0 { - if items, exist := f.suites[item]; exist { - s = append(s, items...) - } - features = append(features, item) - } - s[0] = s[len(s)-1] - } - return features -} - -// Cfg get value by key if exist -func (f *Features) Cfg(key string) (string, bool) { - key = strings.ToLower(key) - value, exist := f.features[key] - return value, exist -} - -// CfgIf check expression is true. if expression just have a string like -// `Sms` is mean `Sms` whether define in suite feature settings. expression like -// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value -// is `SmsJuhe` -func (f *Features) CfgIf(expression string) bool { - kv := strings.Split(expression, "=") - key := strings.Trim(strings.ToLower(kv[0]), " ") - v, ok := f.features[key] - if len(kv) == 2 && ok && strings.Trim(kv[1], " ") == v { - return true - } else if len(kv) == 1 && ok { - return true - } - return false -} - -// CfgIn range actions to check item's expression is true then do the handle, defFn will handle -// if all items are not matched, -func (f *Features) CfgIn(actions Actions, defAct ...types.Fn) { - itemMatched := false - for expression, handle := range actions { - if f.CfgIf(expression) && handle != nil { - handle() - itemMatched = true - } - } - if !itemMatched && len(defAct) > 0 { - for _, handle := range defAct { - if handle != nil { - handle() - } - } - } -} - -// CfgBe check expression is true then do the handle. if expression just have a string like -// `Sms` is mean `Sms` whether defined in suite feature settings. expression like -// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value -// is `SmsJuhe` -func (f *Features) CfgBe(expression string, handle types.Fn) { - if f.CfgIf(expression) && handle != nil { - handle() - } -} - -// CfgNot check expression is not true then do the handle. if expression just have a string like -// `Sms` is mean `Sms` whether defined in suite feature settings. expression like -// `Sms = SmsJuhe` is mean whether `Sms` define in suite feature settings and value -// is `SmsJuhe` -func (f *Features) CfgNot(expression string, handle func()) { - if !f.CfgIf(expression) { - handle() - } -} diff --git a/pkg/cfg/feature_test.go b/pkg/cfg/feature_test.go deleted file mode 100644 index 98e01ec7..00000000 --- a/pkg/cfg/feature_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package cfg - -import ( - "testing" -) - -func TestUseDefault(t *testing.T) { - suites := map[string][]string{ - "default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"}, - "develop": {"Zinc", "MySQL", "AliOSS", "LogFile"}, - "slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"}, - } - kv := map[string]string{ - "sms": "SmsJuhe", - } - f := NewFeatures(suites, kv) - for _, data := range []struct { - key string - expect string - exist bool - }{ - {"Sms", "SmsJuhe", true}, - {"Alipay", "", true}, - {"Zinc", "", true}, - {"Redis", "", true}, - {"Database", "", false}, - } { - if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect { - t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok) - } - } - for exp, res := range map[string]bool{ - "Sms": true, - "Sms = SmsJuhe": true, - "SmsJuhe": false, - "default": true, - } { - if ok := f.CfgIf(exp); res != ok { - t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok) - } - } -} - -func TestUse(t *testing.T) { - suites := map[string][]string{ - "default": {"Sms", "Alipay", "Zinc", "MySQL", "Redis", "AliOSS", "LogZinc"}, - "develop": {"Zinc", "MySQL", "AliOSS", "LogFile"}, - "slim": {"Zinc", "MySQL", "Redis", "AliOSS", "LogFile"}, - } - kv := map[string]string{ - "sms": "SmsJuhe", - } - f := NewFeatures(suites, kv) - - f.Use([]string{"develop"}, true) - for _, data := range []struct { - key string - expect string - exist bool - }{ - {"Sms", "", false}, - {"Alipay", "", false}, - {"Zinc", "", true}, - {"Redis", "", false}, - {"Database", "", false}, - } { - if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect { - t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok) - } - } - for exp, res := range map[string]bool{ - "Sms": false, - "Sms = SmsJuhe": false, - "SmsJuhe": false, - "default": false, - "develop": true, - } { - if ok := f.CfgIf(exp); res != ok { - t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok) - } - } - - f.UseDefault() - f.Use([]string{"slim", "", "demo"}, false) - for _, data := range []struct { - key string - expect string - exist bool - }{ - {"Sms", "SmsJuhe", true}, - {"Alipay", "", true}, - {"Zinc", "", true}, - {"Redis", "", true}, - {"Database", "", false}, - {"demo", "", true}, - } { - if v, ok := f.Cfg(data.key); ok != data.exist || v != data.expect { - t.Errorf("key: %s expect: %s exist: %t got v: %s ok: %t", data.key, data.expect, data.exist, v, ok) - } - } - for exp, res := range map[string]bool{ - "Sms": true, - "Sms = SmsJuhe": true, - "SmsJuhe": false, - "default": true, - "develop": false, - "slim": true, - "demo": true, - } { - if ok := f.CfgIf(exp); res != ok { - t.Errorf("CfgIf(%s) want %t got %t", exp, res, ok) - } - } -}