-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
553 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,13 @@ jobs: | |
fetch-depth: 0 | ||
- name: 'Qodana Scan' | ||
uses: JetBrains/[email protected] | ||
with: | ||
pr-mode: false | ||
args: --apply-fixes | ||
push-fixes: pull-request | ||
upload-result: true | ||
env: | ||
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} | ||
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} | ||
- uses: github/codeql-action/upload-sarif@v2 | ||
with: | ||
sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package models | ||
|
||
import ( | ||
"GuGoTik/src/storage/database" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type Message struct { | ||
ID uint32 `gorm:"not null;primarykey;autoIncrement"` | ||
ToUserId uint32 `gorm:"not null" ` | ||
FromUserId uint32 `gorm:"not null"` | ||
ConversationId string `gorm:"not null" index:"conversationid"` | ||
Content string `gorm:"not null"` | ||
|
||
// Create_time time.Time `gorm:"not null"` | ||
//Updatetime deleteTime | ||
gorm.Model | ||
} | ||
|
||
func init() { | ||
if err := database.Client.AutoMigrate(&Message{}); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package main | ||
|
||
import ( | ||
"GuGoTik/src/constant/config" | ||
"GuGoTik/src/constant/strings" | ||
"GuGoTik/src/extra/tracing" | ||
"GuGoTik/src/models" | ||
"GuGoTik/src/rpc/chat" | ||
"GuGoTik/src/rpc/user" | ||
"GuGoTik/src/storage/database" | ||
grpc2 "GuGoTik/src/utils/grpc" | ||
"GuGoTik/src/utils/logging" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
var UserClient user.UserServiceClient | ||
|
||
type MessageServiceImpl struct { | ||
chat.ChatServiceServer | ||
} | ||
|
||
func init() { | ||
userRpcConn := grpc2.Connect(config.UserRpcServerName) | ||
UserClient = user.NewUserServiceClient(userRpcConn) | ||
} | ||
|
||
func (c MessageServiceImpl) ChatAction(ctx context.Context, request *chat.ActionRequest) (res *chat.ActionResponse, err error) { | ||
ctx, span := tracing.Tracer.Start(ctx, "ChatActionService") | ||
defer span.End() | ||
logger := logging.LogService("ChatService.ActionMessage").WithContext(ctx) | ||
|
||
logger.WithFields(logrus.Fields{ | ||
"ActorId": request.ActorId, | ||
"user_id": request.UserId, | ||
"action_type": request.ActionType, | ||
"content_text": request.Content, | ||
}).Debugf("Process start") | ||
|
||
userResponse, err := UserClient.GetUserInfo(ctx, &user.UserRequest{ | ||
ActorId: request.ActorId, | ||
UserId: request.UserId, | ||
}) | ||
|
||
if err != nil || userResponse.StatusCode != strings.ServiceOKCode { | ||
logger.WithFields(logrus.Fields{ | ||
"err": err, | ||
"cctor_id": request.ActorId, | ||
}).Errorf("User service error") | ||
logging.SetSpanError(span, err) | ||
|
||
return &chat.ActionResponse{ | ||
StatusCode: strings.UnableToAddMessageErrorCode, | ||
StatusMsg: strings.UnableToAddMessageRrror, | ||
}, err | ||
} | ||
|
||
res, err = addMessage(ctx, logger, span, request.ActorId, request.UserId, request.Content) | ||
if err != nil { | ||
logger.WithFields(logrus.Fields{ | ||
"err": err, | ||
"ActorId": request.ActorId, | ||
}).Errorf("User service error") | ||
logging.SetSpanError(span, err) | ||
return res, err | ||
} | ||
|
||
logger.WithFields(logrus.Fields{ | ||
"response": res, | ||
}).Debugf("Process done.") | ||
|
||
return res, err | ||
} | ||
|
||
// Chat(context.Context, *ChatRequest) (*ChatResponse, error) | ||
func (c MessageServiceImpl) Chat(ctx context.Context, request *chat.ChatRequest) (resp *chat.ChatResponse, err error) { | ||
ctx, span := tracing.Tracer.Start(ctx, "ChatService") | ||
defer span.End() | ||
logger := logging.LogService("ChatService.chat").WithContext(ctx) | ||
logger.WithFields(logrus.Fields{ | ||
"user_id": request.UserId, | ||
"ActorId": request.ActorId, | ||
}).Debugf("Process start") | ||
toUserId := request.UserId | ||
fromUserId := request.ActorId | ||
|
||
conversationId := fmt.Sprintf("%d_%d", toUserId, fromUserId) | ||
|
||
if toUserId > fromUserId { | ||
conversationId = fmt.Sprintf("%d_%d", fromUserId, toUserId) | ||
} | ||
//这个地方应该取出多少条消息? | ||
//TO DO 看怎么需要一下 | ||
|
||
var rMessageList []*chat.Message | ||
result := database.Client.WithContext(ctx).Where("conversation_id=?", conversationId). | ||
Order("created_at desc").Find(&rMessageList) | ||
|
||
if result.Error != nil { | ||
logger.WithFields(logrus.Fields{ | ||
"err": result.Error, | ||
}).Errorf("ChatServiceImpl list chat failed to response when listing message") | ||
logging.SetSpanError(span, err) | ||
|
||
resp = &chat.ChatResponse{ | ||
StatusCode: strings.UnableToQueryMessageErrorCode, | ||
StatusMsg: strings.UnableToQueryMessageError, | ||
} | ||
return | ||
} | ||
|
||
resp = &chat.ChatResponse{ | ||
StatusCode: strings.ServiceOKCode, | ||
StatusMsg: strings.ServiceOK, | ||
MessageList: rMessageList, | ||
} | ||
|
||
logger.WithFields(logrus.Fields{ | ||
"response": resp, | ||
}).Debugf("Process done.") | ||
|
||
return | ||
} | ||
|
||
func addMessage(ctx context.Context, logger *logrus.Entry, span trace.Span, fromUserId uint32, toUserId uint32, Context string) (resp *chat.ActionResponse, err error) { | ||
conversationId := fmt.Sprintf("%d_%d", toUserId, fromUserId) | ||
|
||
if toUserId > fromUserId { | ||
conversationId = fmt.Sprintf("%d_%d", fromUserId, toUserId) | ||
} | ||
message := models.Message{ | ||
ToUserId: toUserId, | ||
FromUserId: fromUserId, | ||
Content: Context, | ||
ConversationId: conversationId, | ||
} | ||
|
||
//TO_DO 后面写mq? | ||
result := database.Client.WithContext(ctx).Create(&message) | ||
|
||
if result.Error != nil { | ||
//TO_DO 错误 替换 | ||
logger.WithFields(logrus.Fields{ | ||
"err": result.Error, | ||
"id": message.ID, | ||
"ActorId": message.FromUserId, | ||
"to_id": message.ToUserId, | ||
}).Errorf("send message failed when insert to database") | ||
|
||
resp = &chat.ActionResponse{ | ||
StatusCode: strings.UnableToAddMessageErrorCode, | ||
StatusMsg: strings.UnableToAddMessageRrror, | ||
} | ||
return | ||
} | ||
|
||
resp = &chat.ActionResponse{ | ||
StatusCode: strings.ServiceOKCode, | ||
StatusMsg: strings.ServiceOK, | ||
} | ||
return | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"GuGoTik/src/constant/config" | ||
"GuGoTik/src/extra/profiling" | ||
"GuGoTik/src/extra/tracing" | ||
"GuGoTik/src/rpc/chat" | ||
"GuGoTik/src/rpc/health" | ||
healthImpl "GuGoTik/src/services/health" | ||
"GuGoTik/src/utils/consul" | ||
"GuGoTik/src/utils/logging" | ||
"context" | ||
"net" | ||
|
||
"github.com/sirupsen/logrus" | ||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func main() { | ||
tp, err := tracing.SetTraceProvider(config.MessageRpcServerName) | ||
|
||
if err != nil { | ||
logging.Logger.WithFields(logrus.Fields{ | ||
"err": err, | ||
}).Panicf("Error to set the trace") | ||
} | ||
defer func() { | ||
if err := tp.Shutdown(context.Background()); err != nil { | ||
logging.Logger.WithFields(logrus.Fields{ | ||
"err": err, | ||
}).Errorf("Error to set the trace") | ||
} | ||
}() | ||
|
||
// Configure Pyroscope | ||
profiling.InitPyroscope("GuGoTik.ChatService") | ||
|
||
s := grpc.NewServer( | ||
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()), | ||
) | ||
|
||
log := logging.LogService(config.MessageRpcServerName) | ||
|
||
lis, err := net.Listen("tcp", config.MessageRpcServerPort) | ||
|
||
if err != nil { | ||
log.Panicf("Rpc %s listen happens error: %v", config.MessageRpcServerName, err) | ||
} | ||
|
||
var srv MessageServiceImpl | ||
var probe healthImpl.ProbeImpl | ||
|
||
chat.RegisterChatServiceServer(s, srv) | ||
|
||
health.RegisterHealthServer(s, &probe) | ||
|
||
if err := consul.RegisterConsul(config.MessageRpcServerName, config.MessageRpcServerPort); err != nil { | ||
log.Panicf("Rpc %s register consul happens error for: %v", config.MessageRpcServerName, err) | ||
} | ||
log.Infof("Rpc %s is running at %s now", config.MessageRpcServerName, config.MessageRpcServerPort) | ||
if err := s.Serve(lis); err != nil { | ||
log.Panicf("Rpc %s listen happens error for: %v", config.MessageRpcServerName, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.