commit
06382d88d5
@ -1,2 +1,9 @@
|
||||
bin
|
||||
logs
|
||||
.devcontainer
|
||||
components
|
||||
logs
|
||||
out-test
|
||||
.github
|
||||
|
||||
tmp
|
||||
|
@ -0,0 +1,44 @@
|
||||
|
||||
apps:
|
||||
- name: open_im_api
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/open_im_api"
|
||||
env:
|
||||
OPEN_IM_SWAGGER_API: true
|
||||
watch: ["docs", "cmd", "internal", "pkg"]
|
||||
|
||||
- name: open_im_auth
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/rpc/open_im_auth"
|
||||
|
||||
- name: open_im_offline_msg
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/rpc/open_im_msg"
|
||||
|
||||
- name: open_im_friend
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/rpc/open_im_friend"
|
||||
|
||||
- name: open_im_group
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/rpc/open_im_group"
|
||||
|
||||
- name: open_im_user
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/rpc/open_im_user"
|
||||
|
||||
- name: open_im_push
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/open_im_push"
|
||||
|
||||
- name: open_im_timed_task
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/open_im_timer_task"
|
||||
|
||||
- name: open_im_msg_transfer
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/open_im_msg_transfer"
|
||||
|
||||
- name: open_im_msg_gateway
|
||||
script: "go run main.go"
|
||||
cwd: "cmd/open_im_msg_gateway"
|
@ -0,0 +1 @@
|
||||
Subproject commit a85c10dbffbb797b5b2091e209ff67a5534b9bfc
|
@ -1,42 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/db"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for {
|
||||
fmt.Println("start delete mongodb expired record")
|
||||
timeUnixBegin := time.Now().Unix()
|
||||
count, _ := db.DB.MgoUserCount()
|
||||
fmt.Println("mongodb record count: ", count)
|
||||
for i := 0; i < count; i++ {
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
uid, _ := db.DB.MgoSkipUID(i)
|
||||
fmt.Println("operate uid: ", uid)
|
||||
err := db.DB.DelUserChat(uid)
|
||||
if err != nil {
|
||||
fmt.Println("operate uid failed: ", uid, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
timeUnixEnd := time.Now().Unix()
|
||||
costTime := timeUnixEnd - timeUnixBegin
|
||||
if costTime > int64(config.Config.Mongo.DBRetainChatRecords*24*3600) {
|
||||
continue
|
||||
} else {
|
||||
sleepTime := 0
|
||||
if int64(config.Config.Mongo.DBRetainChatRecords*24*3600)-costTime > 24*3600 {
|
||||
sleepTime = 24 * 3600
|
||||
} else {
|
||||
sleepTime = config.Config.Mongo.DBRetainChatRecords*24*3600 - int(costTime)
|
||||
}
|
||||
fmt.Println("sleep: ", sleepTime)
|
||||
time.Sleep(time.Duration(sleepTime) * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
.PHONY: all build run gotool install clean help
|
||||
|
||||
BINARY_NAME=open_im_timer_task
|
||||
BIN_DIR=../../bin/
|
||||
|
||||
all: gotool build
|
||||
|
||||
build:
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s"
|
||||
|
||||
run:
|
||||
@go run ./
|
||||
|
||||
gotool:
|
||||
go fmt ./
|
||||
go vet ./
|
||||
|
||||
install:
|
||||
make build
|
||||
mv ${BINARY_NAME} ${BIN_DIR}
|
||||
|
||||
clean:
|
||||
@if [ -f ${BINARY_NAME} ] ; then rm ${BINARY_NAME} ; fi
|
||||
|
||||
|
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
commonDB "Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/pkg/common/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.NewPrivateLog("timer")
|
||||
//for {
|
||||
// fmt.Println("start delete mongodb expired record")
|
||||
// timeUnixBegin := time.Now().Unix()
|
||||
// count, _ := db.DB.MgoUserCount()
|
||||
// fmt.Println("mongodb record count: ", count)
|
||||
// for i := 0; i < count; i++ {
|
||||
// time.Sleep(1 * time.Millisecond)
|
||||
// uid, _ := db.DB.MgoSkipUID(i)
|
||||
// fmt.Println("operate uid: ", uid)
|
||||
// err := db.DB.DelUserChat(uid)
|
||||
// if err != nil {
|
||||
// fmt.Println("operate uid failed: ", uid, err.Error())
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// timeUnixEnd := time.Now().Unix()
|
||||
// costTime := timeUnixEnd - timeUnixBegin
|
||||
// if costTime > int64(config.Config.Mongo.DBRetainChatRecords*24*3600) {
|
||||
// continue
|
||||
// } else {
|
||||
// sleepTime := 0
|
||||
// if int64(config.Config.Mongo.DBRetainChatRecords*24*3600)-costTime > 24*3600 {
|
||||
// sleepTime = 24 * 3600
|
||||
// } else {
|
||||
// sleepTime = config.Config.Mongo.DBRetainChatRecords*24*3600 - int(costTime)
|
||||
// }
|
||||
// fmt.Println("sleep: ", sleepTime)
|
||||
// time.Sleep(time.Duration(sleepTime) * time.Second)
|
||||
// }
|
||||
//}
|
||||
for {
|
||||
uidList, err := im_mysql_model.SelectAllUID()
|
||||
if err != nil {
|
||||
log.NewError("999999", err.Error())
|
||||
} else {
|
||||
for _, v := range uidList {
|
||||
minSeq, err := commonDB.DB.GetMinSeqFromMongo(v)
|
||||
if err != nil {
|
||||
log.NewError("999999", "get user minSeq err", err.Error(), v)
|
||||
continue
|
||||
} else {
|
||||
err := commonDB.DB.SetUserMinSeq(v, minSeq)
|
||||
if err != nil {
|
||||
log.NewError("999999", "set user minSeq err", err.Error(), v)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Duration(100) * time.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
|
||||
# The class cannot be named by Pascal or camel case.
|
||||
# If it is not used, the corresponding structure will not be set,
|
||||
# and it will not be read naturally.
|
||||
serverversion: 1.0.0
|
||||
#---------------Infrastructure configuration---------------------#
|
||||
etcd:
|
||||
etcdSchema: openIM
|
||||
etcdAddr: [ etcd:2379 ]
|
||||
|
||||
mysql:
|
||||
dbMysqlAddress: [ mysql:3306 ]
|
||||
dbMysqlUserName: root
|
||||
dbMysqlPassword: openIM
|
||||
dbMysqlDatabaseName: openIM
|
||||
dbTableName: eMsg
|
||||
dbMsgTableNum: 1
|
||||
dbMaxOpenConns: 20
|
||||
dbMaxIdleConns: 10
|
||||
dbMaxLifeTime: 120
|
||||
|
||||
mongo:
|
||||
dbAddress: [ mongodb:27017 ]
|
||||
dbDirect: false
|
||||
dbTimeout: 10
|
||||
dbDatabase: openIM
|
||||
dbSource: admin
|
||||
dbUserName:
|
||||
dbPassword:
|
||||
dbMaxPoolSize: 20
|
||||
dbRetainChatRecords: 7
|
||||
|
||||
redis:
|
||||
dbAddress: redis:6379
|
||||
dbMaxIdle: 128
|
||||
dbMaxActive: 0
|
||||
dbIdleTimeout: 120
|
||||
dbPassWord: openIM
|
||||
|
||||
kafka:
|
||||
ws2mschat:
|
||||
addr: [ kafka:9092 ]
|
||||
topic: "ws2ms_chat"
|
||||
ms2pschat:
|
||||
addr: [ kafka:9092 ]
|
||||
topic: "ms2ps_chat"
|
||||
consumergroupid:
|
||||
msgToMongo: mongo
|
||||
msgToMySql: mysql
|
||||
msgToPush: push
|
||||
|
||||
|
||||
|
||||
#---------------Internal service configuration---------------------#
|
||||
|
||||
# The service ip default is empty,
|
||||
# automatically obtain the machine's valid network card ip as the service ip,
|
||||
# otherwise the configuration ip is preferred
|
||||
serverip:
|
||||
|
||||
api:
|
||||
openImApiPort: [ 10000 ]
|
||||
sdk:
|
||||
openImSdkWsPort: [ 30000 ]
|
||||
|
||||
credential:
|
||||
tencent:
|
||||
appID: 1302656840
|
||||
region: ap-chengdu
|
||||
bucket: echat-1302656840
|
||||
secretID: AKIDGNYVChzIQinu7QEgtNp0hnNgqcV8vZTC
|
||||
secretKey: kz15vW83qM6dBUWIq681eBZA0c0vlIbe
|
||||
|
||||
|
||||
rpcport:
|
||||
openImUserPort: [ 10100 ]
|
||||
openImFriendPort: [ 10200 ]
|
||||
openImOfflineMessagePort: [ 10300 ]
|
||||
openImOnlineRelayPort: [ 10400 ]
|
||||
openImGroupPort: [ 10500 ]
|
||||
openImAuthPort: [ 10600 ]
|
||||
openImPushPort: [ 10700 ]
|
||||
|
||||
|
||||
rpcregistername:
|
||||
openImUserName: User
|
||||
openImFriendName: Friend
|
||||
openImOfflineMessageName: OfflineMessage
|
||||
openImPushName: Push
|
||||
openImOnlineMessageRelayName: OnlineMessageRelay
|
||||
openImGroupName: Group
|
||||
openImAuthName: Auth
|
||||
|
||||
log:
|
||||
storageLocation: ../logs/
|
||||
rotationTime: 24
|
||||
remainRotationCount: 5
|
||||
remainLogLevel: 6
|
||||
elasticSearchSwitch: false
|
||||
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
||||
elasticSearchUser: ""
|
||||
elasticSearchPassword: ""
|
||||
|
||||
modulename:
|
||||
longConnSvrName: msg_gateway
|
||||
msgTransferName: msg_transfer
|
||||
pushName: push
|
||||
|
||||
longconnsvr:
|
||||
openImWsPort: [ 17778 ]
|
||||
websocketMaxConnNum: 10000
|
||||
websocketMaxMsgLen: 4096
|
||||
websocketTimeOut: 10
|
||||
|
||||
push:
|
||||
tpns:
|
||||
ios:
|
||||
accessID: 1600018281
|
||||
secretKey: 3cd68a77a95b89e5089a1aca523f318f
|
||||
android:
|
||||
accessID: 111
|
||||
secretKey: 111
|
||||
jpns:
|
||||
appKey: 2783339cee4de379cc798fe1
|
||||
masterSecret: 66e5f309e032c68cc668c28a
|
||||
pushUrl: "https://api.jpush.cn/v3/push"
|
||||
manager:
|
||||
appManagerUid: ["openIM123456","openIM654321"]
|
||||
secrets: ["openIM1","openIM2"]
|
||||
|
||||
secret: tuoyun
|
||||
|
||||
multiloginpolicy:
|
||||
onlyOneTerminalAccess: false
|
||||
mobileAndPCTerminalAccessButOtherTerminalKickEachOther: true
|
||||
allTerminalAccess: false
|
||||
|
||||
#token config
|
||||
tokenpolicy:
|
||||
accessSecret: "open_im_server"
|
||||
# Token effective time seconds as a unit
|
||||
#Seven days 7*24*60*60
|
||||
accessExpire: 604800
|
||||
|
||||
messagecallback:
|
||||
callbackSwitch: false
|
||||
callbackUrl: "http://www.xxx.com/msg/judge"
|
@ -0,0 +1,11 @@
|
||||
FROM golang:1.16 as base
|
||||
|
||||
FROM base as dev
|
||||
|
||||
ENV GO111MODULE=on
|
||||
ENV GOPROXY=https://goproxy.cn,direct
|
||||
|
||||
|
||||
RUN curl -sSfL https://raw.githubusercontent.com/cosmtrek/air/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
|
||||
|
||||
CMD ["air"]
|
@ -0,0 +1,381 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:5.7
|
||||
ports:
|
||||
- 3306:3306
|
||||
container_name: mysql
|
||||
volumes:
|
||||
- ./components/mysql/data:/var/lib/mysql
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: openIM
|
||||
restart: always
|
||||
|
||||
mongodb:
|
||||
image: mongo:4.0
|
||||
ports:
|
||||
- 27017:27017
|
||||
container_name: mongo
|
||||
volumes:
|
||||
- ./components/mongodb/data:/data/db
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
|
||||
redis:
|
||||
image: redis
|
||||
ports:
|
||||
- 6379:6379
|
||||
container_name: redis
|
||||
volumes:
|
||||
- ./components/redis/data:/data
|
||||
#redis config file
|
||||
#- ./components/redis/config/redis.conf:/usr/local/redis/config/redis.conf
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
sysctls:
|
||||
net.core.somaxconn: 1024
|
||||
command: redis-server --requirepass openIM --appendonly yes
|
||||
|
||||
|
||||
zookeeper:
|
||||
image: wurstmeister/zookeeper
|
||||
ports:
|
||||
- 2181:2181
|
||||
container_name: zookeeper
|
||||
volumes:
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
|
||||
|
||||
kafka:
|
||||
image: wurstmeister/kafka
|
||||
container_name: kafka
|
||||
restart: always
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
KAFKA_BROKER_ID: 0
|
||||
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
|
||||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
|
||||
KAFKA_LISTENERS: PLAINTEXT://kafka:9092
|
||||
depends_on:
|
||||
- zookeeper
|
||||
links:
|
||||
- zookeeper
|
||||
ports:
|
||||
- 9092:9092
|
||||
|
||||
etcd:
|
||||
image: quay.io/coreos/etcd
|
||||
ports:
|
||||
- 2379:2379
|
||||
- 2380:2380
|
||||
container_name: etcd
|
||||
volumes:
|
||||
- /etc/timezone:/etc/timezone
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
ETCDCTL_API: 3
|
||||
restart: always
|
||||
command: /usr/local/bin/etcd --name etcd0 --data-dir /etcd-data --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-advertise-peer-urls http://0.0.0.0:2380 --initial-cluster etcd0=http://0.0.0.0:2380 --initial-cluster-token tkn --initial-cluster-state new
|
||||
|
||||
open_im_api:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_api
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/open_im_api
|
||||
ports:
|
||||
- 10000:10000
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
|
||||
open_im_auth:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_auth
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/rpc/open_im_auth
|
||||
ports:
|
||||
- 10600:10600
|
||||
|
||||
open_im_user:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_user
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/rpc/open_im_user
|
||||
ports:
|
||||
- 10100:10100
|
||||
|
||||
open_im_friend:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_friend
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/rpc/open_im_friend
|
||||
ports:
|
||||
- 10200:10200
|
||||
|
||||
open_im_group:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_group
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/rpc/open_im_group
|
||||
ports:
|
||||
- 10500:10500
|
||||
|
||||
open_im_push:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_push
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/open_im_push
|
||||
ports:
|
||||
- 10700:10700
|
||||
|
||||
open_im_timed_task:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_timed_task
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/open_im_timer_task
|
||||
|
||||
open_im_offline_msg:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_offline_msg
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/rpc/open_im_msg
|
||||
ports:
|
||||
- 10300:10300
|
||||
|
||||
open_im_msg_transfer:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_msg_transfer
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/open_im_msg_transfer
|
||||
|
||||
open_im_msg_gateway:
|
||||
build:
|
||||
dockerfile: dev.Dockerfile
|
||||
context: .
|
||||
target: dev
|
||||
network: host
|
||||
depends_on:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
links:
|
||||
- kafka
|
||||
- mysql
|
||||
- mongodb
|
||||
- redis
|
||||
- etcd
|
||||
container_name: open_im_msg_gateway
|
||||
volumes:
|
||||
- ./cmd:/Open-IM-Server/cmd
|
||||
- ./config/config.dev.yaml:/Open-IM-Server/config/config.yaml
|
||||
- ./internal:/Open-IM-Server/internal
|
||||
- ./pkg:/Open-IM-Server/pkg
|
||||
- ./go.mod:/Open-IM-Server/go.mod
|
||||
- ./go.sum:/Open-IM-Server/go.sum
|
||||
working_dir: /Open-IM-Server/cmd/open_im_msg_gateway
|
||||
ports:
|
||||
- 10400:10400
|
||||
- 17778:17778
|
@ -0,0 +1,85 @@
|
||||
version: "3"
|
||||
services:
|
||||
mysql:
|
||||
image: mysql:5.7
|
||||
ports:
|
||||
- 3306:3306
|
||||
container_name: mysql
|
||||
volumes:
|
||||
- ./components/mysql/data:/var/lib/mysql
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: openIM
|
||||
restart: always
|
||||
|
||||
mongodb:
|
||||
image: mongo:4.0
|
||||
ports:
|
||||
- 27017:27017
|
||||
container_name: mongo
|
||||
volumes:
|
||||
- ./components/mongodb/data:/data/db
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
|
||||
redis:
|
||||
image: redis
|
||||
ports:
|
||||
- 6379:6379
|
||||
container_name: redis
|
||||
volumes:
|
||||
- ./components/redis/data:/data
|
||||
#redis config file
|
||||
#- ./components/redis/config/redis.conf:/usr/local/redis/config/redis.conf
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
sysctls:
|
||||
net.core.somaxconn: 1024
|
||||
command: redis-server --requirepass openIM --appendonly yes
|
||||
|
||||
|
||||
zookeeper:
|
||||
image: wurstmeister/zookeeper
|
||||
ports:
|
||||
- 2181:2181
|
||||
container_name: zookeeper
|
||||
volumes:
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
restart: always
|
||||
|
||||
|
||||
kafka:
|
||||
image: wurstmeister/kafka
|
||||
container_name: kafka
|
||||
restart: always
|
||||
environment:
|
||||
TZ: Asia/Shanghai
|
||||
KAFKA_BROKER_ID: 0
|
||||
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
|
||||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://127.0.0.1:9092
|
||||
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
|
||||
# network_mode: "host"
|
||||
depends_on:
|
||||
- zookeeper
|
||||
links:
|
||||
- zookeeper
|
||||
ports:
|
||||
- 9092:9092
|
||||
|
||||
etcd:
|
||||
image: quay.io/coreos/etcd
|
||||
ports:
|
||||
- 2379:2379
|
||||
- 2380:2380
|
||||
container_name: etcd
|
||||
volumes:
|
||||
- /etc/timezone:/etc/timezone
|
||||
- /etc/localtime:/etc/localtime
|
||||
environment:
|
||||
ETCDCTL_API: 3
|
||||
restart: always
|
||||
command: /usr/local/bin/etcd --name etcd0 --data-dir /etcd-data --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-advertise-peer-urls http://0.0.0.0:2380 --initial-cluster etcd0=http://0.0.0.0:2380 --initial-cluster-token tkn --initial-cluster-state new
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,25 @@
|
||||
package apiAuth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func Test_UserRegister(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(res)
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(`{"secret": "tuoyun", "platform": 1, "uid": "3", "name": "1"}`))
|
||||
|
||||
UserRegister(c)
|
||||
|
||||
assert.Equal(t, res.Code, 200)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func Test_UserRegister(t *testing.T) {
|
||||
res := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(res)
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(`{"uidList": []}`))
|
||||
|
||||
GetUserInfo(c)
|
||||
assert.Equal(t, 400, res.Code)
|
||||
|
||||
res = httptest.NewRecorder()
|
||||
c, _ = gin.CreateTestContext(res)
|
||||
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(`{"operationID": "1", "uidList": []}`))
|
||||
|
||||
GetUserInfo(c)
|
||||
assert.Equal(t, 200, res.Code)
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func GetAuthorization(Appkey string, MasterSecret string) string {
|
||||
str := fmt.Sprintf("%s:%s", Appkey, MasterSecret)
|
||||
buf := []byte(str)
|
||||
Authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(buf))
|
||||
return Authorization
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package push
|
||||
|
||||
import (
|
||||
"Open_IM/internal/push/jpush/common"
|
||||
"Open_IM/internal/push/jpush/requestBody"
|
||||
"Open_IM/pkg/common/config"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type JPushResp struct {
|
||||
}
|
||||
|
||||
func JGAccountListPush(accounts []string, content, detailContent, platform string) ([]byte, error) {
|
||||
|
||||
var pf requestBody.Platform
|
||||
_ = pf.SetPlatform(platform)
|
||||
var au requestBody.Audience
|
||||
au.SetAlias(accounts)
|
||||
var no requestBody.Notification
|
||||
no.SetAlert(content)
|
||||
var me requestBody.Message
|
||||
me.SetMsgContent(detailContent)
|
||||
var po requestBody.PushObj
|
||||
po.SetPlatform(&pf)
|
||||
po.SetAudience(&au)
|
||||
po.SetNotification(&no)
|
||||
po.SetMessage(&me)
|
||||
|
||||
con, err := json.Marshal(po)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
req, err := http.NewRequest("POST", config.Config.Push.Jpns.PushUrl, bytes.NewBuffer(con))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", common.GetAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret))
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
result, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package requestBody
|
||||
|
||||
const (
|
||||
TAG = "tag"
|
||||
TAG_AND = "tag_and"
|
||||
TAG_NOT = "tag_not"
|
||||
ALIAS = "alias"
|
||||
REGISTRATION_ID = "registration_id"
|
||||
SEGMENT = "segment"
|
||||
ABTEST = "abtest"
|
||||
)
|
||||
|
||||
type Audience struct {
|
||||
Object interface{}
|
||||
audience map[string][]string
|
||||
}
|
||||
|
||||
func (a *Audience) set(key string, v []string) {
|
||||
if a.audience == nil {
|
||||
a.audience = make(map[string][]string)
|
||||
a.Object = a.audience
|
||||
}
|
||||
|
||||
//v, ok = this.audience[key]
|
||||
//if ok {
|
||||
// return
|
||||
//}
|
||||
a.audience[key] = v
|
||||
}
|
||||
|
||||
func (a *Audience) SetTag(tags []string) {
|
||||
a.set(TAG, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetTagAnd(tags []string) {
|
||||
a.set(TAG_AND, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetTagNot(tags []string) {
|
||||
a.set(TAG_NOT, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetAlias(alias []string) {
|
||||
a.set(ALIAS, alias)
|
||||
}
|
||||
|
||||
func (a *Audience) SetRegistrationId(ids []string) {
|
||||
a.set(REGISTRATION_ID, ids)
|
||||
}
|
||||
|
||||
func (a *Audience) SetAll() {
|
||||
a.Object = "all"
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package requestBody
|
||||
|
||||
type Message struct {
|
||||
MsgContent string `json:"msg_content"`
|
||||
Title string `json:"title,omitempty"`
|
||||
ContentType string `json:"content_type,omitempty"`
|
||||
Extras map[string]interface{} `json:"extras,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Message) SetMsgContent(c string) {
|
||||
m.MsgContent = c
|
||||
}
|
||||
|
||||
func (m *Message) SetTitle(t string) {
|
||||
m.Title = t
|
||||
}
|
||||
|
||||
func (m *Message) SetContentType(c string) {
|
||||
m.ContentType = c
|
||||
}
|
||||
|
||||
func (m *Message) SetExtras(key string, value interface{}) {
|
||||
if m.Extras == nil {
|
||||
m.Extras = make(map[string]interface{})
|
||||
}
|
||||
m.Extras[key] = value
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package requestBody
|
||||
|
||||
type Notification struct {
|
||||
Alert string `json:"alert,omitempty"`
|
||||
Android *Android `json:"android,omitempty"`
|
||||
IOS *Ios `json:"ios,omitempty"`
|
||||
}
|
||||
|
||||
type Android struct {
|
||||
}
|
||||
|
||||
type Ios struct {
|
||||
}
|
||||
|
||||
func (n *Notification) SetAlert(alert string) {
|
||||
n.Alert = alert
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package requestBody
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"errors"
|
||||
)
|
||||
|
||||
const (
|
||||
ANDROID = "android"
|
||||
IOS = "ios"
|
||||
QUICKAPP = "quickapp"
|
||||
WINDOWSPHONE = "winphone"
|
||||
ALL = "all"
|
||||
)
|
||||
|
||||
type Platform struct {
|
||||
Os interface{}
|
||||
osArry []string
|
||||
}
|
||||
|
||||
func (p *Platform) Set(os string) error {
|
||||
if p.Os == nil {
|
||||
p.osArry = make([]string, 0, 4)
|
||||
} else {
|
||||
switch p.Os.(type) {
|
||||
case string:
|
||||
return errors.New("platform is all")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
for _, value := range p.osArry {
|
||||
if os == value {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
switch os {
|
||||
case IOS:
|
||||
fallthrough
|
||||
case ANDROID:
|
||||
fallthrough
|
||||
case QUICKAPP:
|
||||
fallthrough
|
||||
case WINDOWSPHONE:
|
||||
p.osArry = append(p.osArry, os)
|
||||
p.Os = p.osArry
|
||||
default:
|
||||
return errors.New("unknow platform")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (p *Platform) SetPlatform(platform string) error {
|
||||
switch platform {
|
||||
case utils.AndroidPlatformStr:
|
||||
return p.SetAndroid()
|
||||
case utils.IOSPlatformStr:
|
||||
return p.SetIOS()
|
||||
default:
|
||||
return errors.New("platform err")
|
||||
}
|
||||
|
||||
}
|
||||
func (p *Platform) SetIOS() error {
|
||||
return p.Set(IOS)
|
||||
}
|
||||
|
||||
func (p *Platform) SetAndroid() error {
|
||||
return p.Set(ANDROID)
|
||||
}
|
||||
|
||||
func (p *Platform) SetQuickApp() error {
|
||||
return p.Set(QUICKAPP)
|
||||
}
|
||||
|
||||
func (p *Platform) SetWindowsPhone() error {
|
||||
return p.Set(WINDOWSPHONE)
|
||||
}
|
||||
|
||||
func (p *Platform) SetAll() {
|
||||
p.Os = ALL
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package requestBody
|
||||
|
||||
type PushObj struct {
|
||||
Platform interface{} `json:"platform"`
|
||||
Audience interface{} `json:"audience"`
|
||||
Notification interface{} `json:"notification,omitempty"`
|
||||
Message interface{} `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func (p *PushObj) SetPlatform(pf *Platform) {
|
||||
p.Platform = pf.Os
|
||||
}
|
||||
|
||||
func (p *PushObj) SetAudience(ad *Audience) {
|
||||
p.Audience = ad.Object
|
||||
}
|
||||
|
||||
func (p *PushObj) SetNotification(no *Notification) {
|
||||
p.Notification = no
|
||||
}
|
||||
|
||||
func (p *PushObj) SetMessage(m *Message) {
|
||||
p.Message = m
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
gin.SetMode(gin.TestMode)
|
||||
}
|
||||
|
||||
func performRequest(r http.Handler, method, origin string) *httptest.ResponseRecorder {
|
||||
return performRequestWithHeaders(r, method, origin, http.Header{})
|
||||
}
|
||||
|
||||
func performRequestWithHeaders(r http.Handler, method, origin string, header http.Header) *httptest.ResponseRecorder {
|
||||
req, _ := http.NewRequest(method, "/", nil)
|
||||
// From go/net/http/request.go:
|
||||
// For incoming requests, the Host header is promoted to the
|
||||
// Request.Host field and removed from the Header map.
|
||||
req.Host = header.Get("Host")
|
||||
header.Del("Host")
|
||||
if len(origin) > 0 {
|
||||
header.Set("Origin", origin)
|
||||
}
|
||||
req.Header = header
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
func newTestRouter() *gin.Engine {
|
||||
router := gin.New()
|
||||
router.Use(utils.CorsHandler())
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "get")
|
||||
})
|
||||
router.POST("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "post")
|
||||
})
|
||||
router.PATCH("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "patch")
|
||||
})
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func Test_CorsHandler(t *testing.T) {
|
||||
router := newTestRouter()
|
||||
// no CORS request, origin == ""
|
||||
w := performRequest(router, "GET", "")
|
||||
assert.Equal(t, "get", w.Body.String())
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Origin"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Methods"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Headers"), "*")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Expose-Headers"), "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Max-Age"), "172800")
|
||||
assert.Equal(t, w.Header().Get("Access-Control-Allow-Credentials"), "false")
|
||||
assert.Equal(t, w.Header().Get("content-type"), "application/json")
|
||||
|
||||
w = performRequest(router, "OPTIONS", "")
|
||||
assert.Equal(t, w.Body.String(), "\"Options Request!\"")
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServerIP(t *testing.T) {
|
||||
if net.ParseIP(utils.ServerIP) == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/snowflake"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
idGenerator, err = snowflake.NewNode(getNodeNum())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getNodeNum() int64 {
|
||||
return 1
|
||||
}
|
||||
|
||||
var idGenerator *snowflake.Node
|
||||
|
||||
func GenID() string {
|
||||
return idGenerator.Generate().String()
|
||||
}
|
||||
|
||||
func GenIDs(count int) []string {
|
||||
//impl
|
||||
return []string{}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGenID(t *testing.T) {
|
||||
m := map[string]struct{}{}
|
||||
for i := 0; i < 2000; i++ {
|
||||
got := GenID()
|
||||
if _, ok := m[got]; !ok {
|
||||
m[got] = struct{}{}
|
||||
} else {
|
||||
t.Error("id generate error", got)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var (
|
||||
_, b, _, _ = runtime.Caller(0)
|
||||
// Root folder of this project
|
||||
Root = filepath.Join(filepath.Dir(b), "../..")
|
||||
)
|
||||
|
||||
func Test_GenSmallImage(t *testing.T) {
|
||||
println(Root)
|
||||
err := utils.GenSmallImage(Root+"/docs/open-im-logo.png", Root+"/out-test/open-im-logo-test.png")
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = utils.GenSmallImage(Root+"/docs/open-im-logo.png", "out-test/open-im-logo-test.png")
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = utils.GenSmallImage(Root+"/docs/Architecture.jpg", "out-test/Architecture-test.jpg")
|
||||
assert.Nil(t, err)
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/utils"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_BuildClaims(t *testing.T) {
|
||||
uid := "1"
|
||||
platform := "PC"
|
||||
ttl := int64(-1)
|
||||
claim := utils.BuildClaims(uid, platform, ttl)
|
||||
now := time.Now().Unix()
|
||||
|
||||
assert.Equal(t, claim.UID, uid, "uid should equal")
|
||||
assert.Equal(t, claim.Platform, platform, "platform should equal")
|
||||
assert.Equal(t, claim.RegisteredClaims.ExpiresAt, int64(-1), "StandardClaims.ExpiresAt should be equal")
|
||||
// time difference within 1s
|
||||
assert.Equal(t, claim.RegisteredClaims.IssuedAt, now, "StandardClaims.IssuedAt should be equal")
|
||||
assert.Equal(t, claim.RegisteredClaims.NotBefore, now, "StandardClaims.NotBefore should be equal")
|
||||
|
||||
ttl = int64(60)
|
||||
now = time.Now().Unix()
|
||||
claim = utils.BuildClaims(uid, platform, ttl)
|
||||
// time difference within 1s
|
||||
assert.Equal(t, claim.RegisteredClaims.ExpiresAt, int64(60)+now, "StandardClaims.ExpiresAt should be equal")
|
||||
assert.Equal(t, claim.RegisteredClaims.IssuedAt, now, "StandardClaims.IssuedAt should be equal")
|
||||
assert.Equal(t, claim.RegisteredClaims.NotBefore, now, "StandardClaims.NotBefore should be equal")
|
||||
}
|
||||
|
||||
func Test_CreateToken(t *testing.T) {
|
||||
uid := "1"
|
||||
platform := int32(1)
|
||||
now := time.Now().Unix()
|
||||
|
||||
tokenString, expiresAt, err := utils.CreateToken(uid, platform)
|
||||
|
||||
assert.NotEmpty(t, tokenString)
|
||||
assert.Equal(t, expiresAt, 604800+now)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func Test_VerifyToken(t *testing.T) {
|
||||
uid := "1"
|
||||
platform := int32(1)
|
||||
tokenString, _, _ := utils.CreateToken(uid, platform)
|
||||
result := utils.VerifyToken(tokenString, uid)
|
||||
assert.True(t, result)
|
||||
result = utils.VerifyToken(tokenString, "2")
|
||||
assert.False(t, result)
|
||||
}
|
||||
|
||||
func Test_ParseRedisInterfaceToken(t *testing.T) {
|
||||
uid := "1"
|
||||
platform := int32(1)
|
||||
tokenString, _, _ := utils.CreateToken(uid, platform)
|
||||
|
||||
claims, err := utils.ParseRedisInterfaceToken([]uint8(tokenString))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, claims.UID, uid)
|
||||
|
||||
// timeout
|
||||
config.Config.TokenPolicy.AccessExpire = -80
|
||||
tokenString, _, _ = utils.CreateToken(uid, platform)
|
||||
claims, err = utils.ParseRedisInterfaceToken([]uint8(tokenString))
|
||||
assert.Equal(t, err, utils.TokenExpired)
|
||||
assert.Nil(t, claims)
|
||||
}
|
||||
|
||||
func Test_ParseToken(t *testing.T) {
|
||||
uid := "1"
|
||||
platform := int32(1)
|
||||
tokenString, _, _ := utils.CreateToken(uid, platform)
|
||||
claims, err := utils.ParseToken(tokenString)
|
||||
if err == nil {
|
||||
assert.Equal(t, claims.UID, uid)
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_Md5(t *testing.T) {
|
||||
result := utils.Md5("go")
|
||||
assert.Equal(t, result, "34d1f91fb2e514b8576fab1a75a89a6b")
|
||||
|
||||
result2 := utils.Md5("go")
|
||||
assert.Equal(t, result, result2)
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_PlatformIDToName(t *testing.T) {
|
||||
assert.Equal(t, utils.PlatformIDToName(1), "IOS")
|
||||
assert.Equal(t, utils.PlatformIDToName(2), "Android")
|
||||
assert.Equal(t, utils.PlatformIDToName(3), "Windows")
|
||||
assert.Equal(t, utils.PlatformIDToName(4), "OSX")
|
||||
assert.Equal(t, utils.PlatformIDToName(5), "Web")
|
||||
assert.Equal(t, utils.PlatformIDToName(6), "MiniWeb")
|
||||
assert.Equal(t, utils.PlatformIDToName(7), "Linux")
|
||||
|
||||
assert.Equal(t, utils.PlatformIDToName(0), "")
|
||||
}
|
||||
|
||||
func Test_PlatformNameToID(t *testing.T) {
|
||||
assert.Equal(t, utils.PlatformNameToID("IOS"), int32(1))
|
||||
assert.Equal(t, utils.PlatformNameToID("Android"), int32(2))
|
||||
assert.Equal(t, utils.PlatformNameToID("Windows"), int32(3))
|
||||
assert.Equal(t, utils.PlatformNameToID("OSX"), int32(4))
|
||||
assert.Equal(t, utils.PlatformNameToID("Web"), int32(5))
|
||||
assert.Equal(t, utils.PlatformNameToID("MiniWeb"), int32(6))
|
||||
assert.Equal(t, utils.PlatformNameToID("Linux"), int32(7))
|
||||
|
||||
assert.Equal(t, utils.PlatformNameToID("UnknownDevice"), int32(0))
|
||||
assert.Equal(t, utils.PlatformNameToID(""), int32(0))
|
||||
}
|
||||
|
||||
func Test_PlatformNameToClass(t *testing.T) {
|
||||
assert.Equal(t, utils.PlatformNameToClass("IOS"), "Mobile")
|
||||
assert.Equal(t, utils.PlatformNameToClass("Android"), "Mobile")
|
||||
assert.Equal(t, utils.PlatformNameToClass("OSX"), "PC")
|
||||
assert.Equal(t, utils.PlatformNameToClass("Windows"), "PC")
|
||||
assert.Equal(t, utils.PlatformNameToClass("Web"), "PC")
|
||||
assert.Equal(t, utils.PlatformNameToClass("MiniWeb"), "Mobile")
|
||||
assert.Equal(t, utils.PlatformNameToClass("Linux"), "PC")
|
||||
|
||||
assert.Equal(t, utils.PlatformNameToClass("UnknownDevice"), "")
|
||||
assert.Equal(t, utils.PlatformNameToClass(""), "")
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue