Skip to content

Commit

Permalink
配置文件优化
Browse files Browse the repository at this point in the history
feizhiwu committed Jul 11, 2022
1 parent 024e46a commit ef1de1c
Showing 9 changed files with 174 additions and 50 deletions.
22 changes: 1 addition & 21 deletions app/common/display.go
Original file line number Diff line number Diff line change
@@ -70,10 +70,6 @@ type Display struct {
Params map[string]interface{}
status int
funcs map[string]func()
checks struct {
verify bool
actions []string
}
}

func (d *Display) Get(f func()) {
@@ -143,23 +139,7 @@ func (d *Display) Run() {
action := d.GetHeader("action")
f := d.funcs[d.Request.Method+"-"+action]
if f != nil {
if len(d.checks.actions) > 0 {
if d.checks.verify && InArray(len(d.checks.actions), func(i int) bool {
return d.checks.actions[i] == action
}) {
d.ForceLogin()
f()
} else if !d.checks.verify && !InArray(len(d.checks.actions), func(i int) bool {
return d.checks.actions[i] == action
}) {
d.ForceLogin()
f()
} else {
f()
}
} else {
f()
}
f()
} else {
d.Show(StatusWarn)
}
13 changes: 3 additions & 10 deletions app/middleware/LoggerMiddlevare.go
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ import (
"github.com/jinzhu/gorm"
"log"
"os"
"path"
"stage/app/common"
"stage/config/conf"
"strings"
@@ -107,19 +106,13 @@ func Logger(c *gin.Context) {
}

func writer() (writer []*os.File) {
dir, _ := os.Getwd()
logPath := path.Join(dir, "/data/runtime/log/"+time.Now().Format("200601"))
file := conf.GetFile()
logPath := file.LogDir
//创建文件路径
if _, err := os.Stat(logPath); os.IsNotExist(err) {
os.MkdirAll(logPath, os.ModePerm)
}
var logFix string
if gin.Mode() == gin.TestMode {
logFix = "-test.log"
} else {
logFix = ".log"
}
filePath := logPath + "/" + time.Now().Format("02") + logFix
filePath := file.Logger
if _, err := os.Stat(filePath); os.IsNotExist(err) {
os.Create(filePath)
}
Original file line number Diff line number Diff line change
@@ -5,25 +5,53 @@ import (
"github.com/gin-gonic/gin"
)

func Init(c *gin.Context) {
func Request(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Header("Access-Control-Allow-Headers", "X-Requested-With,X_Requested_With,content-type,token,action")
params := make(map[string]interface{})
c.Request.ParseForm()
raw, _ := c.GetRawData()
if len(raw) == 0 {
c.Request.ParseForm()
for k, v := range c.Request.Form {
params[k] = v[0]
}
} else {
json.Unmarshal(raw, &params)
}
json.Unmarshal(raw, &params)
if c.GetHeader("token") != "" {
//TODO 临时
token := c.GetHeader("token")
if token != "" || true {
//coding 根据token获取用户信息
params["login_uid"] = 1
}
c.Set("params", params)
if !exclude(c) {
if params["login_uid"] == nil {
panic(80003)
}
}
}

//路由免登排除
func exclude(c *gin.Context) bool {
rs := map[string][]string{
"/": nil,
}
var action string
for k, v := range rs {
if k == c.Request.URL.Path {
if v == nil {
return true
}
action = c.GetHeader("action")
for _, a := range v {
if a == action {
return true
}
}
}
}
return false
}
2 changes: 1 addition & 1 deletion app/model/BaseModel.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@ package model

type Pages struct {
Count uint `json:"count"`
Limit uint `json:"limit"`
Size uint `json:"size"`
Page uint `json:"page"`
}
50 changes: 40 additions & 10 deletions config/conf/conf.go
Original file line number Diff line number Diff line change
@@ -11,17 +11,47 @@ import (
"os"
"path"
"strings"
"time"
)

var (
DBC map[string]*gorm.DB
DBS []string
DBC map[string]*gorm.DB
DBS []string
File *file
)

type file struct {
date string
pack string
Config string
Message string
LogDir string
Logger string
}

func GetFile() *file {
if File == nil || File.date != time.Now().Format("02") {
File = new(file)
File.date = time.Now().Format("02")
if gin.Mode() == gin.DebugMode {
dir, _ := os.Getwd()
File.Config = path.Join(dir, "config/config.yml")
File.Message = path.Join(dir, "config/message.yml")
File.LogDir = path.Join(dir, "data/runtime/log/"+time.Now().Format("200601"))
} else {
File.pack = "/stage"
File.Config = path.Join(os.Getenv("GOPATH"), "src", File.pack, "config/config.yml")
File.Message = path.Join(os.Getenv("GOPATH"), "src", File.pack, "config/message.yml")
File.LogDir = path.Join(os.Getenv("GOBIN"), "data", File.pack, "runtime/log/"+time.Now().Format("200601"))
}
File.Logger = path.Join(File.LogDir, File.date+"-"+gin.Mode()+".log")
}
return File
}

// Config 获取配置信息
func Config(key string) interface{} {
dir, _ := os.Getwd()
filePath := path.Join(dir, "/config/config.yml")
filePath := GetFile().Config
fileData, _ := ioutil.ReadFile(filePath)
var config map[interface{}]interface{}
yaml.Unmarshal(fileData, &config)
@@ -47,8 +77,7 @@ func Config(key string) interface{} {
func Message(status int) string {
var msg map[int]string
var filePath string
dir, _ := os.Getwd()
filePath = path.Join(dir, "/config/message.yml")
filePath = GetFile().Message
fileData, _ := ioutil.ReadFile(filePath)
yaml.Unmarshal(fileData, &msg)
return msg[status]
@@ -58,12 +87,13 @@ func Message(status int) string {
func ConnectDB() {
DBC = make(map[string]*gorm.DB)
dbConf := Config("db")
if len(dbConf.(map[interface{}]interface{})) > 1 {
for k, v := range dbConf.(map[interface{}]interface{}) {
for k, v := range dbConf.(map[interface{}]interface{}) {
if _, ok := v.(map[interface{}]interface{}); !ok {
connectDB("db", dbConf.(map[interface{}]interface{}))
break
} else {
connectDB(albedo.MakeString(k), v.(map[interface{}]interface{}))
}
} else {
connectDB("db", dbConf.(map[interface{}]interface{}))
}
}

2 changes: 1 addition & 1 deletion config/route/route.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ type route struct {
// Run 路由路口
func Run(engine *gin.Engine) {
r := route{engine}
r.Engine.Use(middleware.Init, middleware.Recovery, middleware.Logger)
r.Engine.Use(middleware.Recovery, middleware.Logger, middleware.Request)
r.Engine.Any("", controller.Index)
r.v1()
}
93 changes: 93 additions & 0 deletions data/runtime/log/202207/11-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
2022/07/11 18:45:39 %!s(int=80003)
Traceback:
/usr/local/opt/go/libexec/src/runtime/panic.go:1038
/Users/wangdongwu/www/go/src/study/stage/app/middleware/RequestMiddleware.go:17
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:513
/usr/local/opt/go/libexec/src/net/http/server.go:2880
/usr/local/opt/go/libexec/src/net/http/server.go:1931
/usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1582
----------------------------------------------------------------------
2022/07/11 18:45:44 ::1[OK] GET /v1/user action: map[login_uid:1] in 4.643957ms
----------------------------------------------------------------------
2022/07/11 18:46:42 ::1[OK] GET /v1/user action: map[login_uid:1] in 8.665558ms
----------------------------------------------------------------------
2022/07/11 18:47:22 ::1[OK] GET /v1/user action: map[login_uid:1] in 10.702835ms
----------------------------------------------------------------------
2022/07/11 18:47:54 ::1[OK] GET /v1/user action: map[login_uid:1] in 6.849133ms
----------------------------------------------------------------------
2022/07/11 18:48:20 %!s(int=80007)
Traceback:
/usr/local/opt/go/libexec/src/runtime/panic.go:1038
/Users/wangdongwu/www/go/src/study/stage/app/common/display.go:109
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:47
/Users/wangdongwu/www/go/src/study/stage/app/common/display.go:144
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:27
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:513
/usr/local/opt/go/libexec/src/net/http/server.go:2880
/usr/local/opt/go/libexec/src/net/http/server.go:1931
/usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1582
----------------------------------------------------------------------
2022/07/11 18:48:30 reflect: call of reflect.Value.Set on zero Value
Traceback:
/usr/local/opt/go/libexec/src/runtime/panic.go:1038
/usr/local/opt/go/libexec/src/reflect/value.go:255
/usr/local/opt/go/libexec/src/reflect/value.go:1918
/usr/local/opt/go/libexec/src/reflect/value.go:1912
/Users/wangdongwu/www/go/pkg/mod/github.com/feizhiwu/[email protected]/kokomi/kokomi.go:190
/Users/wangdongwu/www/go/src/study/stage/app/dao/UserDao.go:50
/Users/wangdongwu/www/go/src/study/stage/app/service/UserService.go:51
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:48
/Users/wangdongwu/www/go/src/study/stage/app/common/display.go:144
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:27
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:513
/usr/local/opt/go/libexec/src/net/http/server.go:2880
/usr/local/opt/go/libexec/src/net/http/server.go:1931
/usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1582
[MAIN_DB][OK] SELECT * FROM `user` ORDER BY id desc LIMIT 20 OFFSET 0 [] in 124.546072ms
----------------------------------------------------------------------
2022/07/11 18:49:19 reflect: call of reflect.Value.Set on zero Value
Traceback:
/usr/local/opt/go/libexec/src/runtime/panic.go:1038
/usr/local/opt/go/libexec/src/reflect/value.go:255
/usr/local/opt/go/libexec/src/reflect/value.go:1918
/usr/local/opt/go/libexec/src/reflect/value.go:1912
/Users/wangdongwu/www/go/pkg/mod/github.com/feizhiwu/[email protected]/kokomi/kokomi.go:190
/Users/wangdongwu/www/go/src/study/stage/app/dao/UserDao.go:50
/Users/wangdongwu/www/go/src/study/stage/app/service/UserService.go:51
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:48
/Users/wangdongwu/www/go/src/study/stage/app/common/display.go:142
/Users/wangdongwu/www/go/src/study/stage/app/controller/UserController.go:27
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:169
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:166
/Users/wangdongwu/www/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:513
/usr/local/opt/go/libexec/src/net/http/server.go:2880
/usr/local/opt/go/libexec/src/net/http/server.go:1931
/usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1582
[MAIN_DB][OK] SELECT * FROM `user` ORDER BY id desc LIMIT 20 OFFSET 0 [] in 6.695938ms
----------------------------------------------------------------------
2022/07/11 18:50:13 ::1[OK] GET /v1/user?page=1 action:list map[login_uid:1 page:1] in 34.701663ms
[MAIN_DB][OK] SELECT * FROM `user` ORDER BY id desc LIMIT 20 OFFSET 0 [] in 1.511374ms
[MAIN_DB][OK] SELECT count(*) FROM `user` [] in 25.045374ms
----------------------------------------------------------------------
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ module stage
go 1.13

require (
github.com/feizhiwu/gs v1.2.2
github.com/feizhiwu/gs v1.7.9
github.com/gin-gonic/gin v1.7.7
github.com/jinzhu/gorm v1.9.16
github.com/kr/pretty v0.1.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6RO
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/feizhiwu/gs v1.2.2 h1:Qhrfe/hRPHILDcMg4n1D/4rZjBIQ8p5VSl/EaSDcHP0=
github.com/feizhiwu/gs v1.2.2/go.mod h1:t88h0Uiibg2W+hJIqxzpmYgB3d+3edl0nqpW7twrnX4=
github.com/feizhiwu/gs v1.7.9 h1:TMcTEJ2bsLwcsLqDP0CYuRMTIez+7vGNH3T1eyRAizM=
github.com/feizhiwu/gs v1.7.9/go.mod h1:t88h0Uiibg2W+hJIqxzpmYgB3d+3edl0nqpW7twrnX4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=

0 comments on commit ef1de1c

Please sign in to comment.