Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-microservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Li Feifei
im-microservice
Commits
0d8946a0
Commit
0d8946a0
authored
Jul 02, 2020
by
Li Feifei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
服务端错误返回code嘛
parent
1db452e8
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
98 additions
and
61 deletions
+98
-61
helper.go
helper/helper.go
+5
-3
main.go
main.go
+20
-29
db.go
sevice/im_chat_room/db.go
+15
-7
main.go
sevice/im_chat_room/main.go
+5
-3
db.go
sevice/im_configure/db.go
+22
-5
main.go
sevice/im_configure/main.go
+3
-0
im_user.go
sevice/im_user/im_user.go
+3
-3
db.go
sevice/im_user_relationship/db.go
+11
-1
main.go
sevice/im_user_relationship/main.go
+7
-2
validator_strucut.go
validator_struct/validator_strucut.go
+7
-8
No files found.
helper/helper.go
View file @
0d8946a0
...
...
@@ -2,12 +2,14 @@ package helper
import
(
"context"
"errors"
"im-microservice/db"
"log"
"math"
"time"
"im-microservice/db"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/go-playground/validator.v9"
)
...
...
@@ -18,7 +20,7 @@ func Valiator(in interface{}) error {
if
err
!=
nil
{
for
_
,
err
:=
range
err
.
(
validator
.
ValidationErrors
)
{
log
.
Println
(
"error == "
,
err
)
return
errors
.
New
(
"表单验证失败"
)
return
status
.
Error
(
codes
.
PermissionDenied
,
"表单验证失败"
)
}
}
return
nil
...
...
main.go
View file @
0d8946a0
...
...
@@ -3,34 +3,28 @@ package main
import
(
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"sync"
"log"
"net"
"regexp"
"strconv"
"sync"
"time"
"im-microservice/helper"
"im-microservice/pb"
"im-microservice/sevice/im_chat_room"
ic
"im-microservice/sevice/im_configure"
"im-microservice/sevice/im_user"
iur
"im-microservice/sevice/im_user_relationship"
ic
"im-microservice/sevice/im_configure"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
type
Health
interface
{
// Check returns if server is healthy or not
Check
(
c
context
.
Context
)
(
bool
,
error
)
...
...
@@ -38,7 +32,7 @@ type Health interface {
// 健康检测
type
Server
struct
{
mu
sync
.
Mutex
mu
sync
.
Mutex
statusMap
map
[
string
]
pb
.
HealthCheckResponse_ServingStatus
}
...
...
@@ -47,21 +41,24 @@ func NewServer() *Server {
statusMap
:
make
(
map
[
string
]
pb
.
HealthCheckResponse_ServingStatus
),
}
}
func
permissionError
(
msg
string
)
error
{
return
status
.
Error
(
codes
.
InvalidArgument
,
msg
)
}
func
(
s
*
Server
)
Check
(
ctx
context
.
Context
,
in
*
pb
.
HealthCheckRequest
)
(
*
pb
.
HealthCheckResponse
,
error
)
{
s
.
mu
.
Lock
()
defer
s
.
mu
.
Unlock
()
if
in
.
Service
==
""
{
return
&
pb
.
HealthCheckResponse
{
Status
:
pb
.
HealthCheckResponse_SERVING
,},
nil
return
&
pb
.
HealthCheckResponse
{
Status
:
pb
.
HealthCheckResponse_SERVING
},
nil
}
if
status
,
ok
:=
s
.
statusMap
[
in
.
Service
];
ok
{
return
&
pb
.
HealthCheckResponse
{
Status
:
status
,
},
nil
return
&
pb
.
HealthCheckResponse
{
Status
:
status
},
nil
}
return
nil
,
status
.
Error
(
codes
.
NotFound
,
"unkonw service"
)
return
nil
,
status
.
Error
(
codes
.
NotFound
,
"unkonw service"
)
}
const
(
port
=
":50051"
port
=
":50051"
health_path
=
"/pb.Health/Check"
)
...
...
@@ -69,32 +66,30 @@ func checksum(req map[string]string) error {
err
,
AppSecret
:=
helper
.
GetSecretByKey
(
req
[
"Appkey"
])
if
err
!=
nil
{
return
errors
.
New
(
"appkey不存在"
)
return
status
.
Error
(
codes
.
NotFound
,
"appkey不存在"
)
}
server_time
:=
time
.
Now
()
.
Unix
()
client_time
,
err
:=
strconv
.
Atoi
(
req
[
"Curtime"
])
if
err
!=
nil
{
return
err
return
status
.
Error
(
codes
.
Aborted
,
err
.
Error
())
}
if
server_time
>
int64
(
client_time
)
{
return
errors
.
New
(
"当前客户端时间不能小于服务端时间"
)
return
permissionError
(
"当前客户端时间不能小于服务端时间"
)
}
sha11
:=
sha1
.
New
()
sha11
.
Write
([]
byte
(
AppSecret
+
req
[
"Nonce"
]
+
req
[
"Curtime"
]))
sevice_checksum
:=
hex
.
EncodeToString
(
sha11
.
Sum
([]
byte
(
nil
)))
if
sevice_checksum
!=
req
[
"Checksum"
]
{
return
errors
.
New
(
"加密串不正确"
)
return
permissionError
(
"加密串不正确"
)
}
return
nil
}
func
auth
(
ctx
context
.
Context
,
req
interface
{},
info
*
grpc
.
UnaryServerInfo
,
handler
grpc
.
UnaryHandler
)
(
interface
{},
error
)
{
_
,
ok
:=
metadata
.
FromIncomingContext
(
ctx
)
if
!
ok
{
return
nil
,
grpc
.
Errorf
(
codes
.
Unauthenticated
,
"无Token认证信息"
)
}
if
info
.
FullMethod
!=
health_path
{
request_map
:=
make
(
map
[
string
]
string
)
s
:=
fmt
.
Sprintf
(
"%v"
,
req
)
...
...
@@ -110,27 +105,25 @@ func auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, hand
check_sum
:=
cs_r
.
FindAllStringSubmatch
(
s
,
-
1
)
if
len
(
appkey
)
<
1
{
return
nil
,
errors
.
New
(
"缺少appkey"
)
return
nil
,
permissionError
(
"缺少appkey"
)
}
if
len
(
nonce
)
<
1
{
return
nil
,
errors
.
New
(
"缺少nonce"
)
return
nil
,
permissionError
(
"缺少nonce"
)
}
if
len
(
curtime
)
<
1
{
return
nil
,
errors
.
New
(
"缺少curtime"
)
return
nil
,
permissionError
(
"缺少curtime"
)
}
if
len
(
check_sum
)
<
1
{
return
nil
,
errors
.
New
(
"缺少checksum"
)
return
nil
,
permissionError
(
"缺少checksum"
)
}
request_map
[
"Appkey"
]
=
appkey
[
0
][
1
]
request_map
[
"Nonce"
]
=
nonce
[
0
][
1
]
request_map
[
"Curtime"
]
=
curtime
[
0
][
1
]
request_map
[
"Checksum"
]
=
check_sum
[
0
][
1
]
if
err
:=
checksum
(
request_map
);
err
!=
nil
{
return
nil
,
err
}
}
return
handler
(
ctx
,
req
)
}
...
...
@@ -144,10 +137,8 @@ func main() {
var
opts
[]
grpc
.
ServerOption
opts
=
append
(
opts
,
grpc
.
UnaryInterceptor
(
auth
))
s
:=
grpc
.
NewServer
(
opts
...
)
// s := grpc.NewServer()
srv
:=
NewServer
()
pb
.
RegisterHealthServer
(
s
,
srv
)
pb
.
RegisterConfigureSeviceServer
(
s
,
&
ic
.
ConfigureSevice
{})
pb
.
RegisterChatRoomServiceServer
(
s
,
&
im_chat_room
.
ImChatRoomService
{})
pb
.
RegisterImUserServer
(
s
,
&
im_user
.
ImUserServer
{})
...
...
sevice/im_chat_room/db.go
View file @
0d8946a0
package
im_chat_room
import
(
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"im-microservice/db"
"im-microservice/helper"
...
...
@@ -31,7 +32,7 @@ func AddChatRoom(addStruct *validator_struct.ChatRoomAddStruct) (int64, error) {
chat
.
Createtime
=
now
chat
.
Updatetime
=
now
room_id
,
err
:=
db
.
MysqlClient
.
Insert
(
&
chat
)
return
room_id
,
err
return
room_id
,
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
func
UpdateChatRoom
(
update
*
validator_struct
.
ChatRoomUpdate
)
error
{
...
...
@@ -47,7 +48,7 @@ func UpdateChatRoom(update *validator_struct.ChatRoomUpdate) error {
}
if
len
(
orm_params
)
==
0
{
return
errors
.
New
(
"修改房间的参数为空"
)
return
status
.
Error
(
codes
.
InvalidArgument
,
"修改房间的参数为空"
)
}
orm_params
[
"updatetime"
]
=
helper
.
GetNowTime
()
_
,
err
:=
db
.
MysqlClient
.
QueryTable
(
chat_table_name
)
.
...
...
@@ -59,11 +60,18 @@ func UpdateChatRoom(update *validator_struct.ChatRoomUpdate) error {
func
DeleteChatRoom
(
room_id
int64
)
error
{
_
,
err
:=
db
.
MysqlClient
.
QueryTable
(
chat_table_name
)
.
Filter
(
chat_room_id
,
room_id
)
.
Delete
()
return
err
if
err
!=
nil
{
return
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
nil
}
func
ChatRoomInfo
(
info
*
validator_struct
.
ChatRoomInfo
)
(
res
db
.
ImChatRoom
,
err
error
)
{
err
=
db
.
MysqlClient
.
QueryTable
(
chat_table_name
)
.
Filter
(
chat_room_id
,
info
.
RoomId
)
.
One
(
&
res
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
}
...
...
@@ -71,7 +79,7 @@ func GetChatRooms(request *pb.ChatRoomAllRequest) (results map[string]interface{
var
(
announcement
=
request
.
GetAnnouncement
()
name
=
request
.
GetName
()
status
=
request
.
GetStatus
()
req_status
=
request
.
GetStatus
()
creator
=
request
.
GetCreator
()
createtime
=
request
.
GetCreatetime
()
page
=
request
.
GetPage
()
...
...
@@ -86,8 +94,8 @@ func GetChatRooms(request *pb.ChatRoomAllRequest) (results map[string]interface{
if
name
!=
""
{
count_db
=
count_db
.
Filter
(
chat_room
+
"__icontains"
,
name
)
}
if
status
!=
0
{
count_db
=
count_db
.
Filter
(
chat_status
,
status
)
if
req_
status
!=
0
{
count_db
=
count_db
.
Filter
(
chat_status
,
req_
status
)
}
if
creator
!=
""
{
count_db
=
count_db
.
Filter
(
chat_creator
,
creator
)
...
...
sevice/im_chat_room/main.go
View file @
0d8946a0
...
...
@@ -7,6 +7,9 @@ import (
"im-microservice/helper"
"im-microservice/pb"
vs
"im-microservice/validator_struct"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type
ImChatRoomService
struct
{
...
...
@@ -31,7 +34,6 @@ func (cr *ImChatRoomService) Add(ctx context.Context,
func
(
cr
*
ImChatRoomService
)
Update
(
ctx
context
.
Context
,
in
*
pb
.
ChatRoomUpdateRequest
)
(
reply
*
pb
.
ChatRoomUpdateReply
,
err
error
)
{
chat_room_struct
:=
vs
.
NewChatRoomUpdate
(
in
)
if
err
=
helper
.
Valiator
(
chat_room_struct
);
err
!=
nil
{
return
...
...
@@ -89,6 +91,7 @@ func (cr *ImChatRoomService) All(ctx context.Context,
in
*
pb
.
ChatRoomAllRequest
)
(
reply
*
pb
.
GetChatRoomsReply
,
err
error
)
{
results
,
err
:=
GetChatRooms
(
in
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
reply
=
&
pb
.
GetChatRoomsReply
{
Paginate
:
&
pb
.
Page
{}}
...
...
@@ -105,4 +108,4 @@ func (cr *ImChatRoomService) All(ctx context.Context,
reply
.
Paginate
.
TotalCount
=
results
[
"total_count"
]
.
(
int64
)
reply
.
Paginate
.
TotalPage
=
results
[
"total_page"
]
.
(
int64
)
return
}
\ No newline at end of file
}
sevice/im_configure/db.go
View file @
0d8946a0
...
...
@@ -2,9 +2,13 @@ package im_configure
import
(
"errors"
"github.com/astaxie/beego/orm"
"im-microservice/db"
"im-microservice/validator_struct"
"github.com/astaxie/beego/orm"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const
(
...
...
@@ -18,6 +22,9 @@ func GetImBaseByUserId(user_id int64) (db.ImBase, error) {
var
im_base
db
.
ImBase
err
:=
db
.
MysqlClient
.
QueryTable
(
b_table_name
)
.
Filter
(
b_user_id
,
user_id
)
.
One
(
&
im_base
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
im_base
,
err
}
...
...
@@ -27,14 +34,21 @@ func AddImBase(im *validator_struct.ConfigureAddStruct) error {
configure
.
MsgHookUrl
=
im
.
MsgHookUrl
configure
.
MultideviceType
=
im
.
MultideviceType
_
,
err
:=
db
.
MysqlClient
.
Insert
(
configure
)
return
err
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
nil
}
func
DelConfigure
(
user_id
int64
)
error
{
func
DelConfigure
(
user_id
int64
)
(
err
error
)
{
configure
:=
new
(
db
.
ImBase
)
configure
.
CompanyUserId
=
user_id
_
,
err
:=
db
.
MysqlClient
.
Delete
(
configure
)
return
err
_
,
err
=
db
.
MysqlClient
.
Delete
(
configure
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
return
}
func
GetConfigureAll
()
([]
*
db
.
ImBase
,
error
)
{
...
...
@@ -43,6 +57,9 @@ func GetConfigureAll() ([]*db.ImBase, error) {
if
num
==
0
{
return
configurs
,
nil
}
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
configurs
,
err
}
...
...
sevice/im_configure/main.go
View file @
0d8946a0
...
...
@@ -3,6 +3,8 @@ package im_configure
import
(
"context"
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"im-microservice/helper"
"im-microservice/pb"
...
...
@@ -82,6 +84,7 @@ func (cs *ConfigureSevice) Update(ctx context.Context,
}
// 更新数据
if
err
=
UpdateConfiguer
(
configure_add_struct
);
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
reply
=
&
pb
.
ConfigureReply
{}
...
...
sevice/im_user/im_user.go
View file @
0d8946a0
...
...
@@ -31,9 +31,9 @@ type ImUserQ struct {
//注册
func
(
u
*
ImUserServer
)
ImUserRegister
(
ctx
context
.
Context
,
in
*
pb
.
ImUserRequest
)
(
reply
*
pb
.
ImUserReply
,
err
error
)
{
imUser
:=
&
ImUser
{
Name
:
in
.
GetName
(),
Mobile
:
in
.
GetMobile
(),
Code
:
in
.
GetCode
(),
Name
:
in
.
GetName
(),
Mobile
:
in
.
GetMobile
(),
Code
:
in
.
GetCode
(),
Platfrom
:
in
.
GetPlatfrom
(),
}
...
...
sevice/im_user_relationship/db.go
View file @
0d8946a0
...
...
@@ -2,10 +2,14 @@ package im_user_relationship
import
(
"errors"
"github.com/astaxie/beego/orm"
"im-microservice/db"
"im-microservice/helper"
"im-microservice/validator_struct"
"github.com/astaxie/beego/orm"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var
(
...
...
@@ -41,12 +45,18 @@ func AddUserRelationship(in *validator_struct.UserRelationshipAdd) error {
us
.
Mute
=
open
us
.
Createtime
=
now
_
,
err
:=
db
.
MysqlClient
.
Insert
(
&
us
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
err
}
func
DBDelete
(
in
*
validator_struct
.
UserRelationshipDel
)
error
{
_
,
err
:=
db
.
MysqlClient
.
QueryTable
(
db_tabel
)
.
Filter
(
db_field_accid
,
in
.
Accid
)
.
Filter
(
db_field_faccid
,
in
.
Faccid
)
.
Delete
()
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
}
return
err
}
...
...
sevice/im_user_relationship/main.go
View file @
0d8946a0
...
...
@@ -2,7 +2,9 @@ package im_user_relationship
import
(
"context"
"errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"im-microservice/sevice/im_user"
"im-microservice/helper"
...
...
@@ -28,7 +30,7 @@ func (us *UserRelationshipService) Add(ctx context.Context,
return
}
if
ok
:=
IsUserRelationship
(
request
.
Accid
,
request
.
Faccid
);
ok
{
err
=
errors
.
New
(
"已经添加过该好友"
)
err
=
status
.
Error
(
codes
.
AlreadyExists
,
"已经添加过该好友"
)
return
}
//添加好友
...
...
@@ -82,6 +84,7 @@ func (us *UserRelationshipService) All(ctx context.Context,
results
,
err
:=
DBAll
(
request
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
reply
=
&
pb
.
UserRelationshipListReply
{}
...
...
@@ -108,6 +111,7 @@ func (us *UserRelationshipService) SetSpecialRelation(ctx context.Context,
return
}
if
err
=
SetSpecialRelationDb
(
request
);
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
reply
=
&
pb
.
UserRelationshipAddReply
{}
...
...
@@ -127,6 +131,7 @@ func (us *UserRelationshipService) ListBlackAndMuteList(ctx context.Context,
blacklist
,
mutelist
,
err
:=
ListBlackAndMuteListDB
(
request
)
if
err
!=
nil
{
err
=
status
.
Error
(
codes
.
Internal
,
err
.
Error
())
return
}
reply
=
&
pb
.
ListBlackAndMuteListReply
{}
...
...
validator_struct/validator_strucut.go
View file @
0d8946a0
...
...
@@ -201,13 +201,13 @@ func NewUserRelationshipAll(in map[string]interface{}) *UserRelationshipAll {
// 设置用户黑名单/禁音
type
SetSpecialRelation
struct
{
Accid
string
`validate:"required"`
TargetAcc
string
`validate:"required"`
RelationType
int64
`validate:"required,numeric,min=1"`
Value
int64
`validate:"required,numeric,min=1"`
Accid
string
`validate:"required"`
TargetAcc
string
`validate:"required"`
RelationType
int64
`validate:"required,numeric,min=1"`
Value
int64
`validate:"required,numeric,min=1"`
}
func
NewSetSpecialRelation
(
in
map
[
string
]
interface
{})
*
SetSpecialRelation
{
func
NewSetSpecialRelation
(
in
map
[
string
]
interface
{})
*
SetSpecialRelation
{
return
&
SetSpecialRelation
{
Accid
:
in
[
"accid"
]
.
(
string
),
TargetAcc
:
in
[
"faccid"
]
.
(
string
),
...
...
@@ -222,5 +222,5 @@ type ListBlackAndMuteList struct {
}
func
NewListBlackAndMuteList
(
in
map
[
string
]
interface
{})
*
ListBlackAndMuteList
{
return
&
ListBlackAndMuteList
{
Accid
:
in
[
"accid"
]
.
(
string
)}
}
\ No newline at end of file
return
&
ListBlackAndMuteList
{
Accid
:
in
[
"accid"
]
.
(
string
)}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment