Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加新的功能 #60

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea/
.DS_Store

conf/app.ini
runtime/*
!runtime/qrcode/bg.jpg
!runtime/fonts
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ An example of gin contains many useful features

[简体中文](https://github.com/EDDYCJY/go-gin-example/blob/master/README_ZH.md)

## Installation
## Installation (>= go1.12)
```
$ go get github.com/EDDYCJY/go-gin-example
$ git clone https://github.com/zhangrxiang/go-gin-example.git
```

## How to run
Expand All @@ -22,7 +22,7 @@ Create a **blog database** and import [SQL](https://github.com/EDDYCJY/go-gin-ex

### Conf

You should modify `conf/app.ini`
You should copy `conf/app.example.ini` => `conf/app.ini`

```
[database]
Expand All @@ -44,9 +44,8 @@ IdleTimeout = 200

### Run
```
$ cd $GOPATH/src/go-gin-example

$ go run main.go
$ cd go-gin-example
$ go run main.go
```

Project information and existing API
Expand Down
12 changes: 5 additions & 7 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
18. [Gin实践 番外 Golang交叉编译](https://github.com/EDDYCJY/blog/blob/master/golang/gin/2018-03-26-Gin%E5%AE%9E%E8%B7%B5-%E7%95%AA%E5%A4%96-Golang%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91.md)
19. [Gin实践 番外 请入门 Makefile](https://github.com/EDDYCJY/blog/blob/master/golang/gin/2018-08-26-Gin%E5%AE%9E%E8%B7%B5-%E7%95%AA%E5%A4%96-%E8%AF%B7%E5%85%A5%E9%97%A8%20Makefile.md)

## 安装
## 安装 (>= go1.12)
```
$ go get github.com/EDDYCJY/go-gin-example
$ git clone https://github.com/zhangrxiang/go-gin-example.git
```

## 如何运行
Expand All @@ -43,8 +43,7 @@ $ go get github.com/EDDYCJY/go-gin-example
创建一个 `blog` 数据库,并且导入建表的 [SQL](https://github.com/EDDYCJY/go-gin-example/blob/master/docs/sql/blog.sql)

### 配置

你应该修改 `conf/app.ini` 配置文件
复制`conf/app.example.ini` => `conf/app.ini` 配置文件

```
[database]
Expand All @@ -67,9 +66,8 @@ IdleTimeout = 200

### 运行
```
$ cd $GOPATH/src/go-gin-example

$ go run main.go
$ cd go-gin-example
$ go run main.go
```

项目的运行信息和已存在的 API's
Expand Down
83 changes: 42 additions & 41 deletions conf/app.ini → conf/app.example.ini
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
[app]
PageSize = 10
JwtSecret = 233
PrefixUrl = http://127.0.0.1:8000

RuntimeRootPath = runtime/

ImageSavePath = upload/images/
# MB
ImageMaxSize = 5
ImageAllowExts = .jpg,.jpeg,.png

ExportSavePath = export/
QrCodeSavePath = qrcode/
FontSavePath = fonts/

LogSavePath = logs/
LogSaveName = log
LogFileExt = log
TimeFormat = 20060102

[server]
#debug or release
RunMode = debug
HttpPort = 8000
ReadTimeout = 60
WriteTimeout = 60

[database]
Type = mysql
User = root
Password = rootroot
Host = 127.0.0.1:3306
Name = blog
TablePrefix = blog_

[redis]
Host = 127.0.0.1:6379
Password =
MaxIdle = 30
MaxActive = 30
[app]
PageSize = 10
JwtSecret = 233
JwtExpireTime = 3
PrefixUrl = http://127.0.0.1:8000

RuntimeRootPath = runtime/

ImageSavePath = upload/images/
# MB
ImageMaxSize = 5
ImageAllowExts = .jpg,.jpeg,.png

ExportSavePath = export/
QrCodeSavePath = qrcode/
FontSavePath = fonts/

LogSavePath = logs/
LogSaveName = log
LogFileExt = log
TimeFormat = 20060102

[server]
#debug or release
RunMode = debug
HttpPort = 8000
ReadTimeout = 60
WriteTimeout = 60

[database]
Type = mysql
User = root
Password = rootroot
Host = 127.0.0.1:3306
Name = blog
TablePrefix = blog_

[redis]
Host = 127.0.0.1:6379
Password =
MaxIdle = 30
MaxActive = 30
IdleTimeout = 200
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module go-gin-example

go 1.12

require (
github.com/360EntSecGroup-Skylar/excelize v1.4.1
github.com/Unknwon/com v0.0.0-20190321035513-0fed4efef755
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc
github.com/astaxie/beego v1.11.1
github.com/boombuler/barcode v1.0.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.4.0
github.com/go-ini/ini v1.42.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jinzhu/gorm v1.9.8
github.com/swaggo/gin-swagger v1.1.0
github.com/swaggo/swag v1.5.0
github.com/tealeg/xlsx v1.0.3
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
gopkg.in/ini.v1 v1.42.0 // indirect
)

replace github.com/ugorji/go v1.1.4 => github.com/ugorji/go/codec v0.0.0-20190204201341-e444a5086c43
271 changes: 271 additions & 0 deletions go.sum

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (

"github.com/gin-gonic/gin"

"github.com/EDDYCJY/go-gin-example/models"
"github.com/EDDYCJY/go-gin-example/pkg/gredis"
"github.com/EDDYCJY/go-gin-example/pkg/logging"
"github.com/EDDYCJY/go-gin-example/pkg/setting"
"github.com/EDDYCJY/go-gin-example/routers"
"github.com/EDDYCJY/go-gin-example/pkg/util"
"go-gin-example/models"
"go-gin-example/pkg/gredis"
"go-gin-example/pkg/logging"
"go-gin-example/pkg/setting"
"go-gin-example/pkg/util"
"go-gin-example/routers"
)

func init() {
setting.Setup()
models.Setup()
logging.Setup()
gredis.Setup()
_ = gredis.Setup()
util.Setup()
}

Expand Down Expand Up @@ -48,7 +48,7 @@ func main() {

log.Printf("[info] start http server listening %s", endPoint)

server.ListenAndServe()
_ = server.ListenAndServe()

// If you want Graceful Restart, you need a Unix system and download github.com/fvbock/endless
//endless.DefaultReadTimeOut = readTimeout
Expand Down
16 changes: 10 additions & 6 deletions middleware/jwt/jwt.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package jwt

import (
"go-gin-example/pkg/setting"
"net/http"

"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"

"github.com/EDDYCJY/go-gin-example/pkg/e"
"github.com/EDDYCJY/go-gin-example/pkg/util"
"go-gin-example/pkg/e"
"go-gin-example/pkg/util"
)

// JWT is jwt middleware
Expand All @@ -19,15 +20,18 @@ func JWT() gin.HandlerFunc {
code = e.SUCCESS
token := c.Query("token")
if token == "" {
code = e.INVALID_PARAMS
code = e.InvalidParams
} else {
_, err := util.ParseToken(token)
data, err := util.ParseToken(token)
if data != nil {
setting.Username = util.GetUsername(data)
}
if err != nil {
switch err.(*jwt.ValidationError).Errors {
case jwt.ValidationErrorExpired:
code = e.ERROR_AUTH_CHECK_TOKEN_TIMEOUT
code = e.ErrorAuthCheckTokenTimeout
default:
code = e.ERROR_AUTH_CHECK_TOKEN_FAIL
code = e.ErrorAuthCheckTokenFail
}
}
}
Expand Down
40 changes: 39 additions & 1 deletion models/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "github.com/jinzhu/gorm"
import (
"github.com/jinzhu/gorm"
)

type Auth struct {
ID int `gorm:"primary_key" json:"id"`
Expand All @@ -22,3 +24,39 @@ func CheckAuth(username, password string) (bool, error) {

return false, nil
}

//注册用户
func AddAuth(username, password string) (bool, error) {
err := db.Create(&Auth{Username: username, Password: password}).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
return true, nil
}

//修改密码
func ResetPassword(username, password, newPassword string) (bool, error) {
err := db.Model(Auth{}).Where(Auth{Username: username, Password: password}).Update("password", newPassword).Error
if err != nil && err != gorm.ErrRecordNotFound {
return false, err
}
return true, nil
}

func Detail(username string) (Auth, error) {
var auth Auth
err := db.First(&auth).Where("username", username).Error
if err != nil && err != gorm.ErrRecordNotFound {
return auth, err
}
return auth, nil
}

func All() ([]Auth, error) {
var auth []Auth
err := db.Find(&auth).Error
if err != nil && err != gorm.ErrRecordNotFound {
return auth, err
}
return auth, nil
}
2 changes: 1 addition & 1 deletion models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"

"github.com/EDDYCJY/go-gin-example/pkg/setting"
"go-gin-example/pkg/setting"
"time"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/app/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"github.com/gin-gonic/gin"
"net/http"

"github.com/EDDYCJY/go-gin-example/pkg/e"
"go-gin-example/pkg/e"
)

// BindAndValid binds and validates data
func BindAndValid(c *gin.Context, form interface{}) (int, int) {
err := c.Bind(form)
if err != nil {
return http.StatusBadRequest, e.INVALID_PARAMS
return http.StatusBadRequest, e.InvalidParams
}

valid := validation.Validation{}
Expand All @@ -22,7 +22,7 @@ func BindAndValid(c *gin.Context, form interface{}) (int, int) {
}
if !check {
MarkErrors(valid.Errors)
return http.StatusBadRequest, e.INVALID_PARAMS
return http.StatusBadRequest, e.InvalidParams
}

return http.StatusOK, e.SUCCESS
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app
import (
"github.com/astaxie/beego/validation"

"github.com/EDDYCJY/go-gin-example/pkg/logging"
"go-gin-example/pkg/logging"
)

// MarkErrors logs error logs
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app
import (
"github.com/gin-gonic/gin"

"github.com/EDDYCJY/go-gin-example/pkg/e"
"go-gin-example/pkg/e"
)

type Gin struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/e/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package e

const (
CACHE_ARTICLE = "ARTICLE"
CACHE_TAG = "TAG"
CacheArticle = "ARTICLE"
CacheTag = "TAG"
)
Loading