diff --git a/.gitignore b/.gitignore index 7adcef09..5f49ff70 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ output coverage modules.txt *.log -test *_test.go +*.json +*.sh diff --git a/core/config/app_settings.go b/core/config/app_settings.go index 23dd7bb5..f547e1b1 100644 --- a/core/config/app_settings.go +++ b/core/config/app_settings.go @@ -24,15 +24,15 @@ func GetInternalAppSettingsByEnv() *AppSettings { return NewInternalAppSettings(appID, appSecret, verificationToken, encryptKey) } -func NewISVAppSettings(appID, appSecret, verificationToken, eventEncryptKey string) *AppSettings { - return newAppSettings(constants.AppTypeISV, appID, appSecret, verificationToken, eventEncryptKey) +func NewISVAppSettings(appID, appSecret, verificationToken, encryptKey string) *AppSettings { + return newAppSettings(constants.AppTypeISV, appID, appSecret, verificationToken, encryptKey) } -func NewInternalAppSettings(appID, appSecret, verificationToken, eventEncryptKey string) *AppSettings { - return newAppSettings(constants.AppTypeInternal, appID, appSecret, verificationToken, eventEncryptKey) +func NewInternalAppSettings(appID, appSecret, verificationToken, encryptKey string) *AppSettings { + return newAppSettings(constants.AppTypeInternal, appID, appSecret, verificationToken, encryptKey) } -func newAppSettings(appType constants.AppType, appID, appSecret, verificationToken, eventEncryptKey string) *AppSettings { +func newAppSettings(appType constants.AppType, appID, appSecret, verificationToken, encryptKey string) *AppSettings { if appID == "" || appSecret == "" { panic("appID or appSecret is empty") } @@ -41,7 +41,7 @@ func newAppSettings(appType constants.AppType, appID, appSecret, verificationTok AppID: appID, AppSecret: appSecret, VerificationToken: verificationToken, - EncryptKey: eventEncryptKey, + EncryptKey: encryptKey, } } diff --git a/core/test/config.go b/core/test/config.go new file mode 100644 index 00000000..f8ee8107 --- /dev/null +++ b/core/test/config.go @@ -0,0 +1,44 @@ +package test + +import ( + "github.com/larksuite/oapi-sdk-go/core/config" + "github.com/larksuite/oapi-sdk-go/core/constants" + "os" + "strings" +) + +func domainFeiShu(env string) string { + return os.Getenv(env + "_FEISHU_DOMAIN") +} + +func getISVAppSettings(env string) *config.AppSettings { + appID, appSecret, verificationToken, encryptKey := os.Getenv(env+"_ISV_APP_ID"), + os.Getenv(env+"_ISV_APP_SECRET"), os.Getenv(env+"_ISV_VERIFICATION_TOKEN"), os.Getenv(env+"_ISV_ENCRYPT_KEY") + return config.NewISVAppSettings(appID, appSecret, verificationToken, encryptKey) +} + +func getInternalAppSettings(env string) *config.AppSettings { + appID, appSecret, verificationToken, encryptKey := os.Getenv(env+"_INTERNAL_APP_ID"), + os.Getenv(env+"_INTERNAL_APP_SECRET"), os.Getenv(env+"_INTERNAL_VERIFICATION_TOKEN"), os.Getenv(env+"_INTERNAL_ENCRYPT_KEY") + return config.NewInternalAppSettings(appID, appSecret, verificationToken, encryptKey) +} + +func GetISVConf(env string) *config.Config { + env = strings.ToUpper(env) + return config.NewTestConfig(getDomain(env), getISVAppSettings(env)) +} + +func GetInternalConf(env string) *config.Config { + env = strings.ToUpper(env) + return config.NewTestConfig(getDomain(env), getInternalAppSettings(env)) +} + +func getDomain(env string) constants.Domain { + if env != "STAGING" && env != "PRE" && env != "ONLINE" { + panic("env must in [staging, pre, online]") + } + if env == "ONLINE" { + return constants.DomainFeiShu + } + return constants.Domain(domainFeiShu(env)) +} diff --git a/core/test/test_data.go b/core/test/test_data.go deleted file mode 100644 index 1e47bfcf..00000000 --- a/core/test/test_data.go +++ /dev/null @@ -1,35 +0,0 @@ -package test - -import ( - "github.com/larksuite/oapi-sdk-go/core/config" - "github.com/larksuite/oapi-sdk-go/core/constants" - "os" -) - -func domainFeiShuStaging() string { - return os.Getenv("DomainFeiShuStaging") -} - -func getISVAppSettings() *config.AppSettings { - appID, appSecret, verificationToken, eventEncryptKey := os.Getenv("ISVAppID"), - os.Getenv("ISVAppSecret"), os.Getenv("ISVVerificationToken"), os.Getenv("ISVEventEncryptKey") - return config.NewISVAppSettings(appID, appSecret, verificationToken, eventEncryptKey) -} - -func GetStagingISVConf() *config.Config { - return config.NewTestConfig(constants.Domain(domainFeiShuStaging()), getISVAppSettings()) -} - -func GetStagingInternalConf() *config.Config { - return config.NewTestConfig(constants.Domain(domainFeiShuStaging()), getInternalAppSettings()) -} - -func getInternalAppSettings() *config.AppSettings { - appID, appSecret, verificationToken, eventEncryptKey := os.Getenv("InternalAppID"), - os.Getenv("InternalAppSecret"), os.Getenv("InternalVerificationToken"), os.Getenv("InternalEventEncryptKey") - return config.NewInternalAppSettings(appID, appSecret, verificationToken, eventEncryptKey) -} - -func GetOnlineInternalConf() *config.Config { - return config.NewTestConfig(constants.DomainFeiShu, getInternalAppSettings()) -} diff --git a/sample/api/api.go b/sample/api/api.go index dcf5e71e..ff413fce 100644 --- a/sample/api/api.go +++ b/sample/api/api.go @@ -14,7 +14,7 @@ import ( "os" ) -var conf = test.GetOnlineInternalConf() +var conf = test.GetInternalConf("online") func main() { testSendMessage() diff --git a/sample/card/gin.go b/sample/card/gin.go index c8b61de4..8f1406c8 100644 --- a/sample/card/gin.go +++ b/sample/card/gin.go @@ -14,7 +14,7 @@ import ( func main() { - conf := test.GetOnlineInternalConf() + var conf = test.GetInternalConf("online") card.SetHandler(conf, func(coreCtx *core.Context, card *model.Card) (interface{}, error) { fmt.Println(coreCtx.GetRequestID()) diff --git a/sample/card/http_server.go b/sample/card/http_server.go index eb42712e..1f231d37 100644 --- a/sample/card/http_server.go +++ b/sample/card/http_server.go @@ -13,7 +13,7 @@ import ( func main() { - conf := test.GetOnlineInternalConf() + var conf = test.GetInternalConf("online") card.SetHandler(conf, func(ctx *core.Context, card *model.Card) (interface{}, error) { fmt.Println(ctx.GetRequestID()) diff --git a/sample/event/gin.go b/sample/event/gin.go index 094c4fe8..19c91d64 100644 --- a/sample/event/gin.go +++ b/sample/event/gin.go @@ -14,7 +14,7 @@ import ( func main() { - conf := test.GetStagingISVConf() + var conf = test.GetISVConf("online") application.SetAppOpenEventHandler(conf, func(ctx *core.Context, appOpenEvent *application.AppOpenEvent) error { fmt.Println(ctx.GetRequestID()) diff --git a/sample/event/http_server.go b/sample/event/http_server.go index 8d2faf1a..945cefb9 100644 --- a/sample/event/http_server.go +++ b/sample/event/http_server.go @@ -13,7 +13,7 @@ import ( func main() { - conf := test.GetStagingISVConf() + var conf = test.GetISVConf("online") application.SetAppOpenEventHandler(conf, func(coreCtx *core.Context, appOpenEvent *application.AppOpenEvent) error { fmt.Println(coreCtx.GetRequestID())