mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-16 13:00:52 +08:00
update gin handle
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/lzh-1625/go_process_manager/internal/app/constants"
|
||||
@@ -10,17 +9,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func rOk(ctx *gin.Context, message string, data any) {
|
||||
jsonData := map[string]any{
|
||||
"code": 0,
|
||||
"msg": message,
|
||||
}
|
||||
if data != nil {
|
||||
jsonData["data"] = data
|
||||
}
|
||||
ctx.JSON(http.StatusOK, jsonData)
|
||||
}
|
||||
|
||||
func getRole(ctx *gin.Context) constants.Role {
|
||||
if v, ok := ctx.Get(constants.CTXFLG_ROLE); ok {
|
||||
return v.(constants.Role)
|
||||
@@ -39,3 +27,40 @@ func isAdmin(ctx *gin.Context) bool {
|
||||
func hasOprPermission(ctx *gin.Context, uuid int, op constants.OprPermission) bool {
|
||||
return isAdmin(ctx) || reflect.ValueOf(repository.PermissionRepository.GetPermission(getUserName(ctx), uuid)).FieldByName(string(op)).Bool()
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
StatusCode int
|
||||
Code int
|
||||
Data any
|
||||
Msg string
|
||||
}
|
||||
|
||||
func NewResponse() *Response {
|
||||
return &Response{StatusCode: 200}
|
||||
}
|
||||
|
||||
func (r *Response) SetStatusCode(code int) *Response {
|
||||
r.StatusCode = code
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Response) SetDate(data any) *Response {
|
||||
r.Data = data
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Response) SetCode(code int) *Response {
|
||||
r.Code = code
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Response) SetMessage(msg any) *Response {
|
||||
if str, ok := msg.(string); ok {
|
||||
r.Msg = str
|
||||
} else {
|
||||
if err, ok := msg.(error); ok {
|
||||
r.Msg = err.Error()
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"github.com/lzh-1625/go_process_manager/internal/app/logic"
|
||||
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -10,10 +11,8 @@ type configApi struct{}
|
||||
|
||||
var ConfigApi = new(configApi)
|
||||
|
||||
func (c *configApi) GetSystemConfiguration(ctx *gin.Context, _ any) error {
|
||||
result := logic.ConfigLogic.GetSystemConfiguration()
|
||||
rOk(ctx, "Operation successful!", result)
|
||||
return nil
|
||||
func (c *configApi) GetSystemConfiguration(ctx *gin.Context, _ any) []model.SystemConfigurationVo {
|
||||
return logic.ConfigLogic.GetSystemConfiguration()
|
||||
}
|
||||
|
||||
func (c *configApi) SetSystemConfiguration(ctx *gin.Context, _ any) (err error) {
|
||||
|
@@ -11,27 +11,25 @@ type file struct{}
|
||||
|
||||
var FileApi = new(file)
|
||||
|
||||
func (f *file) FilePathHandler(ctx *gin.Context, req model.FilePathHandlerReq) (err error) {
|
||||
rOk(ctx, "Operation successful!", logic.FileLogic.GetFileAndDirByPath(req.Path))
|
||||
return
|
||||
func (f *file) FilePathHandler(ctx *gin.Context, req model.FilePathHandlerReq) []model.FileStruct {
|
||||
return logic.FileLogic.GetFileAndDirByPath(req.Path)
|
||||
}
|
||||
|
||||
func (f *file) FileWriteHandler(ctx *gin.Context, _ any) (err error) {
|
||||
path := ctx.PostForm("filePath")
|
||||
fi, err := ctx.FormFile("data")
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
fiReader, _ := fi.Open()
|
||||
err = logic.FileLogic.UpdateFileData(path, fiReader, fi.Size)
|
||||
return
|
||||
}
|
||||
|
||||
func (f *file) FileReadHandler(ctx *gin.Context, req model.FileReadHandlerReq) (err error) {
|
||||
func (f *file) FileReadHandler(ctx *gin.Context, req model.FileReadHandlerReq) any {
|
||||
bytes, err := logic.FileLogic.ReadFileFromPath(req.FilePath)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
rOk(ctx, "Operation successful!", string(bytes))
|
||||
return
|
||||
return string(bytes)
|
||||
}
|
||||
|
@@ -16,9 +16,9 @@ type logApi struct{}
|
||||
|
||||
var LogApi = new(logApi)
|
||||
|
||||
func (a *logApi) GetLog(ctx *gin.Context, req model.GetLogReq) (err error) {
|
||||
func (a *logApi) GetLog(ctx *gin.Context, req model.GetLogReq) any {
|
||||
if isAdmin(ctx) {
|
||||
rOk(ctx, "Query successful!", logic.LogLogicImpl.Search(req, req.FilterName...))
|
||||
return logic.LogLogicImpl.Search(req, req.FilterName...)
|
||||
} else {
|
||||
processNameList := repository.PermissionRepository.GetProcessNameByPermission(getUserName(ctx), constants.OPERATION_LOG)
|
||||
filterName := slices.DeleteFunc(req.FilterName, func(s string) bool {
|
||||
@@ -30,12 +30,10 @@ func (a *logApi) GetLog(ctx *gin.Context, req model.GetLogReq) (err error) {
|
||||
if len(filterName) == 0 {
|
||||
return errors.New("no information found")
|
||||
}
|
||||
rOk(ctx, "Query successful!", logic.LogLogicImpl.Search(req, filterName...))
|
||||
return logic.LogLogicImpl.Search(req, filterName...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (a *logApi) GetRunningLog(ctx *gin.Context, _ any) error {
|
||||
rOk(ctx, "Query successful!", logic.Loghandler.GetRunning())
|
||||
return nil
|
||||
func (a *logApi) GetRunningLog(ctx *gin.Context, _ any) int {
|
||||
return logic.Loghandler.GetRunning()
|
||||
}
|
||||
|
@@ -15,8 +15,6 @@ func (p *permissionApi) EditPermssion(ctx *gin.Context, req model.Permission) (e
|
||||
return repository.PermissionRepository.EditPermssion(req)
|
||||
}
|
||||
|
||||
func (p *permissionApi) GetPermissionList(ctx *gin.Context, req model.GetPermissionListReq) (err error) {
|
||||
result := repository.PermissionRepository.GetPermssionList(req.Account)
|
||||
rOk(ctx, "Query successful!", result)
|
||||
return
|
||||
func (p *permissionApi) GetPermissionList(ctx *gin.Context, req model.GetPermissionListReq) any {
|
||||
return repository.PermissionRepository.GetPermssionList(req.Account)
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ type procApi struct{}
|
||||
|
||||
var ProcApi = new(procApi)
|
||||
|
||||
func (p *procApi) CreateNewProcess(ctx *gin.Context, req model.Process) (err error) {
|
||||
func (p *procApi) CreateNewProcess(ctx *gin.Context, req model.Process) any {
|
||||
index, err := repository.ProcessRepository.AddProcessConfig(req)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -28,10 +28,9 @@ func (p *procApi) CreateNewProcess(ctx *gin.Context, req model.Process) (err err
|
||||
return err
|
||||
}
|
||||
logic.ProcessCtlLogic.AddProcess(req.Uuid, proc)
|
||||
rOk(ctx, "Operation successful!", gin.H{
|
||||
return gin.H{
|
||||
"id": req.Uuid,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (p *procApi) DeleteNewProcess(ctx *gin.Context, req model.ProcessUuidReq) (err error) {
|
||||
@@ -84,13 +83,12 @@ func (p *procApi) KillAllProcess(ctx *gin.Context, _ any) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p *procApi) GetProcessList(ctx *gin.Context, _ any) (err error) {
|
||||
func (p *procApi) GetProcessList(ctx *gin.Context, _ any) any {
|
||||
if isAdmin(ctx) {
|
||||
rOk(ctx, "Query successful!", logic.ProcessCtlLogic.GetProcessList())
|
||||
return logic.ProcessCtlLogic.GetProcessList()
|
||||
} else {
|
||||
rOk(ctx, "Query successful!", logic.ProcessCtlLogic.GetProcessListByUser(getUserName(ctx)))
|
||||
return logic.ProcessCtlLogic.GetProcessListByUser(getUserName(ctx))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *procApi) UpdateProcessConfig(ctx *gin.Context, req model.Process) (err error) {
|
||||
@@ -99,13 +97,12 @@ func (p *procApi) UpdateProcessConfig(ctx *gin.Context, req model.Process) (err
|
||||
return
|
||||
}
|
||||
|
||||
func (p *procApi) GetProcessConfig(ctx *gin.Context, req model.ProcessUuidReq) (err error) {
|
||||
func (p *procApi) GetProcessConfig(ctx *gin.Context, req model.ProcessUuidReq) any {
|
||||
data, err := repository.ProcessRepository.GetProcessConfigById(req.Uuid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rOk(ctx, "success", data)
|
||||
return
|
||||
return data
|
||||
}
|
||||
|
||||
func (p *procApi) ProcessControl(ctx *gin.Context, req model.ProcessUuidReq) (err error) {
|
||||
@@ -118,9 +115,9 @@ func (p *procApi) ProcessControl(ctx *gin.Context, req model.ProcessUuidReq) (er
|
||||
return
|
||||
}
|
||||
|
||||
func (p *procApi) ProcessCreateShare(ctx *gin.Context, req model.ProcessShare) (err error) {
|
||||
func (p *procApi) ProcessCreateShare(ctx *gin.Context, req model.ProcessShare) any {
|
||||
token := utils.UnwarpIgnore(uuid.NewRandom()).String()
|
||||
if err = repository.WsShare.AddShareData(model.WsShare{
|
||||
if err := repository.WsShare.AddShareData(model.WsShare{
|
||||
ExpireTime: time.Now().Add(time.Minute * time.Duration(req.Minutes)),
|
||||
Write: req.Write,
|
||||
Token: token,
|
||||
@@ -129,8 +126,7 @@ func (p *procApi) ProcessCreateShare(ctx *gin.Context, req model.ProcessShare) (
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
rOk(ctx, "Operation successful!", gin.H{
|
||||
return gin.H{
|
||||
"token": token,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@@ -11,27 +11,22 @@ type pushApi struct{}
|
||||
|
||||
var PushApi = new(pushApi)
|
||||
|
||||
func (p *pushApi) GetPushList(ctx *gin.Context, __ any) (err error) {
|
||||
rOk(ctx, "Query successful!", repository.PushRepository.GetPushList())
|
||||
return
|
||||
func (p *pushApi) GetPushList(ctx *gin.Context, __ any) any {
|
||||
return repository.PushRepository.GetPushList()
|
||||
}
|
||||
|
||||
func (p *pushApi) GetPushById(ctx *gin.Context, req model.PushIdReq) (err error) {
|
||||
rOk(ctx, "Query successful!", repository.PushRepository.GetPushConfigById(req.Id))
|
||||
return
|
||||
func (p *pushApi) GetPushById(ctx *gin.Context, req model.PushIdReq) any {
|
||||
return repository.PushRepository.GetPushConfigById(req.Id)
|
||||
}
|
||||
|
||||
func (p *pushApi) AddPushConfig(ctx *gin.Context, req model.Push) (err error) {
|
||||
err = repository.PushRepository.AddPushConfig(req)
|
||||
return
|
||||
return repository.PushRepository.AddPushConfig(req)
|
||||
}
|
||||
|
||||
func (p *pushApi) UpdatePushConfig(ctx *gin.Context, req model.Push) (err error) {
|
||||
err = repository.PushRepository.UpdatePushConfig(req)
|
||||
return
|
||||
return repository.PushRepository.UpdatePushConfig(req)
|
||||
}
|
||||
|
||||
func (p *pushApi) DeletePushConfig(ctx *gin.Context, req model.PushIdReq) (err error) {
|
||||
err = repository.PushRepository.DeletePushConfig(req.Id)
|
||||
return
|
||||
return repository.PushRepository.DeletePushConfig(req.Id)
|
||||
}
|
||||
|
@@ -16,19 +16,16 @@ func (t *taskApi) CreateTask(ctx *gin.Context, req model.Task) (err error) {
|
||||
return logic.TaskLogic.CreateTask(req)
|
||||
}
|
||||
|
||||
func (t *taskApi) GetTaskById(ctx *gin.Context, req model.TaskIdReq) (err error) {
|
||||
func (t *taskApi) GetTaskById(ctx *gin.Context, req model.TaskIdReq) any {
|
||||
result, err := repository.TaskRepository.GetTaskById(req.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rOk(ctx, "Operation successful!", result)
|
||||
return
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *taskApi) GetTaskList(ctx *gin.Context, _ any) (err error) {
|
||||
result := logic.TaskLogic.GetAllTaskJob()
|
||||
rOk(ctx, "Operation successful!", result)
|
||||
return
|
||||
func (t *taskApi) GetTaskList(ctx *gin.Context, _ any) any {
|
||||
return logic.TaskLogic.GetAllTaskJob()
|
||||
}
|
||||
|
||||
func (t *taskApi) DeleteTaskById(ctx *gin.Context, req model.TaskIdReq) (err error) {
|
||||
|
@@ -18,20 +18,19 @@ var UserApi = new(userApi)
|
||||
|
||||
const DEFAULT_ROOT_PASSWORD = "root"
|
||||
|
||||
func (u *userApi) LoginHandler(ctx *gin.Context, req model.LoginHandlerReq) (err error) {
|
||||
func (u *userApi) LoginHandler(ctx *gin.Context, req model.LoginHandlerReq) any {
|
||||
if !u.checkLoginInfo(req.Account, req.Password) {
|
||||
return errors.New("incorrect username or password")
|
||||
}
|
||||
token, err := utils.GenToken(req.Account)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
rOk(ctx, "Operation successful!", gin.H{
|
||||
return gin.H{
|
||||
"token": token,
|
||||
"username": req.Account,
|
||||
"role": repository.UserRepository.GetUserByName(req.Account).Role,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (u *userApi) CreateUser(ctx *gin.Context, req model.User) (err error) {
|
||||
@@ -74,9 +73,8 @@ func (u *userApi) DeleteUser(ctx *gin.Context, req model.User) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (u *userApi) GetUserList(ctx *gin.Context, _ any) error {
|
||||
rOk(ctx, "Query successful!", repository.UserRepository.GetUserList())
|
||||
return nil
|
||||
func (u *userApi) GetUserList(ctx *gin.Context, _ any) any {
|
||||
return repository.UserRepository.GetUserList()
|
||||
}
|
||||
|
||||
func (u *userApi) checkLoginInfo(account, password string) bool {
|
||||
|
@@ -197,13 +197,10 @@ func (w *wsApi) startWsConnect(wci *WsConnetInstance, cancel context.CancelFunc,
|
||||
|
||||
}
|
||||
|
||||
func GetWsShareList(ctx *gin.Context) {
|
||||
rOk(ctx, "Operation successful!", logic.WsSahreLogic.GetWsShareList())
|
||||
func GetWsShareList(ctx *gin.Context, _ any) any {
|
||||
return logic.WsSahreLogic.GetWsShareList()
|
||||
}
|
||||
|
||||
func DeleteWsShareById(ctx *gin.Context) {
|
||||
if err := logic.WsSahreLogic.DeleteById(ctx.GetInt("id")); err != nil {
|
||||
return
|
||||
}
|
||||
rOk(ctx, "Operation successful!", nil)
|
||||
func DeleteWsShareById(ctx *gin.Context, _ any) any {
|
||||
return logic.WsSahreLogic.DeleteById(ctx.GetInt("id"))
|
||||
}
|
||||
|
@@ -150,7 +150,7 @@ const (
|
||||
Query
|
||||
)
|
||||
|
||||
func bind[T any](fn func(*gin.Context, T) error, bindOption int) func(*gin.Context) {
|
||||
func bind[T any, R any](fn func(*gin.Context, T) R, bindOption int) func(*gin.Context) {
|
||||
return func(ctx *gin.Context) {
|
||||
var req T
|
||||
if bindOption&Body != 0 {
|
||||
@@ -171,16 +171,32 @@ func bind[T any](fn func(*gin.Context, T) error, bindOption int) func(*gin.Conte
|
||||
return
|
||||
}
|
||||
}
|
||||
err := fn(ctx, req)
|
||||
if err != nil {
|
||||
rErr(ctx, err)
|
||||
result := fn(ctx, req)
|
||||
switch v := any(result).(type) {
|
||||
case error:
|
||||
if v != nil {
|
||||
rErr(ctx, v)
|
||||
return
|
||||
}
|
||||
if !ctx.Writer.Written() {
|
||||
} else {
|
||||
ctx.JSON(200, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
})
|
||||
return
|
||||
}
|
||||
case api.Response:
|
||||
ctx.JSON(v.StatusCode, gin.H{
|
||||
"data": v.Data,
|
||||
"msg": v.Msg,
|
||||
"code": v.Code,
|
||||
})
|
||||
return
|
||||
default:
|
||||
ctx.JSON(200, gin.H{
|
||||
"code": 0,
|
||||
"message": "success",
|
||||
"data": v,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user