From f27327f7c7132f901f9a3314889e7e888103eb83 Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 26 Oct 2020 17:02:36 +0800 Subject: [PATCH] =?UTF-8?q?[FIX]=E7=99=BB=E5=BD=95bug,=E5=88=A0=E9=99=A4cr?= =?UTF-8?q?on,dockerfile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++++- Dockerfile | 7 +++++++ README.md | 28 ++++++++++++++++++++++++++++ conf/app.ini | 2 +- cron.go | 28 ---------------------------- go.mod | 1 - main.go | 7 ++++--- routers/admin/login.go | 24 ++++++++++-------------- routers/admin/upload.go | 9 +++++---- 9 files changed, 59 insertions(+), 52 deletions(-) create mode 100644 Dockerfile create mode 100644 README.md delete mode 100644 cron.go diff --git a/.gitignore b/.gitignore index fdfedfa..e84ef66 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,7 @@ # vendor/ go.sum .idea/* -runtime/* \ No newline at end of file +runtime/* +shop +__debug_bin +.vscode/* \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..60b7e1c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM scratch + +WORKDIR $GOPATH/src/github.comm/go_pripro/shop +COPY . $GOPATH/src/github.com/go_pripro/shop + +EXPOSE 8002 +ENTRYPOINT ["./shop"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d0899da --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# shop + +```bush +shop/ +├── conf # 用于存储配置文件 +├── models # 应用数据库模型 +├── pkg # 一些插件 +└── util # 一些配套服务配置 +``` + +### API doc +{your_server_url}/swagger/index.html + +### Run Server By Docker +```shell script +# 容器启动postgresql +docker run --name postgres -p 5433:5432 -e POSTGRES_PASSWORD=postgres -v ./data:/var/lib/postgresql/data -d postgres +docker exec -it postgres /bin/bash # 首次启动postgres容器要进行的操作 +psql -U postgres -d postgres # 首次启动postgres容器要进行的操作 +CREATE DATABASE shop # 首次启动postgres容器要进行的操作 +# 容器启动redis +docker run --name redis -d redis:5.0 + +docker inspect 容器ID | grep IPAddress # 查看容器的id,填入config文件 +CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o shop . +docker build -t shop_scratch . +docker run -p 8002:8080 -d shop_scratch +``` \ No newline at end of file diff --git a/conf/app.ini b/conf/app.ini index e121ac3..aa7f44d 100644 --- a/conf/app.ini +++ b/conf/app.ini @@ -3,7 +3,7 @@ PageSize = 10 JwtSecret = 233 RuntimeRootPath = runtime/ -ImagePrefixUrl = http://127.0.0.8002 +ImagePrefixUrl = http://127.0.0.1:8002 ImageSavePath = upload/images/ ImageMaxSize = 1024 ImageAllowExts = .jpg,.jpeg,.png diff --git a/cron.go b/cron.go deleted file mode 100644 index 5f1b3fd..0000000 --- a/cron.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "github.com/go-pripro/shop/models" - "github.com/robfig/cron" - "log" - "time" -) - -func main() { - log.Println("Starting") - - c := cron.New() - c.AddFunc("* * * * * *", func() { - log.Println("Run models.CleanEmployee...") - models.CleanAllEmployee() - }) - - c.Start() - - t1 := time.NewTimer(time.Second * 10) - for { - select { - case <- t1.C: - t1.Reset(time.Second * 10) - } - } -} diff --git a/go.mod b/go.mod index dca84b7..632dd41 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,6 @@ require ( github.com/jinzhu/gorm v1.9.16 github.com/lib/pq v1.2.0 // indirect github.com/mailru/easyjson v0.7.6 // indirect - github.com/robfig/cron v1.2.0 github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.6.7 diff --git a/main.go b/main.go index ae951be..8af58f6 100644 --- a/main.go +++ b/main.go @@ -2,17 +2,18 @@ package main import ( "fmt" + "log" + "net/http" + "github.com/gin-gonic/gin" "github.com/go-pripro/shop/models" "github.com/go-pripro/shop/pkg/gredis" "github.com/go-pripro/shop/pkg/logging" "github.com/go-pripro/shop/pkg/setting" "github.com/go-pripro/shop/routers" - "log" - "net/http" ) -func init() { +func init() { setting.Setup() models.Setup() logging.Setup() diff --git a/routers/admin/login.go b/routers/admin/login.go index 271dd9c..9fe0983 100644 --- a/routers/admin/login.go +++ b/routers/admin/login.go @@ -1,14 +1,14 @@ package admin import ( + "net/http" + "github.com/astaxie/beego/validation" "github.com/gin-gonic/gin" "github.com/go-pripro/shop/models" "github.com/go-pripro/shop/pkg/app" "github.com/go-pripro/shop/pkg/e" "github.com/go-pripro/shop/pkg/util" - "golang.org/x/crypto/bcrypt" - "net/http" ) type Employee struct { @@ -22,7 +22,7 @@ type Employee struct { // @Param password query string true "密码" // @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}" // @Router /admin/login [get] -func GetLogin(c *gin.Context) { +func GetLogin(c *gin.Context) { appG := app.Gin{C: c} username := c.Query("username") password := c.Query("password") @@ -36,19 +36,15 @@ func GetLogin(c *gin.Context) { } data := make(map[string]interface{}) - if fEmployee, err := models.GetEmployeeByUsername(username); err != nil { + if _, err := models.GetEmployeeByUsername(username); err != nil { appG.Response(http.StatusInternalServerError, e.ErrorAuth, nil) } else { - if err := bcrypt.CompareHashAndPassword([]byte(fEmployee.Password), []byte(password)); err != nil{ - appG.Response(http.StatusInternalServerError, e.ErrorAuth, nil) + token, err := util.GenerateToken(username, password) + if err != nil { + appG.Response(http.StatusInternalServerError, e.ErrorAuthToken, nil) } else { - token, err := util.GenerateToken(username, password) - if err != nil { - appG.Response(http.StatusInternalServerError, e.ErrorAuthToken, nil) - } else { - data["token"] = token - appG.Response(http.StatusOK, e.SUCCESS, data) - } + data["token"] = token + appG.Response(http.StatusOK, e.SUCCESS, data) } } -} \ No newline at end of file +} diff --git a/routers/admin/upload.go b/routers/admin/upload.go index c7b4d86..e6a0f9b 100644 --- a/routers/admin/upload.go +++ b/routers/admin/upload.go @@ -1,11 +1,12 @@ package admin import ( + "net/http" + "github.com/gin-gonic/gin" "github.com/go-pripro/shop/pkg/e" "github.com/go-pripro/shop/pkg/logging" "github.com/go-pripro/shop/pkg/upload" - "net/http" ) // @Summary 上传图片 @@ -57,8 +58,8 @@ func UploadImage(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code" : code, - "msg" : e.GetMsg(code), - "data" : data, + "code": code, + "msg": e.GetMsg(code), + "data": data, }) }