基于 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 routerimport ("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 serviceimport ("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: 8888mysql:diverName: mysqlhost: 127.0.0.1port: 3306database: chatappusername: rootpassword: 123456charset: utf8mb4
- app
- utils
- system_init.go
- utils
数据库 MySQL 初始化文件:
package utilsimport ("fmt""github.com/spf13/viper""gorm.io/driver/mysql""gorm.io/gorm""gorm.io/gorm/logger""log""os""time"
)var DB *gorm.DBfunc 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 = dbreturn db
}func GetDB() *gorm.DB {return DB
}
- app
- models
- userBasic.go
- models
package modelsimport ("gorm.io/gorm""time"
)type UserBasic struct {gorm.ModelName stringPassword stringPhone stringEmail stringIdentity 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 mainimport ("app/utils"
)func main() {utils.InitDB()err := r.Run(":8888")if err != nil {return}
}
1.8 测试
- app
- router
- router.go
- router
package routerimport ("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 dtoimport ("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 serviceimport ("app/dto""github.com/gin-gonic/gin"
)func GetUserList(ctx *gin.Context) {data := dto.GetUserList() // 调用 dto 层方法获取数据ctx.JSON(200, gin.H{"data": data}) // 返回数据
}