Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FLIPPED-AURORA
gin-vue-admin
提交
1f5edd3a
G
gin-vue-admin
项目概览
FLIPPED-AURORA
/
gin-vue-admin
大约 1 年 前同步成功
通知
345
Star
18155
Fork
5506
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gin-vue-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1f5edd3a
编写于
4月 05, 2020
作者:
G
Granty1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine some structs and handle cycle import
上级
2abf4013
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
146 addition
and
190 deletion
+146
-190
server/config.json
server/config.json
+2
-2
server/config/config.go
server/config/config.go
+64
-0
server/core/config.go
server/core/config.go
+30
-0
server/core/log.go
server/core/log.go
+18
-54
server/core/server.go
server/core/server.go
+4
-4
server/global/global.go
server/global/global.go
+4
-3
server/init/db_table.go
server/init/db_table.go
+4
-3
server/init/log.go
server/init/log.go
+0
-17
server/init/mysql.go
server/init/mysql.go
+3
-2
server/init/redis.go
server/init/redis.go
+7
-6
server/init/router.go
server/init/router.go
+5
-6
server/main.go
server/main.go
+2
-26
server/middleware/logger.go
server/middleware/logger.go
+0
-64
server/model/sys_system.go
server/model/sys_system.go
+3
-3
未找到文件。
server/config.json
浏览文件 @
1f5edd3a
...
...
@@ -37,7 +37,7 @@
"log"
:
{
"prefix"
:
"[GIN-VUE-ADMIN]"
,
"logFile"
:
false
,
"stdout"
:
[
"DEBUG"
]
,
"file"
:
[
"WARNING"
]
"stdout"
:
"DEBUG"
,
"file"
:
"WARNING"
}
}
\ No newline at end of file
server/
init
/config.go
→
server/
config
/config.go
浏览文件 @
1f5edd3a
package
init
package
config
import
(
"fmt"
"gin-vue-admin/global"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type
Config
struct
{
MysqlAdmin
MysqlAdmin
`json:"mysqlAdmin"`
type
Server
struct
{
Mysql
Mysql
`json:"mysql"`
Qiniu
Qiniu
`json:"qiniu"`
Casbin
Config
CasbinConfig
`json:"casbinConfig
"`
Redis
Admin
RedisAdmin
`json:"redisAdmin
"`
Casbin
Casbin
`json:"casbin
"`
Redis
Redis
`json:"redis
"`
System
System
`json:"system"`
JWT
JWT
`json:"jwt"`
Captcha
Captcha
`json:"captcha"`
...
...
@@ -28,11 +21,11 @@ type JWT struct { // jwt签名
SigningKey
string
`json:"signingKey"`
}
type
Casbin
Config
struct
{
//casbin配置
type
Casbin
struct
{
//casbin配置
ModelPath
string
`json:"modelPath"`
// casbin model地址配置
}
type
Mysql
Admin
struct
{
// mysql admin 数据库配置
type
Mysql
struct
{
// mysql admin 数据库配置
Username
string
`json:"username"`
Password
string
`json:"password"`
Path
string
`json:"path"`
...
...
@@ -43,7 +36,7 @@ type MysqlAdmin struct { // mysql admin 数据库配置
LogMode
bool
`json:"maxOpenConns"`
}
type
Redis
Admin
struct
{
// Redis admin 数据库配置
type
Redis
struct
{
// Redis admin 数据库配置
Addr
string
`json:"addr"`
Password
string
`json:"password"`
DB
int
`json:"db"`
...
...
@@ -59,45 +52,13 @@ type Captcha struct { // 验证码配置
ImgHeight
int
`json:"imgHeight"`
}
/**
Log Config
"CRITICAL"
"ERROR"
"WARNING"
"NOTICE"
"INFO"
"DEBUG"
*/
type
Log
struct
{
// log 打印的前缀
Prefix
string
`json:"prefix"`
// 是否显示打印log的文件具体路径
LogFile
bool
`json:"logFile"`
// 在控制台打印log的级别, []默认不打印
Stdout
[]
string
`json:"stdout"`
// 在文件中打印log的级别 []默认不打印
File
[]
string
`json:"file"`
}
func
init
()
{
v
:=
viper
.
New
()
v
.
SetConfigName
(
"config"
)
// 设置配置文件名 (不带后缀)
v
.
AddConfigPath
(
"/"
)
// 第一个搜索路径
v
.
SetConfigType
(
"json"
)
err
:=
v
.
ReadInConfig
()
// 搜索路径,并读取配置数据
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"Fatal error config file: %s
\n
"
,
err
))
}
v
.
WatchConfig
()
v
.
OnConfigChange
(
func
(
e
fsnotify
.
Event
)
{
fmt
.
Println
(
"config file changed:"
,
e
.
Name
)
if
err
:=
v
.
Unmarshal
(
&
global
.
GVA_CONFIG
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
})
if
err
:=
v
.
Unmarshal
(
&
global
.
GVA_CONFIG
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
global
.
GVA_VP
=
v
// 在控制台打印log的级别, "" 默认不打印
Stdout
string
`json:"stdout"`
// 在文件中打印log的级别 "" 默认不打印
File
string
`json:"file"`
}
server/core/config.go
0 → 100644
浏览文件 @
1f5edd3a
package
core
import
(
"fmt"
"gin-vue-admin/global"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func
init
()
{
v
:=
viper
.
New
()
v
.
SetConfigName
(
"config"
)
// 设置配置文件名 (不带后缀)
v
.
AddConfigPath
(
"/"
)
// 第一个搜索路径
v
.
SetConfigType
(
"json"
)
err
:=
v
.
ReadInConfig
()
// 搜索路径,并读取配置数据
if
err
!=
nil
{
panic
(
fmt
.
Errorf
(
"Fatal error config file: %s
\n
"
,
err
))
}
v
.
WatchConfig
()
v
.
OnConfigChange
(
func
(
e
fsnotify
.
Event
)
{
fmt
.
Println
(
"config file changed:"
,
e
.
Name
)
if
err
:=
v
.
Unmarshal
(
&
global
.
GVA_CONFIG
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
})
if
err
:=
v
.
Unmarshal
(
&
global
.
GVA_CONFIG
);
err
!=
nil
{
fmt
.
Println
(
err
)
}
global
.
GVA_VP
=
v
}
server/
init/qmlog
/log.go
→
server/
core
/log.go
浏览文件 @
1f5edd3a
package
qmlog
package
core
// Register logger
import
(
"errors"
"fmt"
"gin-vue-admin/init"
"gin-vue-admin/config"
"gin-vue-admin/global"
"gin-vue-admin/utils"
rotatelogs
"github.com/lestrrat/go-file-rotatelogs"
oplogging
"github.com/op/go-logging"
...
...
@@ -21,19 +20,13 @@ const (
)
var
(
configNotFound
=
errors
.
New
(
"logger prefix not found"
)
defaultFormatter
=
`%{time:2006/01/02 - 15:04:05.000} %{longfile} %{color:bold}▶ [%{level:.6s}] %{message}%{color:reset}`
)
type
Logger
struct
{
logger
*
oplogging
.
Logger
}
func
NewLogger
()
error
{
c
:=
init
.
GinVueAdminconfig
.
Log
func
init
()
{
c
:=
global
.
GVA_CONFIG
.
Log
if
c
.
Prefix
==
""
{
return
configNotFound
_
=
fmt
.
Errorf
(
"logger prefix not found"
)
}
logger
:=
oplogging
.
MustGetLogger
(
module
)
var
backends
[]
oplogging
.
Backend
...
...
@@ -41,16 +34,14 @@ func NewLogger() error {
backends
=
registerFile
(
c
,
backends
)
oplogging
.
SetBackend
(
backends
...
)
init
.
SetLogger
(
logger
)
return
nil
global
.
GVA_LOG
=
logger
}
func
registerStdout
(
c
init
.
Log
,
backends
[]
oplogging
.
Backend
)
[]
oplogging
.
Backend
{
for
_
,
v
:=
range
c
.
Stdout
{
level
,
err
:=
oplogging
.
LogLevel
(
v
)
func
registerStdout
(
c
config
.
Log
,
backends
[]
oplogging
.
Backend
)
[]
oplogging
.
Backend
{
if
c
.
Stdout
!=
""
{
level
,
err
:=
oplogging
.
LogLevel
(
c
.
Stdout
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
continue
}
backends
=
append
(
backends
,
createBackend
(
os
.
Stdout
,
c
,
level
))
}
...
...
@@ -58,8 +49,8 @@ func registerStdout(c init.Log, backends []oplogging.Backend) []oplogging.Backen
return
backends
}
func
registerFile
(
c
init
.
Log
,
backends
[]
oplogging
.
Backend
)
[]
oplogging
.
Backend
{
if
len
(
c
.
File
)
>
0
{
func
registerFile
(
c
config
.
Log
,
backends
[]
oplogging
.
Backend
)
[]
oplogging
.
Backend
{
if
c
.
File
!=
""
{
if
ok
,
_
:=
utils
.
PathExists
(
logDir
);
!
ok
{
// directory not exist
fmt
.
Println
(
"create log directory"
)
...
...
@@ -79,20 +70,17 @@ func registerFile(c init.Log, backends []oplogging.Backend) []oplogging.Backend
fmt
.
Println
(
err
)
return
backends
}
for
_
,
v
:=
range
c
.
File
{
level
,
err
:=
oplogging
.
LogLevel
(
v
)
level
,
err
:=
oplogging
.
LogLevel
(
c
.
File
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
continue
}
backends
=
append
(
backends
,
createBackend
(
fileWriter
,
c
,
level
))
}
}
return
backends
}
func
createBackend
(
w
io
.
Writer
,
c
init
.
Log
,
level
oplogging
.
Level
)
oplogging
.
Backend
{
func
createBackend
(
w
io
.
Writer
,
c
config
.
Log
,
level
oplogging
.
Level
)
oplogging
.
Backend
{
backend
:=
oplogging
.
NewLogBackend
(
w
,
c
.
Prefix
,
0
)
stdoutWriter
:=
false
if
w
==
os
.
Stdout
{
...
...
@@ -104,7 +92,7 @@ func createBackend(w io.Writer, c init.Log, level oplogging.Level) oplogging.Bac
return
backendLeveled
}
func
getLogFormatter
(
c
init
.
Log
,
stdoutWriter
bool
)
oplogging
.
Formatter
{
func
getLogFormatter
(
c
config
.
Log
,
stdoutWriter
bool
)
oplogging
.
Formatter
{
pattern
:=
defaultFormatter
if
!
stdoutWriter
{
// Color is only required for console output
...
...
@@ -118,27 +106,3 @@ func getLogFormatter(c init.Log, stdoutWriter bool) oplogging.Formatter {
}
return
oplogging
.
MustStringFormatter
(
pattern
)
}
func
(
l
Logger
)
Debug
(
v
...
interface
{})
{
l
.
logger
.
Debug
(
v
)
}
func
(
l
Logger
)
Info
(
v
...
interface
{})
{
l
.
logger
.
Info
(
v
)
}
func
(
l
Logger
)
Warning
(
v
...
interface
{})
{
l
.
logger
.
Warning
(
v
)
}
func
(
l
Logger
)
Error
(
v
...
interface
{})
{
l
.
logger
.
Error
(
v
)
}
func
(
l
Logger
)
Critical
(
v
...
interface
{})
{
l
.
logger
.
Critical
(
v
)
}
func
(
l
Logger
)
Fatal
(
v
...
interface
{})
{
l
.
logger
.
Fatal
(
v
)
}
server/core/server.go
浏览文件 @
1f5edd3a
...
...
@@ -11,9 +11,9 @@ import (
func
RunWindowsServer
()
{
if
global
.
GVA_CONFIG
.
System
.
UseMultipoint
{
// 初始化redis服务
init
.
Re
gisterRe
dis
()
init
.
Redis
()
}
Router
:=
init
.
R
egisterRouter
()
Router
:=
init
.
R
outers
()
Router
.
Static
(
"/form-generator"
,
"./resource/page"
)
address
:=
fmt
.
Sprintf
(
":%d"
,
global
.
GVA_CONFIG
.
System
.
Addr
)
s
:=
&
http
.
Server
{
...
...
@@ -24,11 +24,11 @@ func RunWindowsServer() {
MaxHeaderBytes
:
1
<<
20
,
}
time
.
Sleep
(
10
*
time
.
Microsecond
)
init
.
L
.
Debug
(
"server run success on "
,
address
)
global
.
GVA_LOG
.
Debug
(
"server run success on "
,
address
)
fmt
.
Printf
(
`欢迎使用 Gin-Vue-Admin
默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
默认前端文件运行地址:http://127.0.0.1:8080
`
,
s
.
Addr
)
init
.
L
.
Error
(
s
.
ListenAndServe
())
global
.
GVA_LOG
.
Error
(
s
.
ListenAndServe
())
}
server/global/global.go
浏览文件 @
1f5edd3a
package
global
import
(
"gin-vue-admin/
init
"
"gin-vue-admin/
config
"
"github.com/go-redis/redis"
"github.com/jinzhu/gorm"
oplogging
"github.com/op/go-logging"
"github.com/spf13/viper"
)
var
(
GVA_DB
*
gorm
.
DB
GVA_REDIS
*
redis
.
Client
GVA_LOG
init
.
Logger
GVA_CONFIG
init
.
Config
GVA_CONFIG
config
.
Server
GVA_VP
*
viper
.
Viper
GVA_LOG
*
oplogging
.
Logger
)
server/init/
regist
_table.go
→
server/init/
db
_table.go
浏览文件 @
1f5edd3a
package
init
import
(
"gin-vue-admin/global"
"gin-vue-admin/model"
"github.com/jinzhu/gorm"
)
//注册数据库表专用
func
RegisterTable
(
db
*
gorm
.
DB
)
{
func
DBTables
()
{
db
:=
global
.
GVA_DB
db
.
AutoMigrate
(
model
.
SysUser
{},
model
.
SysAuthority
{},
model
.
SysMenu
{},
...
...
@@ -20,5 +21,5 @@ func RegisterTable(db *gorm.DB) {
model
.
ExaFileChunk
{},
model
.
ExaCustomer
{},
)
L
.
Debug
(
"register table success"
)
global
.
GVA_LOG
.
Debug
(
"register table success"
)
}
server/init/log.go
已删除
100644 → 0
浏览文件 @
2abf4013
package
init
// Custom Logger
type
Logger
interface
{
Debug
(
v
...
interface
{})
Info
(
v
...
interface
{})
Warning
(
v
...
interface
{})
Error
(
v
...
interface
{})
Critical
(
v
...
interface
{})
Fatal
(
v
...
interface
{})
}
var
L
Logger
func
SetLogger
(
logger
Logger
)
{
L
=
logger
}
server/init/mysql.go
浏览文件 @
1f5edd3a
...
...
@@ -7,9 +7,10 @@ import (
)
//初始化数据库并产生数据库全局变量
func
RegisterMysql
(
admin
MysqlAdmin
)
{
func
Mysql
()
{
admin
:=
global
.
GVA_CONFIG
.
MysqlAdmin
if
db
,
err
:=
gorm
.
Open
(
"mysql"
,
admin
.
Username
+
":"
+
admin
.
Password
+
"@("
+
admin
.
Path
+
")/"
+
admin
.
Dbname
+
"?"
+
admin
.
Config
);
err
!=
nil
{
L
.
Error
(
"DEFAULTDB数据库启动异常"
,
err
)
global
.
GVA_LOG
.
Error
(
"DEFAULTDB数据库启动异常"
,
err
)
}
else
{
global
.
GVA_DB
=
db
global
.
GVA_DB
.
DB
()
.
SetMaxIdleConns
(
admin
.
MaxIdleConns
)
...
...
server/init/redis.go
浏览文件 @
1f5edd3a
...
...
@@ -5,17 +5,18 @@ import (
"github.com/go-redis/redis"
)
func
RegisterRedis
()
{
func
Redis
()
{
redisCfg
:=
global
.
GVA_CONFIG
.
RedisAdmin
client
:=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
GinVueAdminconfig
.
RedisAdmin
.
Addr
,
Password
:
GinVueAdminconfig
.
RedisAdmin
.
Password
,
// no password set
DB
:
GinVueAdminconfig
.
RedisAdmin
.
DB
,
// use default DB
Addr
:
redisCfg
.
Addr
,
Password
:
redisCfg
.
Password
,
// no password set
DB
:
redisCfg
.
DB
,
// use default DB
})
pong
,
err
:=
client
.
Ping
()
.
Result
()
if
err
!=
nil
{
L
.
Error
(
err
)
global
.
GVA_LOG
.
Error
(
err
)
}
else
{
L
.
Info
(
"redis connect ping response:"
,
pong
)
global
.
GVA_LOG
.
Info
(
"redis connect ping response:"
,
pong
)
global
.
GVA_REDIS
=
client
}
}
server/init/router.go
浏览文件 @
1f5edd3a
...
...
@@ -2,6 +2,7 @@ package init
import
(
_
"gin-vue-admin/docs"
"gin-vue-admin/global"
"gin-vue-admin/middleware"
"gin-vue-admin/router"
"github.com/gin-gonic/gin"
...
...
@@ -10,18 +11,16 @@ import (
)
//初始化总路由
func
R
egisterRouter
()
*
gin
.
Engine
{
func
R
outers
()
*
gin
.
Engine
{
var
Router
=
gin
.
Default
()
//Router.Use(middleware.LoadTls()) // 打开就能玩https了
// 如果不需要日志 请关闭这里
Router
.
Use
(
middleware
.
Logger
())
L
.
Debug
(
"use middleware logger"
)
global
.
GVA_LOG
.
Debug
(
"use middleware logger"
)
// 跨域
Router
.
Use
(
middleware
.
Cors
())
L
.
Debug
(
"use middleware cors"
)
global
.
GVA_LOG
.
Debug
(
"use middleware cors"
)
Router
.
GET
(
"/swagger/*any"
,
ginSwagger
.
WrapHandler
(
swaggerFiles
.
Handler
))
L
.
Debug
(
"register swagger handler"
)
global
.
GVA_LOG
.
Debug
(
"register swagger handler"
)
// 方便统一添加路由组前缀 多服务器上线使用
ApiGroup
:=
Router
.
Group
(
""
)
router
.
InitUserRouter
(
ApiGroup
)
// 注册用户路由
...
...
server/main.go
浏览文件 @
1f5edd3a
...
...
@@ -4,36 +4,12 @@ import (
"gin-vue-admin/core"
"gin-vue-admin/global"
"gin-vue-admin/init"
"gin-vue-admin/init/qmlog"
"os"
//"runtime"
)
// @title Swagger Example API
// @version 0.0.1
// @description This is a sample Server pets
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name x-token
// @BasePath /
var
(
mysqlHost
=
os
.
Getenv
(
"MYSQLHOST"
)
mysqlPort
=
os
.
Getenv
(
"MYSQLPORT"
)
)
func
main
()
{
if
err
:=
qmlog
.
NewLogger
();
err
!=
nil
{
panic
(
err
)
}
// 可以通过环境变量来覆盖配置值
// 未设定有效的环境变量时,使用配置值
mysqlConfig
:=
init
.
GinVueAdminconfig
.
MysqlAdmin
// 链接初始化数据库
init
.
RegisterMysql
(
mysqlConfig
)
// 链接初始化数据库
// 注册数据库表
init
.
RegisterTable
(
global
.
GVA_DB
)
init
.
Mysql
()
init
.
DBTables
()
// 程序结束前关闭数据库链接
defer
global
.
GVA_DB
.
Close
()
...
...
server/middleware/logger.go
已删除
100644 → 0
浏览文件 @
2abf4013
package
middleware
import
(
"bytes"
"gin-vue-admin/init"
"net/http/httputil"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func
Logger
()
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
// request time
start
:=
time
.
Now
()
// request path
path
:=
c
.
Request
.
URL
.
Path
logFlag
:=
true
if
strings
.
Contains
(
path
,
"swagger"
)
{
logFlag
=
false
}
// request ip
clientIP
:=
c
.
ClientIP
()
// method
method
:=
c
.
Request
.
Method
// copy request content
req
,
_
:=
httputil
.
DumpRequest
(
c
.
Request
,
true
)
if
logFlag
{
init
.
L
.
Debug
(
"Request:"
,
method
,
clientIP
,
path
,
string
(
req
))
}
// replace writer
cusWriter
:=
&
responseBodyWriter
{
ResponseWriter
:
c
.
Writer
,
body
:
bytes
.
NewBufferString
(
""
),
}
c
.
Writer
=
cusWriter
// handle request
c
.
Next
()
// ending time
end
:=
time
.
Now
()
//execute time
latency
:=
end
.
Sub
(
start
)
statusCode
:=
c
.
Writer
.
Status
()
if
logFlag
{
init
.
L
.
Debug
(
"Response:"
,
statusCode
,
latency
,
cusWriter
.
body
.
String
())
}
}
}
type
responseBodyWriter
struct
{
gin
.
ResponseWriter
body
*
bytes
.
Buffer
}
func
(
w
responseBodyWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
w
.
body
.
Write
(
b
)
return
w
.
ResponseWriter
.
Write
(
b
)
}
server/model/sys_system.go
浏览文件 @
1f5edd3a
package
model
import
(
"gin-vue-admin/config"
"gin-vue-admin/global"
"gin-vue-admin/init"
"gin-vue-admin/utils"
)
//配置文件结构体
type
System
struct
{
Config
init
.
Config
Config
config
.
Server
}
//读取配置文件
func
(
s
*
System
)
GetSystemConfig
()
(
err
error
,
conf
init
.
Config
)
{
func
(
s
*
System
)
GetSystemConfig
()
(
err
error
,
conf
config
.
Server
)
{
return
nil
,
global
.
GVA_CONFIG
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录