基于 gin + websocket 即时通讯项目
1、安装环境与初始化
- 搜索各种包官网
https://pkg.go.dev/
1.1 安装 grom
go get -u gorm.io/grom
1.2 安装 MySQL 驱动
go get -u gorm.io/driver/sqlite
go get -u gorm.io/driver/mysql
1.3 安装 gin
go get -u github.com/gin-gonic/gin
1.4 安装 viper
go get github.com/spf13/viper
1.5 安装 gin
go get github.com/googollee/go-socket.io
1.6 安装 swaggo
- 安装
go get -u github.com/swaggo/swag/cmd/swag
go install github.com/swaggo/swag/cmd/swag@latest
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
- 初始化
swag init
-
配置
-
app
- router
- router.go
- router
package router
import (
"app/docs"
"app/service"
"github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func Router() *gin.Engine {
r := gin.Default()
r.GET("swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
docs.SwaggerInfo.BasePath = ""
r.GET("/index", service.GetIndex)
userGroup := r.Group("api/user")
userGroup.GET("/getUserList", service.GetUserList)
return r
}
- app
- service
- user.go
- service
package service
import (
"app/dto"
"github.com/gin-gonic/gin"
)
// @title 查询所有用户
// @version 1.0
// @Tags 用户
// @contact.name 通过用户手机号获取购物车信息
// @Success 200 {string} data
// @Router /api/user/getUserList [get]
// @host 127.0.0.1:9999
func GetUserList(ctx *gin.Context) {
data := dto.GetUserList()
ctx.JSON(200, gin.H{"data": data})
}
注意: 注解之后需要重新初始化 然后重启项目
swag init
- 访问
http://127.0.0.1:8888/swagger/index.html
1.7 数据库初始化
- app
- config
- application.yml
- config
数据库配置文件:
server:
port: 8888
mysql:
diverName: mysql
host: 127.0.0.1
port: 3306
database: chatapp
username: root
password: 123456
charset: utf8mb4
- app
- utils
- system_init.go
- utils
数据库 MySQL 初始化文件:
package utils
import (
"fmt"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
var DB *gorm.DB
func InitDB() *gorm.DB {
viper.SetConfigName("application") // 你的数据库配置 yml 文件名
viper.AddConfigPath("config") // yml 的文件路径
err := viper.ReadInConfig()
if err != nil {
fmt.Println(err)
}
host := viper.GetString("mysql.host")
port := viper.GetString("mysql.port")
database := viper.GetString("mysql.database")
username := viper.GetString("mysql.username")
password := viper.GetString("mysql.password")
charset := viper.GetString("mysql.charset")
// 自定义 log 日志
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Info,
Colorful: true,
})
db, err := gorm.Open(mysql.Open(username+`:`+password+`@tcp(`+host+`:`+port+`)/`+database+`?charset=`+charset+`&parseTime=true&loc=Local`),
&gorm.Config{Logger: newLogger})
if err != nil {
panic("failed to connect database, err: " + err.Error())
}
//db.AutoMigrate(&models.UserBasic{}) // 迁移数据
DB = db
return db
}
func GetDB() *gorm.DB {
return DB
}
- app
- models
- userBasic.go
- models
package models
import (
"gorm.io/gorm"
"time"
)
type UserBasic struct {
gorm.Model
Name string
Password string
Phone string
Email string
Identity uint64 // 唯一标识
ClientIp string // 设备
ClientPort string // 端口
LoginTime time.Time // 登录时间
HeartBeatTime time.Time // 心跳
LoginOutTime time.Time // 登出时间
IsLoginOut bool // 是否下线
DeviceInfo string //设备信息
}
func (table *UserBasic) TableName() string {
return "user_basic"
}
- app
- main.go
package main
import (
"app/utils"
)
func main() {
utils.InitDB()
err := r.Run(":8888")
if err != nil {
return
}
}
1.8 测试
- app
- router
- router.go
- router
package router
import (
"app/service"
"github.com/gin-gonic/gin"
)
func Router() *gin.Engine {
r := gin.Default()
r.GET("/index", service.GetIndex)
userGroup := r.Group("api/user") // 路由组
userGroup.GET("/getUserList", service.GetUserList)
return r
}
- app
- dto
- user.go
- dto
package dto
import (
"app/models"
"app/utils"
)
func GetUserList() []*models.UserBasic {
data := make([]*models.UserBasic, 10)
utils.GetDB().Debug().Find(&data) // 查询所有用户
return data
}
- app
- service
- user.go
- service
package service
import (
"app/dto"
"github.com/gin-gonic/gin"
)
func GetUserList(ctx *gin.Context) {
data := dto.GetUserList() // 调用 dto 层方法获取数据
ctx.JSON(200, gin.H{"data": data}) // 返回数据
}