|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + |
| 6 | + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" |
| 7 | + "github.com/1Panel-dev/1Panel/backend/app/dto" |
| 8 | + "github.com/1Panel-dev/1Panel/backend/constant" |
| 9 | + "github.com/gin-gonic/gin" |
| 10 | +) |
| 11 | + |
| 12 | +// @Tags Device |
| 13 | +// @Summary Load device base info |
| 14 | +// @Description 获取设备基础信息 |
| 15 | +// @Success 200 {object} dto.DeviceBaseInfo |
| 16 | +// @Security ApiKeyAuth |
| 17 | +// @Router /toolbox/device/base [get] |
| 18 | +func (b *BaseApi) LoadDeviceBaseInfo(c *gin.Context) { |
| 19 | + data, err := deviceService.LoadBaseInfo() |
| 20 | + if err != nil { |
| 21 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + helper.SuccessWithData(c, data) |
| 26 | +} |
| 27 | + |
| 28 | +// @Tags Device |
| 29 | +// @Summary list time zone options |
| 30 | +// @Description 获取系统可用时区选项 |
| 31 | +// @Accept json |
| 32 | +// @Success 200 {Array} string |
| 33 | +// @Security ApiKeyAuth |
| 34 | +// @Router /toolbox/device/zone/options [get] |
| 35 | +func (b *BaseApi) LoadTimeOption(c *gin.Context) { |
| 36 | + list, err := deviceService.LoadTimeZone() |
| 37 | + if err != nil { |
| 38 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + helper.SuccessWithData(c, list) |
| 43 | +} |
| 44 | + |
| 45 | +// @Tags Device |
| 46 | +// @Summary load conf |
| 47 | +// @Description 获取系统配置文件 |
| 48 | +// @Accept json |
| 49 | +// @Param request body dto.OperationWithName true "request" |
| 50 | +// @Success 200 |
| 51 | +// @Security ApiKeyAuth |
| 52 | +// @Router /toolbox/device/conf [post] |
| 53 | +func (b *BaseApi) LoadDeviceConf(c *gin.Context) { |
| 54 | + var req dto.OperationWithName |
| 55 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + list, err := deviceService.LoadConf(req.Name) |
| 60 | + if err != nil { |
| 61 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + helper.SuccessWithData(c, list) |
| 66 | +} |
| 67 | + |
| 68 | +// @Tags Device |
| 69 | +// @Summary Update device conf by file |
| 70 | +// @Description 通过文件修改配置 |
| 71 | +// @Accept json |
| 72 | +// @Param request body dto.UpdateByNameAndFile true "request" |
| 73 | +// @Success 200 |
| 74 | +// @Security ApiKeyAuth |
| 75 | +// @Router /toolbox/device/update/byconf [post] |
| 76 | +func (b *BaseApi) UpdateDevicByFile(c *gin.Context) { |
| 77 | + var req dto.UpdateByNameAndFile |
| 78 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 79 | + return |
| 80 | + } |
| 81 | + if err := deviceService.UpdateByConf(req); err != nil { |
| 82 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + helper.SuccessWithData(c, nil) |
| 87 | +} |
| 88 | + |
| 89 | +// @Tags Device |
| 90 | +// @Summary Update device |
| 91 | +// @Description 修改系统参数 |
| 92 | +// @Accept json |
| 93 | +// @Param request body dto.SettingUpdate true "request" |
| 94 | +// @Success 200 |
| 95 | +// @Security ApiKeyAuth |
| 96 | +// @Router /toolbox/device/update/conf [post] |
| 97 | +// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机参数 [key] => [value]","formatEN":"update device conf [key] => [value]"} |
| 98 | +func (b *BaseApi) UpdateDeviceConf(c *gin.Context) { |
| 99 | + var req dto.SettingUpdate |
| 100 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + if err := deviceService.Update(req.Key, req.Value); err != nil { |
| 105 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + helper.SuccessWithData(c, nil) |
| 110 | +} |
| 111 | + |
| 112 | +// @Tags Device |
| 113 | +// @Summary Update device hosts |
| 114 | +// @Description 修改系统 hosts |
| 115 | +// @Success 200 |
| 116 | +// @Security ApiKeyAuth |
| 117 | +// @Router /toolbox/device/update/host [post] |
| 118 | +// @x-panel-log {"bodyKeys":["key","value"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"修改主机 Host [key] => [value]","formatEN":"update device host [key] => [value]"} |
| 119 | +func (b *BaseApi) UpdateDeviceHost(c *gin.Context) { |
| 120 | + var req []dto.HostHelper |
| 121 | + if err := helper.CheckBind(&req, c); err != nil { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if err := deviceService.UpdateHosts(req); err != nil { |
| 126 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + helper.SuccessWithData(c, nil) |
| 131 | +} |
| 132 | + |
| 133 | +// @Tags Device |
| 134 | +// @Summary Update device passwd |
| 135 | +// @Description 修改系统密码 |
| 136 | +// @Accept json |
| 137 | +// @Param request body dto.ChangePasswd true "request" |
| 138 | +// @Success 200 |
| 139 | +// @Security ApiKeyAuth |
| 140 | +// @Router /toolbox/device/update/passwd [post] |
| 141 | +func (b *BaseApi) UpdateDevicPasswd(c *gin.Context) { |
| 142 | + var req dto.ChangePasswd |
| 143 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 144 | + return |
| 145 | + } |
| 146 | + if len(req.Passwd) != 0 { |
| 147 | + password, err := base64.StdEncoding.DecodeString(req.Passwd) |
| 148 | + if err != nil { |
| 149 | + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) |
| 150 | + return |
| 151 | + } |
| 152 | + req.Passwd = string(password) |
| 153 | + } |
| 154 | + if err := deviceService.UpdatePasswd(req); err != nil { |
| 155 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + helper.SuccessWithData(c, nil) |
| 160 | +} |
| 161 | + |
| 162 | +// @Tags Device |
| 163 | +// @Summary Check device DNS conf |
| 164 | +// @Description 检查系统 DNS 配置可用性 |
| 165 | +// @Accept json |
| 166 | +// @Param request body dto.SettingUpdate true "request" |
| 167 | +// @Success 200 |
| 168 | +// @Security ApiKeyAuth |
| 169 | +// @Router /toolbox/device/check/dns [post] |
| 170 | +func (b *BaseApi) CheckDNS(c *gin.Context) { |
| 171 | + var req dto.SettingUpdate |
| 172 | + if err := helper.CheckBindAndValidate(&req, c); err != nil { |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + data, err := deviceService.CheckDNS(req.Key, req.Value) |
| 177 | + if err != nil { |
| 178 | + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + helper.SuccessWithData(c, data) |
| 183 | +} |
0 commit comments