You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
EasyDarwin/routers/sys.go

230 lines
5.8 KiB

6 years ago
package routers
import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
"time"
"github.com/EasyDarwin/EasyDarwin/models"
"github.com/EasyDarwin/EasyDarwin/rtsp"
6 years ago
"github.com/gin-gonic/gin"
"github.com/penggy/EasyGoLib/db"
"github.com/penggy/EasyGoLib/utils"
"github.com/penggy/sessions"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
)
/**
6 years ago
* @apiDefine sys
6 years ago
*/
type APIHandler struct {
RestartChan chan bool
}
var API = &APIHandler{
RestartChan: make(chan bool),
}
var (
memData []PercentData = make([]PercentData, 0)
cpuData []PercentData = make([]PercentData, 0)
pusherData []CountData = make([]CountData, 0)
playerData []CountData = make([]CountData, 0)
)
func init() {
go func() {
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
timeSize := 30
for {
select {
case <-ticker.C:
mem, _ := mem.VirtualMemory()
cpu, _ := cpu.Percent(0, false)
now := utils.DateTime(time.Now())
memData = append(memData, PercentData{Time: now, Used: mem.UsedPercent / 100})
cpuData = append(cpuData, PercentData{Time: now, Used: cpu[0] / 100})
pusherData = append(pusherData, CountData{Time: now, Total: uint(rtsp.Instance.GetPusherSize())})
playerCnt := 0
for _, pusher := range rtsp.Instance.GetPushers() {
playerCnt += len(pusher.GetPlayers())
}
playerData = append(playerData, CountData{Time: now, Total: uint(playerCnt)})
if len(memData) > timeSize {
memData = memData[len(memData)-timeSize:]
}
if len(cpuData) > timeSize {
cpuData = cpuData[len(cpuData)-timeSize:]
}
if len(pusherData) > timeSize {
pusherData = pusherData[len(pusherData)-timeSize:]
}
if len(playerData) > timeSize {
playerData = playerData[len(playerData)-timeSize:]
}
}
}
}()
}
func (h *APIHandler) ModifyPassword(c *gin.Context) {
type Form struct {
OldPassword string `form:"oldpassword" binding:"required"`
NewPassword string `form:"newpassword" binding:"required"`
}
var form Form
if err := c.Bind(&form); err != nil {
return
}
sess := sessions.Default(c)
var user models.User
db.SQLite.First(&user, sess.Get("uid"))
if user.ID != "" && strings.EqualFold(form.OldPassword, user.Password) {
db.SQLite.Model(&user).Update("password", form.NewPassword)
} else {
c.AbortWithStatusJSON(http.StatusBadRequest, "原密码不正确")
return
}
token, _ := sess.RenewID()
c.IndentedJSON(http.StatusOK, gin.H{
"token": token,
})
}
/**
6 years ago
* @api {get} /api/v1/getserverinfo
6 years ago
* @apiGroup sys
* @apiName GetServerInfo
* @apiSuccess (200) {String} Hardware
* @apiSuccess (200) {String} RunningTime
* @apiSuccess (200) {String} StartUpTime
* @apiSuccess (200) {String} Server
*/
func (h *APIHandler) GetServerInfo(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{
"Hardware": strings.ToUpper(runtime.GOARCH),
"InterfaceVersion": "V1",
"RunningTime": utils.UpTimeString(),
"StartUpTime": utils.DateTime(utils.StartTime),
"Server": fmt.Sprintf("%s/%s,%s (Platform/%s;)", "EasyDarwin", BuildDateTime, BuildVersion, strings.Title(runtime.GOOS)),
6 years ago
"memData": memData,
"cpuData": cpuData,
"pusherData": pusherData,
"playerData": playerData,
})
}
/**
6 years ago
* @api {get} /api/v1/restart
6 years ago
* @apiGroup sys
* @apiName Restart
* @apiUse simpleSuccess
*/
func (h *APIHandler) Restart(c *gin.Context) {
log.Println("Restart...")
c.JSON(http.StatusOK, "OK")
go func() {
select {
case h.RestartChan <- true:
default:
}
}()
}
/**
* @apiDefine userInfo
* @apiSuccess (200) {String} id
* @apiSuccess (200) {String} name
* @apiSuccess (200) {String[]} [roles]
*/
/**
6 years ago
* @api {get} /api/v1/login
6 years ago
* @apiGroup sys
* @apiName Login
* @apiParam {String} username
* @apiParam {String} password (md5,32,线,)
* @apiSuccessExample
* HTTP/1.1 200 OK
* Set-Cookie: token=s%3ArkyMbQE0M.5AKAOXbW8c7iP%2BOo0venPkCYiEiPK9FY31mB6AlFQak;//用着后续接口调用的 token
*/
func (h *APIHandler) Login(c *gin.Context) {
type Form struct {
Username string `form:"username" binding:"required"`
Password string `form:"password" binding:"required"`
}
var form Form
if err := c.Bind(&form); err != nil {
return
}
var user models.User
db.SQLite.Where(&models.User{Username: form.Username}).First(&user)
if user.ID == "" {
c.AbortWithStatusJSON(401, "用户名或密码错误")
return
}
if !strings.EqualFold(user.Password, form.Password) {
c.AbortWithStatusJSON(401, "用户名或密码错误")
return
}
sess := sessions.Default(c)
sess.Set("uid", user.ID)
sess.Set("uname", user.Username)
c.IndentedJSON(200, gin.H{
"token": sessions.Default(c).ID(),
})
}
/**
6 years ago
* @api {get} /api/v1/userInfo
6 years ago
* @apiGroup sys
* @apiName UserInfo
* @apiUse userInfo
*/
func (h *APIHandler) UserInfo(c *gin.Context) {
sess := sessions.Default(c)
uid := sess.Get("uid")
if uid != nil {
c.IndentedJSON(200, gin.H{
"id": uid,
"name": sess.Get("uname"),
})
} else {
c.IndentedJSON(200, nil)
}
}
/**
6 years ago
* @api {get} /api/v1/logout
6 years ago
* @apiGroup sys
* @apiName Logout
* @apiUse simpleSuccess
*/
func (h *APIHandler) Logout(c *gin.Context) {
sess := sessions.Default(c)
sess.Destroy()
c.IndentedJSON(200, "OK")
}
func (h *APIHandler) DefaultLoginInfo(c *gin.Context) {
var user models.User
sec := utils.Conf().Section("http")
defUser := sec.Key("default_username").MustString("admin")
defPass := sec.Key("default_password").MustString("admin")
db.SQLite.First(&user, "username = ?", defUser)
if utils.MD5(defPass) != user.Password {
defPass = ""
}
c.JSON(200, gin.H{
"username": defUser,
"password": defPass,
})
}