This commit is contained in:
zhangwenjian committed 2020-04-13 00:30:58 +08:00
1 parent f25da5f1ee
commit 1df55e75e5
72 files changed
+1401 -1559

No files matched your search

+18 -22
View File
@@ -4,9 +4,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
"github.com/mssola/user_agent"
. "go-admin/models"
"go-admin/models"
jwt "go-admin/pkg/jwtauth"
"go-admin/utils"
"go-admin/pkg/utils"
"log"
"net/http"
)
@@ -15,15 +15,15 @@ var store = base64Captcha.DefaultMemStore
func PayloadFunc(data interface{}) jwt.MapClaims {
if v, ok := data.(map[string]interface{}); ok {
u, _ := v["user"].(SysUser)
r, _ := v["role"].(SysRole)
u, _ := v["user"].(models.SysUser)
r, _ := v["role"].(models.SysRole)
return jwt.MapClaims{
jwt.IdentityKey: u.Id,
jwt.RoleIdKey: r.Id,
jwt.IdentityKey: u.UserId,
jwt.RoleIdKey: r.RoleId,
jwt.RoleKey: r.RoleKey,
jwt.NiceKey: u.Username,
jwt.DataScopeKey: r.DataScope,
jwt.RoleNameKey: r.Name,
jwt.RoleNameKey: r.RoleName,
}
}
return jwt.MapClaims{}
@@ -52,17 +52,15 @@ func IdentityHandler(c *gin.Context) interface{} {
// @Success 200 {string} string "{"code": 200, "expire": "2019-08-07T12:45:48+08:00", "token": ".eyJleHAiOjE1NjUxNTMxNDgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTU2NTE0OTU0OH0.-zvzHvbg0A" }"
// @Router /login [post]
func Authenticator(c *gin.Context) (interface{}, error) {
var loginVals Login
var loginlog LoginLog
var loginVals models.Login
var loginlog models.LoginLog
ua := user_agent.New(c.Request.UserAgent())
loginlog.Ipaddr = c.ClientIP()
location := utils.GetLocation(c.ClientIP())
loginlog.LoginLocation = location
loginlog.LoginTime = utils.GetCurrntTime()
loginlog.CreateTime = utils.GetCurrntTime()
loginlog.Status = "0"
loginlog.IsDel = "0"
loginlog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginlog.Browser = browserName + " " + browserVersion
@@ -73,11 +71,11 @@ func Authenticator(c *gin.Context) (interface{}, error) {
if err := c.ShouldBind(&loginVals); err != nil {
loginlog.Status = "1"
loginlog.Msg = "数据解析失败"
loginlog.UserName = loginVals.Username
loginlog.Username = loginVals.Username
loginlog.Create()
return nil, jwt.ErrMissingLoginValues
}
loginlog.UserName = loginVals.Username
loginlog.Username = loginVals.Username
if !store.Verify(loginVals.UUID, loginVals.Code, true) {
loginlog.Status = "1"
loginlog.Msg = "验证码错误"
@@ -109,21 +107,19 @@ func Authenticator(c *gin.Context) (interface{}, error) {
// @Router /logout [post]
// @Security
func LogOut(c *gin.Context) {
var loginlog LoginLog
var loginlog models.LoginLog
ua := user_agent.New(c.Request.UserAgent())
loginlog.Ipaddr = c.ClientIP()
location := utils.GetLocation(c.ClientIP())
loginlog.LoginLocation = location
loginlog.LoginTime = utils.GetCurrntTime()
loginlog.CreateTime = utils.GetCurrntTime()
loginlog.Status = "0"
loginlog.IsDel = "0"
loginlog.Remark = c.Request.UserAgent()
browserName, browserVersion := ua.Browser()
loginlog.Browser = browserName + " " + browserVersion
loginlog.Os = ua.OS()
loginlog.Platform = ua.Platform()
loginlog.UserName = utils.GetUserName(c)
loginlog.Username = utils.GetUserName(c)
loginlog.Msg = "退出成功"
loginlog.Create()
c.JSON(http.StatusOK, gin.H{
@@ -136,11 +132,11 @@ func LogOut(c *gin.Context) {
func Authorizator(data interface{}, c *gin.Context) bool {
if v, ok := data.(map[string]interface{}); ok {
u, _ := v["user"].(SysUser)
r, _ := v["role"].(SysRole)
c.Set("role", r.Name)
c.Set("roleIds", r.Id)
c.Set("userId", u.Id)
u, _ := v["user"].(models.SysUser)
r, _ := v["role"].(models.SysRole)
c.Set("role", r.RoleName)
c.Set("roleIds", r.RoleId)
c.Set("userId", u.UserId)
c.Set("userName", u.UserName)
c.Set("dataScope", r.DataScope)
-19
View File
@@ -1,19 +0,0 @@
package cronjob
import (
"github.com/robfig/cron/v3"
"log"
)
func TestJob(c *cron.Cron) {
id, err := c.AddFunc("1 * * * *", func() {
log.Println("Every hour on the one hour")
})
if err != nil {
log.Println(err)
log.Println("start error")
} else {
log.Println("Start Success; ID: %v", id)
}
}
+3 -2
View File
@@ -4,13 +4,14 @@ import (
"github.com/gin-gonic/gin"
jwt "go-admin/pkg/jwtauth"
"log"
"net/http"
)
func NoFound(c *gin.Context) {
claims := jwt.ExtractClaims(c)
log.Printf("NoRoute claims: %#v\n", claims)
c.JSON(404, gin.H{
"code": "NOT_FOUND",
c.JSON(http.StatusOK, gin.H{
"code": "404",
"message": "not found",
})
}
+11
View File
@@ -0,0 +1,11 @@
package handler
import (
"github.com/gin-gonic/gin"
)
func Ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "ok",
})
}
+32 -27
View File
@@ -7,7 +7,7 @@ import (
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"go-admin/utils"
"go-admin/pkg/app"
"net/http"
"runtime"
"time"
@@ -29,8 +29,7 @@ const (
// @Router /sd/health [get]
// @BasePath
func HealthCheck(c *gin.Context) {
message := "OK"
c.String(http.StatusOK, "\n"+message)
app.OK(c, "", "OK")
}
// @Summary 服务器硬盘使用量
@@ -70,28 +69,27 @@ func DiskCheck(c *gin.Context) {
// @Description Os
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string "OK - Free space: 455MB (0GB) / 8192MB (8GB) | Used: 5%"
// @Failure 500 {string} string "CRITICAL"
// @Failure 429 {string} string "WARNING"
// @Success 200 {string} string ""
// @Router /sd/os [get]
// @BasePath
func OSCheck(c *gin.Context) {
fmt.Println(runtime.GOOS) //取得操作系统版本
fmt.Println(runtime.Compiler) //取得编译器名称-gc
fmt.Println(runtime.NumCPU()) //取得CPU多核的数目
fmt.Println(runtime.Version())
status := http.StatusOK
c.String(status, runtime.GOOS+"/"+runtime.GOARCH+"/"+utils.IntToString(runtime.NumCPU()))
app.Custum(c, gin.H{
"code": 200,
"status": status,
"goOs": runtime.GOOS,
"compiler": runtime.Compiler,
"numCpu": runtime.NumCPU(),
"version": runtime.Version(),
"numGoroutine": runtime.NumGoroutine(),
})
}
// @Summary CPU 使用量
// @Description CPU 使用量 DiskCheck checks the disk usage.
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string "OK - Load average: 2.39, 2.13, 1.97 | Cores: 2"
// @Failure 500 {string} string "CRITICAL"
// @Failure 429 {string} string "WARNING"
// @Success 200 {string} string ""
// @Router /sd/cpu [get]
// @BasePath
func CPUCheck(c *gin.Context) {
@@ -104,8 +102,6 @@ func CPUCheck(c *gin.Context) {
}
}
fmt.Println(c)
a, _ := load.Avg()
l1 := a.Load1
l5 := a.Load5
@@ -121,27 +117,29 @@ func CPUCheck(c *gin.Context) {
status = http.StatusTooManyRequests
text = "WARNING"
}
message := fmt.Sprintf("%s - Load average: %.2f, %.2f, %.2f | Cores: %d", text, l1, l5, l15, cores)
c.String(status, "\n"+message)
app.Custum(c, gin.H{
"code": 200,
"msg": text,
"status": status,
"cores": cores,
"load1": l1,
"load5": l5,
"load15": l15,
})
}
// @Summary 内存使用量
// @Description 内存使用量 RAMCheck checks the disk usage.
// @Accept text/html
// @Produce text/html
// @Success 200 {string} string "OK - Free space: 455MB (0GB) / 8192MB (8GB) | Used: 5%"
// @Failure 500 {string} string "CRITICAL"
// @Failure 429 {string} string "WARNING"
// @Success 200 {string} string ""
// @Router /sd/ram [get]
// @BasePath
func RAMCheck(c *gin.Context) {
u, _ := mem.VirtualMemory()
usedMB := int(u.Used) / MB
usedGB := int(u.Used) / GB
totalMB := int(u.Total) / MB
totalGB := int(u.Total) / GB
usedPercent := int(u.UsedPercent)
status := http.StatusOK
@@ -155,6 +153,13 @@ func RAMCheck(c *gin.Context) {
text = "WARNING"
}
message := fmt.Sprintf("%s - Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%", text, usedMB, usedGB, totalMB, totalGB, usedPercent)
c.String(status, "\n"+message)
app.Custum(c, gin.H{
"code": 200,
"msg": text,
"status": status,
"used": usedMB,
"total": totalMB,
"usedPercent": usedPercent,
})
}