diff --git a/QMPlusServer/controller/api/user.go b/QMPlusServer/controller/api/user.go new file mode 100644 index 00000000..da11c06c --- /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 9acf3448..e040b123 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 835cacc0..84adb1a3 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 7a36dc16..b2dabedf 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 00000000..c54debef --- /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 bcb11334..ace5e798 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 b36e0c29..b1e7f448 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 45acda7d..bd8f0a39 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 00000000..91784c17 --- /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) + } +}