提交 50cac8f0 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

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

上级 f6a0b05e
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)
}
......@@ -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
)
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
}
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
}
package registTable
import (
"github.com/jinzhu/gorm"
"main/model/dbModel"
)
func RegistTable(db *gorm.DB) {
db.AutoMigrate(dbModel.User{})
}
......@@ -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,
......
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
}
package model
package modelInterface
// 因为我也不确定项目要不要多人维护 所以定义了CURD接口 凡是对数据库进行简单CURD操作 请实现此接口 默认首位返回 error
type CURD interface {
......
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)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册