練手小專案,為熟悉Gin框架跟websocket使用 ??????
線上demo (PS: 請儘量使用Chrome遊覽器,開啟多個不同使用者遊覽器即可體驗效果)
feature
- 登入/註冊(防止重複登入)
- 群聊(支援文字、emoji、檔案(圖片)上傳、多房間)
- 私聊(訊息提醒)
- 歷史訊息檢視(點選載入更多)
- 心跳檢測,來自 github.com/zimv/websocket-heartbea...
- go mod 包管理
- 靜態資源嵌入,執行只依賴編譯好的可執行檔案與mysql
- 支援 http/ws 、 https/wss
結構
.
|-- bindata
|-- conf
|-- controller
|-- models
|-- routes
|-- services
| |-- helper
| |-- img_kr
| |-- message_service
| |-- session
| |-- user_service
| `-- validator
|-- sql
|-- static
| |-- emoji
| |-- images
| | |-- rooms
| | |-- theme
| | `-- user
| |-- javascripts
| |-- rolling
| | |-- css
| | `-- js
| `-- stylesheets
|-- tmp
|-- views
`-- ws
介面
database
mysql
CREATE TABLE `messages` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT '使用者ID',
`room_id` int(11) NOT NULL COMMENT '房間ID',
`to_user_id` int(11) NULL DEFAULT 0 COMMENT '私聊使用者ID',
`content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '聊天內容',
`image_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '圖片URL',
`created_at` datetime(0) NULL DEFAULT NULL,
`updated_at` datetime(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0),
`deleted_at` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_user_id`(`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '暱稱',
`password` varchar(125) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '密碼',
`avatar_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '1' COMMENT '頭像ID',
`created_at` datetime(0) NULL DEFAULT NULL,
`updated_at` datetime(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0),
`deleted_at` datetime(0) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `username`(`username`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = DYNAMIC;
Tools
- 模板提供
- github.com/gin-gonic/gin
- gorm.io/driver/mysql
- gorm.io/gorm
- github.com/gravityblast/fresh
- github.com/valyala/fasthttp
- github.com/spf13/viper
- blog.hi917.com/detail/87.html
使用
# 自行匯入資料庫檔案 sql/go_gin_chat.sql
git clone github.com/hezhizheng/go-gin-chat
cd go-gin-chat
cp conf/config.go.env conf/config.go // 根據實際情況修改配置
go-bindata -o=bindata/bindata.go -pkg=bindata ./static/... ./views/... // 安裝請參考 https://blog.hi917.com/detail/87.html
go run main.go
nginx 部署
server {
listen 80;
listen 443 ssl http2;
server_name go-gin-chat.hzz.cool;
#ssl on;
ssl_certificate xxxpath\cert.pem;
ssl_certificate_key xxxpath\key.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location ~ .*\.(gif|jpg|png|css|js)(.*) {
proxy_pass http://127.0.0.1:8322;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache cache_one;
proxy_cache_valid 200 302 24h;
proxy_cache_valid 301 30d;
proxy_cache_valid any 5m;
expires 90d;
add_header wall "Big brother is watching you";
}
location / {
try_files /_not_exists_ @backend;
}
location @backend {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8322;
}
location /ws {
proxy_pass http://127.0.0.1:8322;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 6000s;
}
編譯可執行檔案(跨平臺)
# 用法參考 https://github.com/mitchellh/gox
# 生成檔案可直接執行 Linux
gox -osarch="linux/amd64"
......
Tip
- 修改靜態檔案需要執行
go-bindata -o=bindata/bindata.go -pkg=bindata ./static/... ./views/...
重新編譯
todo
- 心跳機制
- 多頻道聊天
- 私聊
- 線上使用者列表
- https支援
本作品採用《CC 協議》,轉載必須註明作者和本文連結