From 1ab3e04cba4d6bd9464ba1944e177bff18596231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=AE=88=E8=B6=8A?= Date: Mon, 21 Mar 2022 11:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Emongo.dbUrl=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=20(#174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 当dbUri值不为空则直接使用该值 * 新增支持ali oss Co-authored-by: mashouyue's m1max --- cmd/open_im_api/main.go | 15 ++-- config/config.yaml | 11 +++ deploy/config.example.yaml | 1 + go.mod | 2 + go.sum | 7 +- internal/api/third/ali_oss_credential.go | 95 ++++++++++++++++++++++++ pkg/base_info/oss_api_struct.go | 22 ++++++ pkg/common/config/config.go | 28 +++++-- pkg/common/db/model.go | 11 ++- 9 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 internal/api/third/ali_oss_credential.go create mode 100644 pkg/base_info/oss_api_struct.go diff --git a/cmd/open_im_api/main.go b/cmd/open_im_api/main.go index 551a37a0e..eff8710e0 100644 --- a/cmd/open_im_api/main.go +++ b/cmd/open_im_api/main.go @@ -59,13 +59,13 @@ func main() { groupRouterGroup.POST("/transfer_group", group.TransferGroupOwner) //1 groupRouterGroup.POST("/get_recv_group_applicationList", group.GetRecvGroupApplicationList) //1 groupRouterGroup.POST("/get_user_req_group_applicationList", group.GetUserReqGroupApplicationList) - groupRouterGroup.POST("/get_groups_info", group.GetGroupsInfo) //1 - groupRouterGroup.POST("/kick_group", group.KickGroupMember) //1 - groupRouterGroup.POST("/get_group_member_list", group.GetGroupMemberList) //no use - groupRouterGroup.POST("/get_group_all_member_list", group.GetGroupAllMemberList) //1 - groupRouterGroup.POST("/get_group_members_info", group.GetGroupMembersInfo) //1 - groupRouterGroup.POST("/invite_user_to_group", group.InviteUserToGroup) //1 - groupRouterGroup.POST("/get_joined_group_list", group.GetJoinedGroupList) //1 + groupRouterGroup.POST("/get_groups_info", group.GetGroupsInfo) //1 + groupRouterGroup.POST("/kick_group", group.KickGroupMember) //1 + groupRouterGroup.POST("/get_group_member_list", group.GetGroupMemberList) //no use + groupRouterGroup.POST("/get_group_all_member_list", group.GetGroupAllMemberList) //1 + groupRouterGroup.POST("/get_group_members_info", group.GetGroupMembersInfo) //1 + groupRouterGroup.POST("/invite_user_to_group", group.InviteUserToGroup) //1 + groupRouterGroup.POST("/get_joined_group_list", group.GetJoinedGroupList) //1 } //certificate authRouterGroup := r.Group("/auth") @@ -77,6 +77,7 @@ func main() { thirdGroup := r.Group("/third") { thirdGroup.POST("/tencent_cloud_storage_credential", apiThird.TencentCloudStorageCredential) + thirdGroup.POST("/ali_oss_credential", apiThird.AliOSSCredential) thirdGroup.POST("/minio_storage_credential", apiThird.MinioStorageCredential) } //Message diff --git a/config/config.yaml b/config/config.yaml index 16445973d..e7c511930 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -19,6 +19,7 @@ mysql: dbMaxLifeTime: 120 mongo: + dbUri: ""#当dbUri值不为空则直接使用该值 dbAddress: [ 127.0.0.1:37017 ] #redis地址 目前仅支持单机,默认即可 dbDirect: false dbTimeout: 10 @@ -93,6 +94,16 @@ credential: #腾讯cos,发送图片、视频、文件时需要,请自行申 endpoint: http://127.0.0.1:9000 accessKeyID: user12345 secretAccessKey: key12345 + ali: # ali oss + regionID: "oss-cn-beijing" + accessKeyID: "" + accessKeySecret: "" + stsEndpoint: "sts.cn-beijing.aliyun.com" + ossEndpoint: "oss-cn-beijing.aliyuncs.com" + bucket: "bucket1" + finalHost: "http://bucket1.oss-cn-beijing.aliyuncs.com" + stsDurationSeconds: 3600 + OssRoleArn: "acs:ram::xxx:role/xxx" rpcport: #rpc服务端口 默认即可 diff --git a/deploy/config.example.yaml b/deploy/config.example.yaml index f0ae8314e..08d8858e4 100644 --- a/deploy/config.example.yaml +++ b/deploy/config.example.yaml @@ -19,6 +19,7 @@ mysql: dbMaxLifeTime: 120 mongo: + dbUri: ""#当dbUri值不为空则直接使用该值 dbAddress: [ openim_mongo:27017 ] dbDirect: false dbTimeout: 10 diff --git a/go.mod b/go.mod index 9fbbf033a..8bb682c49 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/Shopify/toxiproxy v2.1.4+incompatible // indirect github.com/alibabacloud-go/darabonba-openapi v0.1.11 github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8 + github.com/alibabacloud-go/sts-20150401 v1.1.0 github.com/alibabacloud-go/tea v1.1.17 github.com/antonfisher/nested-logrus-formatter v1.3.0 github.com/bwmarrin/snowflake v0.3.0 @@ -52,6 +53,7 @@ require ( google.golang.org/protobuf v1.27.1 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df + gopkg.in/ini.v1 v1.66.2 // indirect gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b sigs.k8s.io/yaml v1.2.0 // indirect diff --git a/go.sum b/go.sum index 2385d32f5..ab34c4c92 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,7 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWso github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alibabacloud-go/darabonba-openapi v0.1.7/go.mod h1:6FV1Bt1AItYIlC2rVopPTumrRNtkfPBmrPVAZ8v2bLk= github.com/alibabacloud-go/darabonba-openapi v0.1.11 h1:w59gtSA0s87p0U5NNG/N/PIHsRP3rtj7qCP9hx9+GL8= github.com/alibabacloud-go/darabonba-openapi v0.1.11/go.mod h1:MPJMxv7HYrFm5m9uOZWkDYsAWyZztEgnBRfk9Fg0eIU= github.com/alibabacloud-go/darabonba-string v1.0.0/go.mod h1:93cTfV3vuPhhEwGGpKKqhVW4jLe7tDpo3LUM0i0g6mA= @@ -59,8 +60,11 @@ github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8 h1:KXMiCg99Jx7B6V+DlRFbWw github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8/go.mod h1:8aL6tSyQIWJygF7W/Vqxdf/QDbN2S+u57k36bEA8hD8= github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q= github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE= +github.com/alibabacloud-go/openapi-util v0.0.8/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws= github.com/alibabacloud-go/openapi-util v0.0.9 h1:Z0DP4LFzkM/rW2nxOMiiLoQVZSeE3jVc5jrZ9Fd/UX0= github.com/alibabacloud-go/openapi-util v0.0.9/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws= +github.com/alibabacloud-go/sts-20150401 v1.1.0 h1:1yVyKz02ES6aKo3xVjmoPLBH1OAmmSqPkhKRdjEkmYs= +github.com/alibabacloud-go/sts-20150401 v1.1.0/go.mod h1:QW4O/c7Hp4krHYt+6xwnoG8EyZW3V9GYkl6EgIBmxJc= github.com/alibabacloud-go/tea v1.1.0/go.mod h1:IkGyUSX4Ba1V+k4pCtJUc6jDpZLFph9QMy2VUPTwukg= github.com/alibabacloud-go/tea v1.1.7/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4= github.com/alibabacloud-go/tea v1.1.8/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4= @@ -904,8 +908,9 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AW gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c= gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= +gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw= gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= diff --git a/internal/api/third/ali_oss_credential.go b/internal/api/third/ali_oss_credential.go new file mode 100644 index 000000000..c07cd5d22 --- /dev/null +++ b/internal/api/third/ali_oss_credential.go @@ -0,0 +1,95 @@ +package apiThird + +import ( + api "Open_IM/pkg/base_info" + "Open_IM/pkg/common/config" + "Open_IM/pkg/common/constant" + "Open_IM/pkg/common/log" + "Open_IM/pkg/common/token_verify" + "fmt" + openapi "github.com/alibabacloud-go/darabonba-openapi/client" + sts20150401 "github.com/alibabacloud-go/sts-20150401/client" + "github.com/alibabacloud-go/tea/tea" + "github.com/fatih/structs" + + //"github.com/fatih/structs" + "github.com/gin-gonic/gin" + "net/http" + "time" +) + +var stsClient *sts20150401.Client + +/** + * 使用AK&SK初始化账号Client + * @param accessKeyId + * @param accessKeySecret + * @return Client + * @throws Exception + */ +func getStsClient() *sts20150401.Client { + if stsClient != nil { + return stsClient + } + conf := &openapi.Config{ + // 您的AccessKey ID + AccessKeyId: tea.String(config.Config.Credential.Ali.AccessKeyID), + // 您的AccessKey Secret + AccessKeySecret: tea.String(config.Config.Credential.Ali.AccessKeySecret), + // Endpoint + Endpoint: tea.String(config.Config.Credential.Ali.StsEndpoint), + } + result, err := sts20150401.NewClient(conf) + if err != nil { + log.NewError("", "alists client初始化失败 ", err) + } + stsClient = result + return stsClient +} + +func AliOSSCredential(c *gin.Context) { + req := api.OSSCredentialReq{} + if err := c.BindJSON(&req); err != nil { + log.NewError("0", "BindJSON failed ", err.Error()) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()}) + return + } + ok, userID := token_verify.GetUserIDFromToken(c.Request.Header.Get("token")) + if !ok { + log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token")) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"}) + return + } + log.NewInfo(req.OperationID, "AliOSSCredential args ", userID) + + stsResp, err := getStsClient().AssumeRole(&sts20150401.AssumeRoleRequest{ + DurationSeconds: tea.Int64(config.Config.Credential.Ali.StsDurationSeconds), + Policy: nil, + RoleArn: tea.String(config.Config.Credential.Ali.OssRoleArn), + RoleSessionName: tea.String(fmt.Sprintf("%s-%d", userID, time.Now().Unix())), + }) + + resp := api.OSSCredentialResp{} + if err != nil { + resp.ErrCode = constant.ErrTencentCredential.ErrCode + resp.ErrMsg = err.Error() + } else { + resp = api.OSSCredentialResp{ + CommResp: api.CommResp{}, + OssData: api.OSSCredentialRespData{ + Endpoint: config.Config.Credential.Ali.OssEndpoint, + AccessKeyId: *stsResp.Body.Credentials.AccessKeyId, + AccessKeySecret: *stsResp.Body.Credentials.AccessKeySecret, + Token: *stsResp.Body.Credentials.SecurityToken, + Bucket: config.Config.Credential.Ali.Bucket, + FinalHost: config.Config.Credential.Ali.FinalHost, + }, + Data: nil, + } + } + + resp.Data = structs.Map(&resp.OssData) + log.NewInfo(req.OperationID, "AliOSSCredential return ", resp) + + c.JSON(http.StatusOK, resp) +} diff --git a/pkg/base_info/oss_api_struct.go b/pkg/base_info/oss_api_struct.go new file mode 100644 index 000000000..621d18820 --- /dev/null +++ b/pkg/base_info/oss_api_struct.go @@ -0,0 +1,22 @@ +package base_info + +type OSSCredentialReq struct { + OperationID string `json:"operationID"` + Filename string `json:"filename"` + FileType string `json:"file_type"` +} + +type OSSCredentialRespData struct { + Endpoint string `json:"endpoint"` + AccessKeyId string `json:"access_key_id"` + AccessKeySecret string `json:"access_key_secret"` + Token string `json:"token"` + Bucket string `json:"bucket"` + FinalHost string `json:"final_host"` +} + +type OSSCredentialResp struct { + CommResp + OssData OSSCredentialRespData `json:"-"` + Data map[string]interface{} `json:"data"` +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 282440672..05ff23020 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -20,8 +20,8 @@ var ( var Config config type callBackConfig struct { - Enable bool `yaml:"enable"` - CallbackTimeOut int `yaml:"callbackTimeOut"` + Enable bool `yaml:"enable"` + CallbackTimeOut int `yaml:"callbackTimeOut"` CallbackFailedContinue bool `CallbackFailedContinue` } @@ -31,7 +31,7 @@ type config struct { Api struct { GinPort []int `yaml:"openImApiPort"` } - CmsApi struct{ + CmsApi struct { GinPort []int `yaml:"openImCmsApiPort"` } Sdk struct { @@ -45,6 +45,17 @@ type config struct { SecretID string `yaml:"secretID"` SecretKey string `yaml:"secretKey"` } + Ali struct { + RegionID string `yaml:"regionID"` + AccessKeyID string `yaml:"accessKeyID"` + AccessKeySecret string `yaml:"accessKeySecret"` + StsEndpoint string `yaml:"stsEndpoint"` + OssEndpoint string `yaml:"ossEndpoint"` + Bucket string `yaml:"bucket"` + FinalHost string `yaml:"finalHost"` + StsDurationSeconds int64 `yaml:"stsDurationSeconds"` + OssRoleArn string `yaml:"OssRoleArn"` + } Minio struct { Bucket string `yaml:"bucket"` Location string `yaml:"location"` @@ -66,6 +77,7 @@ type config struct { DBMaxLifeTime int `yaml:"dbMaxLifeTime"` } Mongo struct { + DBUri string `yaml:"dbUri"` // 当dbUri值不为空则直接使用该值 DBAddress []string `yaml:"dbAddress"` DBDirect bool `yaml:"dbDirect"` DBTimeout int `yaml:"dbTimeout"` @@ -182,12 +194,12 @@ type config struct { } Callback struct { - CallbackUrl string `yaml:"callbackUrl"` + CallbackUrl string `yaml:"callbackUrl"` CallbackBeforeSendSingleMsg callBackConfig `yaml:"callbackbeforeSendSingleMsg"` - CallbackAfterSendSingleMsg callBackConfig `yaml:"callbackAfterSendSingleMsg"` - CallbackBeforeSendGroupMsg callBackConfig `yaml:"callbackBeforeSendGroupMsg"` - CallbackAfterSendGroupMsg callBackConfig `yaml:"callbackAfterSendGroupMsg"` - CallbackWordFilter callBackConfig `yaml:"callbackWordFilter"` + CallbackAfterSendSingleMsg callBackConfig `yaml:"callbackAfterSendSingleMsg"` + CallbackBeforeSendGroupMsg callBackConfig `yaml:"callbackBeforeSendGroupMsg"` + CallbackAfterSendGroupMsg callBackConfig `yaml:"callbackAfterSendGroupMsg"` + CallbackWordFilter callBackConfig `yaml:"callbackWordFilter"` } `yaml:"callback"` Notification struct { ///////////////////////group///////////////////////////// diff --git a/pkg/common/db/model.go b/pkg/common/db/model.go index 0576e6286..e7b03cbd5 100644 --- a/pkg/common/db/model.go +++ b/pkg/common/db/model.go @@ -42,9 +42,14 @@ func init() { // mongo init // "mongodb://sysop:moon@localhost/records" uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority" - uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d", - config.Config.Mongo.DBAddress[0],config.Config.Mongo.DBDatabase, - config.Config.Mongo.DBMaxPoolSize) + if config.Config.Mongo.DBUri != "" { + // example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize + uri = config.Config.Mongo.DBUri + } else { + uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d", + config.Config.Mongo.DBAddress[0], config.Config.Mongo.DBDatabase, + config.Config.Mongo.DBMaxPoolSize) + } mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) if err != nil{