https://pkg.go.dev/
go get -u gorm.io/grom
go get -u gorm.io/driver/sqlite
go get -u gorm.io/driver/mysql
go get -u github.com/gin-gonic/gin
go get github.com/spf13/viper
go get github.com/googollee/go-socket.io
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
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
}
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
数据库配置文件:
server:
port: 8888
mysql:
diverName: mysql
host: 127.0.0.1
port: 3306
database: chatapp
username: root
password: 123456
charset: utf8mb4
数据库 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
}
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"
}
package main
import (
"app/utils"
)
func main() {
utils.InitDB()
err := r.Run(":8888")
if err != nil {
return
}
}
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
}
package dto
import (
"app/models"
"app/utils"
)
func GetUserList() []*models.UserBasic {
data := make([]*models.UserBasic, 10)
utils.GetDB().Debug().Find(&data) // 查询所有用户
return data
}
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}) // 返回数据
}