feat: LokiDeploy 初始提交 - 轻量级 K8s Loki 部署平台
Go(Gin) + React(Vite + shadcn/ui + Tailwind) 前后端分离,全中文 UI - 集群管理:Kubeconfig / Token 双接入,kubectl + helm 部署 - 主机管理:SSH 密钥 / 密码双认证,CPU/内存/磁盘使用率监控,SSH 错误中文友好提示 - 存储配置、部署管理、系统设置 - Docker 镜像内置 kubectl + helm,开箱即用 - run.sh 一键启停(start/stop/restart/status) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
2a93de600d
134 files changed
+19595
No files matched your search
@@ -0,0 +1,94 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/models"
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthController struct {
|
||||
service *services.AuthService
|
||||
}
|
||||
|
||||
func NewAuthController() *AuthController {
|
||||
return &AuthController{service: services.NewAuthService()}
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=6"`
|
||||
Nickname string `json:"nickname"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type ChangePasswordRequest struct {
|
||||
OldPassword string `json:"oldPassword" binding:"required"`
|
||||
NewPassword string `json:"newPassword" binding:"required,min=6"`
|
||||
}
|
||||
|
||||
func (ctrl *AuthController) Login(c *gin.Context) {
|
||||
var req LoginRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
user, token, err := ctrl.service.Login(req.Username, req.Password)
|
||||
if err != nil {
|
||||
utils.Unauthorized(c, "用户名或密码错误")
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"token": token, "user": models.SanitizedJSON(user)})
|
||||
}
|
||||
|
||||
func (ctrl *AuthController) Register(c *gin.Context) {
|
||||
var req RegisterRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
if req.Role == "" {
|
||||
req.Role = "user"
|
||||
}
|
||||
user, err := ctrl.service.Register(req.Username, req.Password, req.Nickname, req.Role)
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(user))
|
||||
}
|
||||
|
||||
func (ctrl *AuthController) GetUser(c *gin.Context) {
|
||||
claims, ok := utils.GetUserFromContext(c)
|
||||
if !ok {
|
||||
utils.Unauthorized(c, "")
|
||||
return
|
||||
}
|
||||
user, err := ctrl.service.GetUserByID(claims.UserID)
|
||||
if err != nil {
|
||||
utils.Error(c, 404, "用户不存在")
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(user))
|
||||
}
|
||||
|
||||
func (ctrl *AuthController) ChangePassword(c *gin.Context) {
|
||||
var req ChangePasswordRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
claims, ok := utils.GetUserFromContext(c)
|
||||
if !ok {
|
||||
utils.Unauthorized(c, "")
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.ChangePassword(claims.UserID, req.OldPassword, req.NewPassword); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "password changed"})
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/models"
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ClusterController struct {
|
||||
service *services.ClusterService
|
||||
}
|
||||
|
||||
func NewClusterController() *ClusterController {
|
||||
return &ClusterController{service: services.NewClusterService()}
|
||||
}
|
||||
|
||||
type CreateClusterRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
ConfigType string `json:"configType"` // kubeconfig | token
|
||||
APIServer string `json:"apiServer"` // token 模式必填
|
||||
KubeConfig string `json:"kubeconfig"` // kubeconfig 模式必填
|
||||
Token string `json:"token"` // token 模式必填
|
||||
Context string `json:"context"`
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) Create(c *gin.Context) {
|
||||
var req CreateClusterRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
if req.ConfigType == "" {
|
||||
req.ConfigType = "kubeconfig"
|
||||
}
|
||||
if req.ConfigType == "kubeconfig" && req.KubeConfig == "" {
|
||||
utils.BadRequest(c, "请填写 kubeconfig 内容")
|
||||
return
|
||||
}
|
||||
if req.ConfigType == "token" && (req.APIServer == "" || req.Token == "") {
|
||||
utils.BadRequest(c, "Token 接入方式需要填写 API Server 和 Token")
|
||||
return
|
||||
}
|
||||
|
||||
cluster, err := ctrl.service.Create(req.Name, req.Description, req.ConfigType, req.APIServer, req.KubeConfig, req.Token, req.Context)
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(cluster))
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) Get(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
cluster, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "集群不存在")
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(cluster))
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) List(c *gin.Context) {
|
||||
page, pageSize := utils.Pagination(c)
|
||||
clusters, 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(clusters), Total: total, Page: page, PageSize: pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) 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": "更新成功"})
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) 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": "删除成功"})
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) Validate(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
go ctrl.service.Validate(uint(id))
|
||||
utils.Success(c, gin.H{"message": "已开始验证连接"})
|
||||
}
|
||||
|
||||
func (ctrl *ClusterController) GetInfo(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
cluster, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "集群不存在")
|
||||
return
|
||||
}
|
||||
info, err := services.GetClusterInfo(c.Request.Context(), cluster)
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, info)
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DeploymentController struct {
|
||||
service *services.DeploymentService
|
||||
}
|
||||
|
||||
func NewDeploymentController() *DeploymentController {
|
||||
return &DeploymentController{service: services.NewDeploymentService()}
|
||||
}
|
||||
|
||||
type CreateDeploymentRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
Version string `json:"version"`
|
||||
Mode string `json:"mode" binding:"required,oneof=k8s-helm k8s-yaml host-binary"`
|
||||
ClusterID *uint `json:"clusterID"`
|
||||
HostID *uint `json:"hostID"`
|
||||
Namespace string `json:"namespace"`
|
||||
StorageConfigID uint `json:"storageConfigID"`
|
||||
AuthEnabled bool `json:"authEnabled"`
|
||||
RetentionDays int `json:"retentionDays"`
|
||||
Replicas int `json:"replicas"`
|
||||
ResourcesCPU string `json:"resourcesCPU"`
|
||||
ResourcesMem string `json:"resourcesMem"`
|
||||
StorageSize string `json:"storageSize"`
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) Create(c *gin.Context) {
|
||||
var req CreateDeploymentRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
deployment, err := ctrl.service.Create(&services.CreateDeploymentRequest{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Version: req.Version,
|
||||
Mode: req.Mode,
|
||||
ClusterID: req.ClusterID,
|
||||
HostID: req.HostID,
|
||||
Namespace: req.Namespace,
|
||||
StorageConfigID: req.StorageConfigID,
|
||||
AuthEnabled: req.AuthEnabled,
|
||||
RetentionDays: req.RetentionDays,
|
||||
Replicas: req.Replicas,
|
||||
ResourcesCPU: req.ResourcesCPU,
|
||||
ResourcesMem: req.ResourcesMem,
|
||||
StorageSize: req.StorageSize,
|
||||
})
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, deployment)
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) Get(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
deployment, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "部署不存在")
|
||||
return
|
||||
}
|
||||
utils.Success(c, deployment)
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) List(c *gin.Context) {
|
||||
page, pageSize := utils.Pagination(c)
|
||||
deployments, total, err := ctrl.service.List(page, pageSize)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, utils.PageData{
|
||||
List: deployments, Total: total, Page: page, PageSize: pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) 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 *DeploymentController) 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 *DeploymentController) Deploy(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.StartDeploy(uint(id)); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "deployment started"})
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) Stop(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.StopDeployment(uint(id)); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "deployment stopped"})
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) Restart(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
if err := ctrl.service.RestartDeployment(uint(id)); err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"message": "deployment restarted"})
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) GetLogs(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
page, pageSize := utils.Pagination(c)
|
||||
logs, total, err := ctrl.service.GetLogs(uint(id), page, pageSize)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, utils.PageData{
|
||||
List: logs, Total: total, Page: page, PageSize: pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *DeploymentController) GetStats(c *gin.Context) {
|
||||
stats, err := ctrl.service.GetStats()
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, stats)
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/models"
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HostController struct {
|
||||
service *services.HostService
|
||||
}
|
||||
|
||||
func NewHostController() *HostController {
|
||||
return &HostController{service: services.NewHostService()}
|
||||
}
|
||||
|
||||
type CreateHostRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
HostIP string `json:"hostIP" binding:"required"`
|
||||
Port int `json:"port"`
|
||||
User string `json:"user" binding:"required"`
|
||||
AuthType string `json:"authType" binding:"required,oneof=password key"`
|
||||
Password string `json:"password"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
KeyPhrase string `json:"keyPhrase"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Labels string `json:"labels"`
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Create(c *gin.Context) {
|
||||
var req CreateHostRequest
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Create(req.Name, req.HostIP, req.Port, req.User, req.AuthType,
|
||||
req.Password, req.PrivateKey, req.KeyPhrase, req.OS, req.Arch, req.Labels)
|
||||
if err != nil {
|
||||
utils.Error(c, 400, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(host))
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Get(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "主机不存在")
|
||||
return
|
||||
}
|
||||
utils.Success(c, models.SanitizedJSON(host))
|
||||
}
|
||||
|
||||
func (ctrl *HostController) List(c *gin.Context) {
|
||||
page, pageSize := utils.Pagination(c)
|
||||
hosts, 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(hosts),
|
||||
Total: total, Page: page, PageSize: pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) 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 *HostController) 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 *HostController) Validate(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
go ctrl.service.Validate(uint(id))
|
||||
utils.Success(c, gin.H{"message": "validation started"})
|
||||
}
|
||||
|
||||
func (ctrl *HostController) GetInfo(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
host, err := ctrl.service.Get(uint(id))
|
||||
if err != nil {
|
||||
utils.NotFound(c, "主机不存在")
|
||||
return
|
||||
}
|
||||
|
||||
password := host.Password
|
||||
if len(password) > 4 && password[:4] == "ENC:" {
|
||||
password, _ = utils.Decrypt(password[4:])
|
||||
}
|
||||
|
||||
client, err := services.NewSSHClient(host.HostIP, host.Port, host.User, password)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
info, err := client.GetHostInfo(c.Request.Context())
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, info)
|
||||
}
|
||||
|
||||
func (ctrl *HostController) Execute(c *gin.Context) {
|
||||
id, err := utils.IntParam(c, "id")
|
||||
if err != nil {
|
||||
utils.BadRequest(c, "ID 无效")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Command string `json:"command" binding:"required"`
|
||||
}
|
||||
if !utils.BindAndValidate(c, &req) {
|
||||
return
|
||||
}
|
||||
stdout, stderr, err := ctrl.service.ExecuteCommand(uint(id), req.Command)
|
||||
if err != nil {
|
||||
utils.Error(c, 500, err.Error())
|
||||
return
|
||||
}
|
||||
utils.Success(c, gin.H{"stdout": stdout, "stderr": stderr})
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"lokideploy/models"
|
||||
"lokideploy/services"
|
||||
"lokideploy/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type StorageController struct {
|
||||
service *services.StorageService
|
||||
}
|
||||
|
||||
func NewStorageController() *StorageController {
|
||||
return &StorageController{service: services.NewStorageService()}
|
||||
}
|
||||
|
||||
type CreateStorageRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
StorageType string `json:"storageType" binding:"required,oneof=s3 oss minio rustfs"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Bucket string `json:"bucket" binding:"required"`
|
||||
Region string `json:"region"`
|
||||
AccessKey string `json:"accessKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
PathPrefix string `json:"pathPrefix"`
|
||||
Insecure bool `json:"insecure"`
|
||||
IsDefault bool `json:"isDefault"`
|
||||
}
|
||||
|
||||
func (ctrl *StorageController) Create(c *gin.Context) {
|
||||
var req 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))
|
||||
}
|
||||
Reference in new issue
Block a user