Skip to content

Commit 1fd5ce6

Browse files
committed
feat: go chat 1.0
0 parents  commit 1fd5ce6

38 files changed

+2578
-0
lines changed

.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
bin/
2+
release/
3+
test/tmp/
4+
test/logs/
5+
logs
6+
doc/blueprint/
7+
*.iml
8+
*.swp
9+
*.log
10+
coverage.*
11+
y.output
12+
13+
.DS_Store
14+
.vscode/
15+
.idea
16+
_tools/
17+
18+
TestMarkdown2Html.html
19+
20+
#test log
21+
test/logs
22+
23+
#upload file
24+
static/img/*

Makefile

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
BINARY="chat"
2+
3+
# colors compatible setting
4+
CRED:=$(shell tput setaf 1 2>/dev/null)
5+
CGREEN:=$(shell tput setaf 2 2>/dev/null)
6+
CYELLOW:=$(shell tput setaf 3 2>/dev/null)
7+
CEND:=$(shell tput sgr0 2>/dev/null)
8+
9+
.PHONY: all
10+
all: | fmt build
11+
12+
# Code format
13+
.PHONY: fmt
14+
fmt:
15+
@echo "$(CGREEN)Run gofmt ...$(CEND)"
16+
@echo "gofmt -l -s -w ..."
17+
@ret=0 && for d in $$(go list -f '{{.Dir}}' ./... | grep -v /vendor/); do \
18+
gofmt -l -s -w $$d/*.go || ret=$$? ; \
19+
done ; exit $$ret
20+
21+
# build
22+
.PHONY: build-darwin
23+
build-darwin: fmt
24+
@echo "$(CGREEN)Building for darwin ...$(CEND)"
25+
@mkdir -p bin
26+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/${BINARY} cmd/main.go
27+
@echo "$(CGREEN)Build Success!$(CEND)"
28+
29+
# build
30+
.PHONY: build
31+
build: fmt
32+
@echo "$(CGREEN)Building for linux ...$(CEND)"
33+
@mkdir -p bin
34+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/${BINARY} cmd/main.go
35+
@echo "$(CGREEN)Build Success!$(CEND)"
36+
37+
# install
38+
install: build
39+
@echo "$(CGREEN)Install ...$(CEND)"
40+
go install ./...
41+
@echo "$(CGREEN)Install Success!$(CEND)"
42+
43+
# clean
44+
.PHONY: clean
45+
clean:
46+
@echo "$(CGREEN)Cleanup ...$(CEND)"
47+
go clean
48+
@rm -f bin/${BINARY}
49+
@echo "rm -f bin/${BINARY}"
50+
@for GOOS in darwin linux windows; do \
51+
for GOARCH in 386 amd64; do \
52+
rm -f bin/${BINARY}.$${GOOS}-$${GOARCH} ;\
53+
done ;\
54+
done
55+
rm -f ${BINARY} coverage.* test/tmp/*
56+
find . -name "*.log" -delete
57+
58+
# protoc build
59+
.PHONY: protoc
60+
protoc:
61+
protoc --gogo_out=. protocol/*.proto

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
使用go开发的聊天程序。
2+
前端代码仓库:
3+
https://github.com/kone-net/go-chat-web
4+
5+
已完成发送文字,截图,照片,文件,语音,视频等。

api/v1/file_router.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package v1
2+
3+
import (
4+
"io/ioutil"
5+
6+
"chat-room/global/log"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
// 前端通过文件名称获取文件流,显示文件
12+
func GetFile(c *gin.Context) {
13+
fileName := c.Param("fileName")
14+
log.Info(fileName)
15+
data, _ := ioutil.ReadFile("static/img/" + fileName)
16+
c.Writer.Write(data)
17+
}

api/v1/group_router.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package v1
2+
3+
import (
4+
"chat-room/model"
5+
"chat-room/response"
6+
"chat-room/service"
7+
"net/http"
8+
9+
"github.com/gin-gonic/gin"
10+
)
11+
// 获取分组列表
12+
func GetGroup(c *gin.Context) {
13+
uuid := c.Param("uuid")
14+
groups, err := service.GroupService.GetGroups(uuid)
15+
if err != nil {
16+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
17+
return
18+
}
19+
20+
c.JSON(http.StatusOK, response.SuccessMsg(groups))
21+
}
22+
23+
// 保存分组列表
24+
func SaveGroup(c *gin.Context) {
25+
uuid := c.Param("uuid")
26+
var group model.Group
27+
c.ShouldBindJSON(&group)
28+
29+
service.GroupService.SaveGroup(uuid, group)
30+
c.JSON(http.StatusOK, response.SuccessMsg(nil))
31+
}
32+
33+
// 加入组别
34+
func JoinGroup(c *gin.Context) {
35+
userUuid := c.Param("userUuid")
36+
groupUuid := c.Param("groupUuid")
37+
err := service.GroupService.JoinGroup(groupUuid, userUuid)
38+
if err != nil {
39+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
40+
return
41+
}
42+
c.JSON(http.StatusOK, response.SuccessMsg(nil))
43+
}
44+
45+
// 获取组内成员信息
46+
func GetGroupUsers(c *gin.Context) {
47+
groupUuid := c.Param("uuid")
48+
users := service.GroupService.GetUserIdByGroupUuid(groupUuid)
49+
c.JSON(http.StatusOK, response.SuccessMsg(users))
50+
}

api/v1/message_router.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package v1
2+
3+
import (
4+
"net/http"
5+
6+
"chat-room/global/log"
7+
"chat-room/model/request"
8+
"chat-room/response"
9+
"chat-room/service"
10+
11+
"github.com/gin-gonic/gin"
12+
)
13+
14+
// 获取消息列表
15+
func GetMessage(c *gin.Context) {
16+
log.Info(c.Query("uuid"))
17+
var messageRequest request.MessageRequest
18+
err := c.BindQuery(&messageRequest)
19+
if nil != err {
20+
log.Error("bindQueryError", log.Any("bindQueryError", err))
21+
}
22+
log.Info("messageRequest params: ", log.Any("messageRequest", messageRequest))
23+
24+
messages, err := service.MessageService.GetMessages(messageRequest)
25+
if err != nil {
26+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
27+
return
28+
}
29+
30+
c.JSON(http.StatusOK, response.SuccessMsg(messages))
31+
}

api/v1/user_router.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package v1
2+
3+
import (
4+
"net/http"
5+
6+
"chat-room/global/log"
7+
"chat-room/model"
8+
"chat-room/model/request"
9+
"chat-room/response"
10+
"chat-room/service"
11+
12+
"github.com/gin-gonic/gin"
13+
)
14+
15+
func Login(c *gin.Context) {
16+
var user model.User
17+
// c.BindJSON(&user)
18+
c.ShouldBindJSON(&user)
19+
log.Debug("user", log.Any("user", user))
20+
21+
if service.UserService.Login(&user) {
22+
c.JSON(http.StatusOK, response.SuccessMsg(user))
23+
return
24+
}
25+
26+
c.JSON(http.StatusOK, response.FailMsg("Login failed"))
27+
}
28+
29+
func Register(c *gin.Context) {
30+
var user model.User
31+
c.ShouldBindJSON(&user)
32+
err := service.UserService.Register(&user)
33+
if err != nil {
34+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
35+
return
36+
}
37+
38+
c.JSON(http.StatusOK, response.SuccessMsg(user))
39+
}
40+
41+
func ModifyUserInfo(c *gin.Context) {
42+
var user model.User
43+
c.ShouldBindJSON(&user)
44+
log.Debug("user", log.Any("user", user))
45+
if err := service.UserService.ModifyUserInfo(&user); err != nil {
46+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
47+
return
48+
}
49+
50+
c.JSON(http.StatusOK, response.SuccessMsg(nil))
51+
}
52+
53+
54+
func GetUserDetails(c *gin.Context) {
55+
uuid := c.Param("uuid")
56+
57+
c.JSON(http.StatusOK, response.SuccessMsg(service.UserService.GetUserDetails(uuid)))
58+
}
59+
60+
func GetUserList(c *gin.Context) {
61+
uuid := c.Query("uuid")
62+
c.JSON(http.StatusOK, response.SuccessMsg(service.UserService.GetUserList(uuid)))
63+
}
64+
65+
func AddFriend(c *gin.Context) {
66+
var userFriendRequest request.FriendRequest
67+
c.ShouldBindJSON(&userFriendRequest)
68+
69+
err := service.UserService.AddFriend(&userFriendRequest)
70+
if nil != err {
71+
c.JSON(http.StatusOK, response.FailMsg(err.Error()))
72+
return
73+
}
74+
75+
c.JSON(http.StatusOK, response.SuccessMsg(nil))
76+
}

chat.sql

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
DROP TABLE IF EXISTS `users`;
2+
CREATE TABLE IF NOT EXISTS `users` (
3+
`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',
4+
`uuid` varchar(150) NOT NULL COMMENT 'uuid',
5+
`username` varchar(191) NOT NULL COMMENT '''用户名''',
6+
`nickname` varchar(255) DEFAULT NULL COMMENT '昵称',
7+
`email` varchar(80) DEFAULT NULL COMMENT '邮箱',
8+
`password` varchar(150) NOT NULL COMMENT '密码',
9+
`avatar` varchar(250) NOT NULL COMMENT '头像',
10+
`create_at` datetime(3) DEFAULT NULL,
11+
`update_at` datetime(3) DEFAULT NULL,
12+
`delete_at` bigint DEFAULT NULL,
13+
PRIMARY KEY (`id`),
14+
UNIQUE KEY `username` (`username`),
15+
UNIQUE KEY `idx_uuid` (`uuid`),
16+
UNIQUE KEY `username_2` (`username`)
17+
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT '用户表';
18+
19+
20+
DROP TABLE IF EXISTS `user_friends`;
21+
CREATE TABLE IF NOT EXISTS `user_friends` (
22+
`id` int NOT NULL AUTO_INCREMENT COMMENT 'id',
23+
`created_at` datetime(3) DEFAULT NULL COMMENT '创建时间',
24+
`updated_at` datetime(3) DEFAULT NULL COMMENT '更新时间',
25+
`deleted_at` bigint unsigned DEFAULT NULL COMMENT '删除时间戳',
26+
`user_id` int DEFAULT NULL COMMENT '用户ID',
27+
`friend_id` int DEFAULT NULL COMMENT '好友ID',
28+
PRIMARY KEY (`id`),
29+
KEY `idx_user_friends_user_id` (`user_id`),
30+
KEY `idx_user_friends_friend_id` (`friend_id`)
31+
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT '好友信息表';
32+
33+
34+
DROP TABLE IF EXISTS `messages`;
35+
CREATE TABLE IF NOT EXISTS `messages` (
36+
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
37+
`created_at` datetime(3) DEFAULT NULL COMMENT '创建时间',
38+
`updated_at` datetime(3) DEFAULT NULL COMMENT '更新时间',
39+
`deleted_at` bigint unsigned DEFAULT NULL COMMENT '删除时间戳',
40+
`from_user_id` int DEFAULT NULL COMMENT '发送人ID',
41+
`to_user_id` int DEFAULT NULL COMMENT '发送对象ID',
42+
`content` varchar(2500) DEFAULT NULL COMMENT '消息内容',
43+
`url` varchar(350) DEFAULT NULL COMMENT '''文件或者图片地址''',
44+
`pic` text COMMENT '缩略图',
45+
`message_type` smallint DEFAULT NULL COMMENT '''消息类型:1单聊,2群聊''',
46+
`content_type` smallint DEFAULT NULL COMMENT '''消息内容类型:1文字,2语音,3视频''',
47+
PRIMARY KEY (`id`),
48+
KEY `idx_messages_deleted_at` (`deleted_at`),
49+
KEY `idx_messages_from_user_id` (`from_user_id`),
50+
KEY `idx_messages_to_user_id` (`to_user_id`)
51+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT '消息表';
52+
53+
54+
DROP TABLE IF EXISTS `groups`;
55+
CREATE TABLE IF NOT EXISTS `groups` (
56+
`id` int NOT NULL AUTO_INCREMENT,
57+
`created_at` datetime(3) DEFAULT NULL,
58+
`updated_at` datetime(3) DEFAULT NULL,
59+
`deleted_at` bigint unsigned DEFAULT NULL,
60+
`user_id` int DEFAULT NULL COMMENT '''群主ID''',
61+
`name` varchar(150) DEFAULT NULL COMMENT '''群名称',
62+
`notice` varchar(350) DEFAULT NULL COMMENT '''群公告',
63+
`uuid` varchar(150) NOT NULL COMMENT '''uuid''',
64+
PRIMARY KEY (`id`),
65+
KEY `idx_groups_user_id` (`user_id`)
66+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT '群组表';
67+
68+
69+
DROP TABLE IF EXISTS `group_members`;
70+
CREATE TABLE IF NOT EXISTS `group_members` (
71+
`id` int NOT NULL AUTO_INCREMENT,
72+
`created_at` datetime(3) DEFAULT NULL,
73+
`updated_at` datetime(3) DEFAULT NULL,
74+
`deleted_at` bigint unsigned DEFAULT NULL,
75+
`user_id` int DEFAULT NULL COMMENT '''用户ID''',
76+
`group_id` int DEFAULT NULL COMMENT '''群组ID''',
77+
`nickname` varchar(350) DEFAULT NULL COMMENT '''昵称',
78+
`mute` smallint DEFAULT NULL COMMENT '''是否禁言''',
79+
PRIMARY KEY (`id`),
80+
KEY `idx_group_members_user_id` (`user_id`),
81+
KEY `idx_group_members_group_id` (`group_id`)
82+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT '群组成员表';

cmd/main.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"chat-room/global/log"
5+
"chat-room/config"
6+
"chat-room/router"
7+
"chat-room/server"
8+
"go.uber.org/zap"
9+
"net/http"
10+
"time"
11+
)
12+
13+
func main() {
14+
log.InitLogger(config.GetConfig().Log.Path, config.GetConfig().Log.Level)
15+
log.Info("config", zap.Any("config", config.GetConfig()))
16+
17+
log.Info("start server", zap.String("start", "start web sever..."))
18+
19+
newRouter := router.NewRouter()
20+
21+
go server.MyServer.Start()
22+
23+
s := &http.Server{
24+
Addr: "127.0.0.1:8888",
25+
Handler: newRouter,
26+
ReadTimeout: 10 * time.Second,
27+
WriteTimeout: 10 * time.Second,
28+
MaxHeaderBytes: 1 << 20,
29+
}
30+
err := s.ListenAndServe()
31+
if nil != err {
32+
log.Error("server error", zap.Any("serverError", err))
33+
}
34+
}

0 commit comments

Comments
 (0)