Add the Loki query UI, access policies, ingress insights, alert and registry settings, and their backend APIs. Include the related Kubernetes deployment and collector updates required by the integrated application. Validated with go test ./..., npm run build, and npm run lint.
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package controllers
|
|
|
|
import (
|
|
"opsxloki/dto"
|
|
"opsxloki/models"
|
|
"opsxloki/services"
|
|
"opsxloki/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type StorageController struct {
|
|
service *services.StorageService
|
|
}
|
|
|
|
func NewStorageController() *StorageController {
|
|
return &StorageController{service: services.NewStorageService()}
|
|
}
|
|
|
|
func (ctrl *StorageController) Create(c *gin.Context) {
|
|
var req dto.CreateStorageRequest
|
|
if !utils.BindAndValidate(c, &req) {
|
|
return
|
|
}
|
|
storage, err := ctrl.service.Create(req.Name, req.StorageType, req.Endpoint, req.Bucket,
|
|
req.Region, req.AccessKey, req.SecretKey, req.PathPrefix, req.Insecure, req.IsDefault)
|
|
if err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(storage))
|
|
}
|
|
|
|
func (ctrl *StorageController) Get(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
storage, err := ctrl.service.Get(uint(id))
|
|
if err != nil {
|
|
utils.NotFound(c, "存储配置不存在")
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(storage))
|
|
}
|
|
|
|
func (ctrl *StorageController) List(c *gin.Context) {
|
|
page, pageSize := utils.Pagination(c)
|
|
storages, total, err := ctrl.service.List(page, pageSize)
|
|
if err != nil {
|
|
utils.Error(c, 500, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, utils.PageData{
|
|
List: models.SanitizedJSON(storages),
|
|
Total: total, Page: page, PageSize: pageSize,
|
|
})
|
|
}
|
|
|
|
func (ctrl *StorageController) Update(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
var updates map[string]interface{}
|
|
if !utils.BindAndValidate(c, &updates) {
|
|
return
|
|
}
|
|
if err := ctrl.service.Update(uint(id), updates); err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"message": "updated"})
|
|
}
|
|
|
|
func (ctrl *StorageController) Delete(c *gin.Context) {
|
|
id, err := utils.IntParam(c, "id")
|
|
if err != nil {
|
|
utils.BadRequest(c, "ID 无效")
|
|
return
|
|
}
|
|
if err := ctrl.service.Delete(uint(id)); err != nil {
|
|
utils.Error(c, 400, err.Error())
|
|
return
|
|
}
|
|
utils.Success(c, gin.H{"message": "deleted"})
|
|
}
|
|
|
|
func (ctrl *StorageController) GetDefault(c *gin.Context) {
|
|
storage, err := ctrl.service.GetDefault()
|
|
if err != nil {
|
|
utils.NotFound(c, "未设置默认存储配置")
|
|
return
|
|
}
|
|
utils.Success(c, models.SanitizedJSON(storage))
|
|
}
|
|
|
|
// TestConnection 测试对象存储连接(凭证 + 桶存在性,可选自动创建;不落库)
|
|
func (ctrl *StorageController) TestConnection(c *gin.Context) {
|
|
var req dto.StorageTestRequest
|
|
if !utils.BindAndValidate(c, &req) {
|
|
return
|
|
}
|
|
result := services.TestStorageConnection(services.StorageTestParams{
|
|
StorageType: req.StorageType,
|
|
Endpoint: req.Endpoint,
|
|
Bucket: req.Bucket,
|
|
Region: req.Region,
|
|
AccessKey: req.AccessKey,
|
|
SecretKey: req.SecretKey,
|
|
Insecure: req.Insecure,
|
|
CreateIfMissing: req.CreateIfMissing,
|
|
})
|
|
utils.Success(c, result)
|
|
}
|