[FIX]使用postgresql
This commit is contained in:
parent
e98c0f3b34
commit
d81d3b6ca9
|
@ -10,9 +10,10 @@ READ_TIMEOUT = 60
|
||||||
WRITE_TIMEOUT = 60
|
WRITE_TIMEOUT = 60
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
TYPE = mysql
|
TYPE = postgres
|
||||||
USER = root
|
USER = postgres
|
||||||
PASSWORD = a19960425
|
PASSWORD = a19960425
|
||||||
HOST = localhost:3306
|
HOST = 0.0.0.0
|
||||||
|
PORT = 5432
|
||||||
NAME = blog
|
NAME = blog
|
||||||
TABLE_PREFIX = blog_
|
TABLE_PREFIX = blog_
|
||||||
|
|
9
go.mod
9
go.mod
|
@ -3,26 +3,21 @@ module github.com/go-pripro/shop
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/astaxie/beego v1.12.2
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/gin-gonic/gin v1.6.3
|
github.com/gin-gonic/gin v1.6.3
|
||||||
github.com/go-ini/ini v1.61.0
|
github.com/go-ini/ini v1.61.0
|
||||||
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
github.com/go-playground/validator/v10 v10.3.0 // indirect
|
||||||
github.com/golang/protobuf v1.4.2 // indirect
|
|
||||||
github.com/jinzhu/gorm v1.9.16
|
github.com/jinzhu/gorm v1.9.16
|
||||||
github.com/json-iterator/go v1.1.10 // indirect
|
|
||||||
github.com/lib/pq v1.2.0 // indirect
|
github.com/lib/pq v1.2.0 // indirect
|
||||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
|
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
|
||||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
|
||||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
||||||
|
github.com/swaggo/gin-swagger v1.2.0
|
||||||
github.com/ugorji/go v1.1.8 // indirect
|
github.com/ugorji/go v1.1.8 // indirect
|
||||||
github.com/unknwon/com v1.0.1
|
github.com/unknwon/com v1.0.1
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
||||||
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
|
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.25.0 // indirect
|
google.golang.org/protobuf v1.25.0 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
|
||||||
gopkg.in/ini.v1 v1.61.0 // indirect
|
gopkg.in/ini.v1 v1.61.0 // indirect
|
||||||
gopkg.in/yaml.v2 v2.3.0 // indirect
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type Employee struct {
|
||||||
|
ID int `gorm:"primary_key" json:"id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Department string `json:"department"`
|
||||||
|
Position string `json:"position"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckEmployee(username, password string) bool {
|
||||||
|
var employee Employee
|
||||||
|
db.Select("id").Where(Employee{Username: username, Password: password}).First(&employee)
|
||||||
|
if employee.ID > 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-pripro/shop/pkg/setting"
|
"github.com/go-pripro/shop/pkg/setting"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ type Model struct {
|
||||||
func init() {
|
func init() {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
dbType, dbName, user, password, host, tablePrefix string
|
dbType, dbName, user, password, host, port, tablePrefix string
|
||||||
)
|
)
|
||||||
|
|
||||||
sec, err := setting.Cfg.GetSection("database")
|
sec, err := setting.Cfg.GetSection("database")
|
||||||
|
@ -32,13 +32,15 @@ func init() {
|
||||||
user = sec.Key("USER").String()
|
user = sec.Key("USER").String()
|
||||||
password = sec.Key("PASSWORD").String()
|
password = sec.Key("PASSWORD").String()
|
||||||
host = sec.Key("HOST").String()
|
host = sec.Key("HOST").String()
|
||||||
|
port = sec.Key("PORT").String()
|
||||||
tablePrefix = sec.Key("TABLE_PREFIX").String()
|
tablePrefix = sec.Key("TABLE_PREFIX").String()
|
||||||
|
|
||||||
db, err = gorm.Open(dbType, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
db, err = gorm.Open(dbType, fmt.Sprintf("host=%s user=%s dbname=%s port=%s sslmode=disable password=%s",
|
||||||
user,
|
|
||||||
password,
|
|
||||||
host,
|
host,
|
||||||
dbName))
|
user,
|
||||||
|
dbName,
|
||||||
|
port,
|
||||||
|
password))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -48,7 +50,7 @@ func init() {
|
||||||
return tablePrefix + defaultTableName
|
return tablePrefix + defaultTableName
|
||||||
}
|
}
|
||||||
|
|
||||||
//db.AutoMigrate(&Tag{}, &Article{}, &Auth{})
|
db.AutoMigrate(&Employee{})
|
||||||
//db.SingularTable(true)
|
//db.SingularTable(true)
|
||||||
db.LogMode(true)
|
db.LogMode(true)
|
||||||
db.DB().SetMaxIdleConns(10)
|
db.DB().SetMaxIdleConns(10)
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego/validation"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-pripro/shop/models"
|
||||||
|
"github.com/go-pripro/shop/pkg/e"
|
||||||
|
"github.com/go-pripro/shop/pkg/logging"
|
||||||
|
"github.com/go-pripro/shop/pkg/util"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Employee struct {
|
||||||
|
Username string `valid:"Required; MaxSize(50)"`
|
||||||
|
Password string `valid:"Required; MaxSize(50)"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetEmployee(c *gin.Context) {
|
||||||
|
username := c.Query("username")
|
||||||
|
password := c.Query("password")
|
||||||
|
valid := validation.Validation{}
|
||||||
|
a := Employee{Username: username, Password: password}
|
||||||
|
ok, _ := valid.Valid(&a)
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
code := e.INVALID_PARAMS
|
||||||
|
if ok {
|
||||||
|
isExist := models.CheckEmployee(username, password)
|
||||||
|
if isExist {
|
||||||
|
token, err := util.GenerateToken(username, password)
|
||||||
|
if err != nil {
|
||||||
|
code = e.ERROR_AUTH_TOKEN
|
||||||
|
} else {
|
||||||
|
data["token"] = token
|
||||||
|
code = e.SUCCESS
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
code = e.ERROR_AUTH
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, err := range valid.Errors {
|
||||||
|
logging.Info(err.Key, err.Message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"code": code,
|
||||||
|
"msg": e.GetMsg(code),
|
||||||
|
"data": data,
|
||||||
|
})
|
||||||
|
}
|
|
@ -3,6 +3,9 @@ package routers
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-pripro/shop/pkg/setting"
|
"github.com/go-pripro/shop/pkg/setting"
|
||||||
|
"github.com/go-pripro/shop/routers/admin"
|
||||||
|
ginSwagger "github.com/swaggo/gin-swagger"
|
||||||
|
"github.com/swaggo/gin-swagger/swaggerFiles"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitRouter() *gin.Engine {
|
func InitRouter() *gin.Engine {
|
||||||
|
@ -25,5 +28,9 @@ func InitRouter() *gin.Engine {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
||||||
|
r.GET("/login", admin.GetEmployee)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue