提交 4ca3c2e2 编写于 作者: G Granty1

add base operation record code

上级 d70599f6
......@@ -11,8 +11,8 @@ jwt:
# mysql connect configuration
mysql:
username: root
password: 'Aa@6447985'
path: '127.0.0.1:3306'
password: 'xxxxx'
path: 'xxxxxx:3306'
db-name: 'qmPlus'
config: 'charset=utf8&parseTime=True&loc=Local'
max-idle-conns: 10
......@@ -31,6 +31,7 @@ qiniu:
secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY'
bucket: 'qm-plus-img'
img-path: 'http://qmplusimg.henrongyi.top'
# redis configuration
redis:
addr: '127.0.0.1:6379'
......@@ -55,4 +56,10 @@ log:
prefix: '[GIN-VUE-ADMIN]'
log-file: true
stdout: 'DEBUG'
file: 'DEBUG'
\ No newline at end of file
file: 'DEBUG'
# operation configuration
operation:
skip_paths:
- '/base/login'
- '/base/register'
\ No newline at end of file
package config
type Server struct {
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
Sqlite Sqlite `mapstructure:"sqlite" json:"sqlite" yaml:"sqlite"`
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"`
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
System System `mapstructure:"system" json:"system" yaml:"system"`
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
Log Log `mapstructure:"log" json:"log" yaml:"log"`
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
Sqlite Sqlite `mapstructure:"sqlite" json:"sqlite" yaml:"sqlite"`
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
Casbin Casbin `mapstructure:"casbin" json:"casbin" yaml:"casbin"`
Redis Redis `mapstructure:"redis" json:"redis" yaml:"redis"`
System System `mapstructure:"system" json:"system" yaml:"system"`
JWT JWT `mapstructure:"jwt" json:"jwt" yaml:"jwt"`
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
Log Log `mapstructure:"log" json:"log" yaml:"log"`
Operation Operation `mapstructure:"operation" json:"operation" yaml:"operation"`
}
type System struct {
......@@ -70,3 +71,7 @@ type Sqlite struct {
Config string `mapstructure:"config" json:"config" yaml:"config"`
LogMode bool `mapstructure:"log-mode" json:"logMode" yaml:"log-mode"`
}
type Operation struct {
SkipPaths []string `mapstructure: "skip_paths" json:"skip_paths" yaml: "username"`
}
......@@ -30,26 +30,24 @@ func init() {
}
logger := oplogging.MustGetLogger(module)
var backends []oplogging.Backend
backends = registerStdout(c, backends)
backends = registerFile(c, backends)
registerStdout(c, &backends)
registerFile(c, &backends)
oplogging.SetBackend(backends...)
global.GVA_LOG = logger
}
func registerStdout(c config.Log, backends []oplogging.Backend) []oplogging.Backend {
func registerStdout(c config.Log, backends *[]oplogging.Backend) {
if c.Stdout != "" {
level, err := oplogging.LogLevel(c.Stdout)
if err != nil {
fmt.Println(err)
}
backends = append(backends, createBackend(os.Stdout, c, level))
*backends = append(*backends, createBackend(os.Stdout, c, level))
}
return backends
}
func registerFile(c config.Log, backends []oplogging.Backend) []oplogging.Backend {
func registerFile(c config.Log, backends *[]oplogging.Backend) {
if c.File != "" {
if ok, _ := utils.PathExists(logDir); !ok {
// directory not exist
......@@ -67,16 +65,13 @@ func registerFile(c config.Log, backends []oplogging.Backend) []oplogging.Backen
)
if err != nil {
fmt.Println(err)
return backends
}
level, err := oplogging.LogLevel(c.File)
if err != nil {
fmt.Println(err)
}
backends = append(backends, createBackend(fileWriter, c, level))
*backends = append(*backends, createBackend(fileWriter, c, level))
}
return backends
}
func createBackend(w io.Writer, c config.Log, level oplogging.Level) oplogging.Backend {
......
......@@ -14,6 +14,8 @@ import (
func Routers() *gin.Engine {
var Router = gin.Default()
// 操作记录
Router.Use(middleware.RecordRequestBody(), middleware.OperationRecord())
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
global.GVA_LOG.Debug("use middleware logger")
// 跨域
......
package middleware
import (
"bytes"
"fmt"
"gin-vue-admin/global"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"time"
)
var body []byte
func RecordRequestBody() gin.HandlerFunc {
return func(c *gin.Context) {
if c.Request.Method != http.MethodGet {
var err error
body, err = ioutil.ReadAll(c.Request.Body)
if err != nil {
global.GVA_LOG.Error(err)
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
} else {
body = nil
}
}
}
func OperationRecord() gin.HandlerFunc {
return gin.LoggerWithConfig(gin.LoggerConfig{
Formatter: func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" \"%s\" %s\"\n",
param.ClientIP,
param.TimeStamp.Format(time.RFC1123),
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
string(body),
param.ErrorMessage,
)
},
// 暂时没考虑好
Output: nil,
SkipPaths: global.GVA_CONFIG.Operation.SkipPaths,
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册