Browse Source

swagger配置 数据库初始化结构调整

main
pixelqm 5 years ago
parent
commit
50cac8f02d
  1. 26
      QMPlusServer/controller/api/user.go
  2. 2
      QMPlusServer/go.mod
  3. 11
      QMPlusServer/init/initRouter/initRouter.go
  4. 6
      QMPlusServer/init/mysql/initMysql.go
  5. 10
      QMPlusServer/init/registTable/registTable.go
  6. 23
      QMPlusServer/main.go
  7. 7
      QMPlusServer/model/dbModel/user.go
  8. 2
      QMPlusServer/model/modelInterface/interface.go
  9. 13
      QMPlusServer/router/user.go

26
QMPlusServer/controller/api/user.go

@ -0,0 +1,26 @@
package api
import (
"fmt"
"github.com/gin-gonic/gin"
"main/model/dbModel"
)
type RegistStuct struct {
UserName string `json:"userName"`
PassWord string `json:"passWord"`
}
// @Summary 创建用户
// @Produce application/x-www-form-urlencoded
// @Param data body api.RegistStuct true "用户注册接口"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /user/regist [post]
func Regist(c *gin.Context) {
var U dbModel.User
_ = c.BindJSON(&U)
fmt.Println(U)
err, user := U.Create()
fmt.Println(err, user)
}

2
QMPlusServer/go.mod

@ -3,6 +3,7 @@ module main
go 1.12 go 1.12
require ( require (
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/fsnotify/fsnotify v1.4.7 github.com/fsnotify/fsnotify v1.4.7
@ -17,5 +18,6 @@ require (
github.com/sirupsen/logrus v1.2.0 github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.4.0 github.com/spf13/viper v1.4.0
github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.5.1
github.com/tebeka/strftime v0.1.3 // indirect github.com/tebeka/strftime v0.1.3 // indirect
) )

11
QMPlusServer/init/initRouter.go → QMPlusServer/init/initRouter/initRouter.go

@ -1,14 +1,17 @@
package init
package initRouter
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/swaggo/gin-swagger" "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles" "github.com/swaggo/gin-swagger/swaggerFiles"
_ "main/docs"
"main/router"
) )
var Router = gin.Default()
func InitRouter() {
func InitRouter() *gin.Engine {
var Router = gin.Default()
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//Router.Use(middleware.Logger()) //Router.Use(middleware.Logger())
router.InitUserRouter(Router)
return Router
} }

6
QMPlusServer/init/initMysql.go → QMPlusServer/init/mysql/initMysql.go

@ -1,4 +1,4 @@
package init
package mysql
import ( import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
@ -9,13 +9,13 @@ import (
var DEFAULTDB *gorm.DB var DEFAULTDB *gorm.DB
func InitMysql(admin config.Admin) {
func InitMysql(admin config.Admin) *gorm.DB {
if db, err := gorm.Open("mysql", admin.UserName+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil { if db, err := gorm.Open("mysql", admin.UserName+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname+"?"+admin.Config); err != nil {
log.Printf("DEFAULTDB数据库启动异常%S", err) log.Printf("DEFAULTDB数据库启动异常%S", err)
} else { } else {
DEFAULTDB = db DEFAULTDB = db
DEFAULTDB.DB().SetMaxIdleConns(10) DEFAULTDB.DB().SetMaxIdleConns(10)
DEFAULTDB.DB().SetMaxIdleConns(100) DEFAULTDB.DB().SetMaxIdleConns(100)
DEFAULTDB.AutoMigrate()
} }
return DEFAULTDB
} }

10
QMPlusServer/init/registTable/registTable.go

@ -0,0 +1,10 @@
package registTable
import (
"github.com/jinzhu/gorm"
"main/model/dbModel"
)
func RegistTable(db *gorm.DB) {
db.AutoMigrate(dbModel.User{})
}

23
QMPlusServer/main.go

@ -2,28 +2,21 @@ package main
import ( import (
"main/config" "main/config"
"main/init"
"main/init/initRouter"
"main/init/mysql"
"main/init/registTable"
"net/http" "net/http"
"time" "time"
) )
// @Summary 打印测试功能
// @title Swagger Example API
// @version 0.0.1
// @description This is a sample server Petstore server.
// @BasePath /api/v1
// @Host 127.0.0.1:8080
// @Produce json
// @Param name query string true "Name"
// @Success 200 {string} json "{"code":200,"data":"name","msg":"ok"}"
// @Router / [get]
func main() { func main() {
init.InitMysql(config.Dbconfig.Admin)
defer init.DEFAULTDB.Close()
init.InitRouter()
registTable.RegistTable(mysql.InitMysql(config.Dbconfig.Admin))
defer mysql.DEFAULTDB.Close()
Router := initRouter.InitRouter()
s := &http.Server{ s := &http.Server{
Addr: ":8888", Addr: ":8888",
Handler: init.Router,
Handler: Router,
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,

7
QMPlusServer/model/user.go → QMPlusServer/model/dbModel/user.go

@ -1,8 +1,9 @@
package model
package dbModel
import ( import (
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"main/init/mysql"
) )
type User struct { type User struct {
@ -19,3 +20,7 @@ type User struct {
//type Propertie struct { //type Propertie struct {
// gorm.Model // gorm.Model
//} //}
func (u *User) Create() (err error, user interface{}) {
err = mysql.DEFAULTDB.Create(u).Error
return err, user
}

2
QMPlusServer/model/interface.go → QMPlusServer/model/modelInterface/interface.go

@ -1,4 +1,4 @@
package model
package modelInterface
// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error // 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error
type CURD interface { type CURD interface {

13
QMPlusServer/router/user.go

@ -0,0 +1,13 @@
package router
import (
"github.com/gin-gonic/gin"
"main/controller/api"
)
func InitUserRouter(Router *gin.Engine) {
UserRouter := Router.Group("user")
{
UserRouter.POST("regist", api.Regist)
}
}
Loading…
Cancel
Save