diff --git a/QMPlusServer/controller/api/user.go b/QMPlusServer/controller/api/user.go new file mode 100644 index 0000000000000000000000000000000000000000..da11c06cfae160578125d0e3b501dbbb4ba41023 --- /dev/null +++ b/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) +} diff --git a/QMPlusServer/go.mod b/QMPlusServer/go.mod index 9acf3448668d722e9abb00ee1ca55f0a95ad23ba..e040b12357a16be2dd99e911cd33509657f3684e 100644 --- a/QMPlusServer/go.mod +++ b/QMPlusServer/go.mod @@ -3,6 +3,7 @@ module main go 1.12 require ( + github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/fsnotify/fsnotify v1.4.7 @@ -17,5 +18,6 @@ require ( github.com/sirupsen/logrus v1.2.0 github.com/spf13/viper v1.4.0 github.com/swaggo/gin-swagger v1.2.0 + github.com/swaggo/swag v1.5.1 github.com/tebeka/strftime v0.1.3 // indirect ) diff --git a/QMPlusServer/init/initRouter.go b/QMPlusServer/init/initRouter/initRouter.go similarity index 59% rename from QMPlusServer/init/initRouter.go rename to QMPlusServer/init/initRouter/initRouter.go index 835cacc046cb3bcd84938939f61b1c87f4ca443e..84adb1a340fd9433c7df23fcc050684910645b39 100644 --- a/QMPlusServer/init/initRouter.go +++ b/QMPlusServer/init/initRouter/initRouter.go @@ -1,14 +1,17 @@ -package init +package initRouter import ( "github.com/gin-gonic/gin" "github.com/swaggo/gin-swagger" "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.Use(middleware.Logger()) + router.InitUserRouter(Router) + return Router } diff --git a/QMPlusServer/init/initMysql.go b/QMPlusServer/init/mysql/initMysql.go similarity index 84% rename from QMPlusServer/init/initMysql.go rename to QMPlusServer/init/mysql/initMysql.go index 7a36dc16c34750eb5e20ca1aa6ff164a7109cb25..b2dabedfff269f49f5ec150558d0a008eca231ff 100644 --- a/QMPlusServer/init/initMysql.go +++ b/QMPlusServer/init/mysql/initMysql.go @@ -1,4 +1,4 @@ -package init +package mysql import ( "github.com/jinzhu/gorm" @@ -9,13 +9,13 @@ import ( 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 { log.Printf("DEFAULTDB数据库启动异常%S", err) } else { DEFAULTDB = db DEFAULTDB.DB().SetMaxIdleConns(10) DEFAULTDB.DB().SetMaxIdleConns(100) - DEFAULTDB.AutoMigrate() } + return DEFAULTDB } diff --git a/QMPlusServer/init/registTable/registTable.go b/QMPlusServer/init/registTable/registTable.go new file mode 100644 index 0000000000000000000000000000000000000000..c54debefff2835b479a72ed94282a33c6ce89e13 --- /dev/null +++ b/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{}) +} diff --git a/QMPlusServer/main.go b/QMPlusServer/main.go index bcb11334c103a9a0ebcc7c3056d4e5f3d9afa5f2..ace5e798ee96bd5ced3c111d7a2de91d1d078458 100644 --- a/QMPlusServer/main.go +++ b/QMPlusServer/main.go @@ -2,28 +2,21 @@ package main import ( "main/config" - "main/init" + "main/init/initRouter" + "main/init/mysql" + "main/init/registTable" "net/http" "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() { - 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{ Addr: ":8888", - Handler: init.Router, + Handler: Router, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, diff --git a/QMPlusServer/model/user.go b/QMPlusServer/model/dbModel/user.go similarity index 79% rename from QMPlusServer/model/user.go rename to QMPlusServer/model/dbModel/user.go index b36e0c296dc23e83e02caa7e825fc8de4f133caf..b1e7f44822cb7e188d961abcf49632e9b59094a2 100644 --- a/QMPlusServer/model/user.go +++ b/QMPlusServer/model/dbModel/user.go @@ -1,8 +1,9 @@ -package model +package dbModel import ( "github.com/jinzhu/gorm" uuid "github.com/satori/go.uuid" + "main/init/mysql" ) type User struct { @@ -19,3 +20,7 @@ type User struct { //type Propertie struct { // gorm.Model //} +func (u *User) Create() (err error, user interface{}) { + err = mysql.DEFAULTDB.Create(u).Error + return err, user +} diff --git a/QMPlusServer/model/interface.go b/QMPlusServer/model/modelInterface/interface.go similarity index 93% rename from QMPlusServer/model/interface.go rename to QMPlusServer/model/modelInterface/interface.go index 45acda7d456a2ab3c1d07b5e3af271b3055d3974..bd8f0a396bdd3e75c257b2bec4406b4bdabf04d1 100644 --- a/QMPlusServer/model/interface.go +++ b/QMPlusServer/model/modelInterface/interface.go @@ -1,4 +1,4 @@ -package model +package modelInterface // 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error type CURD interface { diff --git a/QMPlusServer/router/user.go b/QMPlusServer/router/user.go new file mode 100644 index 0000000000000000000000000000000000000000..91784c179e750455fbdcf60347f6c66565deae0e --- /dev/null +++ b/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) + } +}