feat: 添加数据库资产、命令拦截器、授权资产等功能,修复GitHub Actions工作流
This commit is contained in:
+131
-28
@@ -1,7 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -10,65 +17,161 @@ import (
|
||||
type DatabaseAssetApi struct{}
|
||||
|
||||
func (api DatabaseAssetApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
dbType := c.QueryParam("type")
|
||||
items, err := repository.DatabaseAssetRepository.FindByType(context.TODO(), dbType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, items)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
name := c.QueryParam("keyword")
|
||||
dbType := c.QueryParam("type")
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.DatabaseAssetRepository.Find(context.TODO(), pageIndex, pageSize, name, dbType, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"items": []interface{}{},
|
||||
"total": 0,
|
||||
"items": items,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) CreateEndpoint(c echo.Context) error {
|
||||
var item map[string]interface{}
|
||||
if err := c.Bind(&item); err != nil {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Description string `json:"description"`
|
||||
GatewayType string `json:"gatewayType"`
|
||||
GatewayId string `json:"gatewayId"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = utils.LongUUID()
|
||||
|
||||
account, _ := GetCurrentAccount(c)
|
||||
|
||||
item := &model.DatabaseAsset{
|
||||
ID: utils.UUID(),
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Host: req.Host,
|
||||
Port: req.Port,
|
||||
Database: req.Database,
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
Description: req.Description,
|
||||
GatewayType: req.GatewayType,
|
||||
GatewayId: req.GatewayId,
|
||||
Tags: strings.Join(req.Tags, ","),
|
||||
Owner: account.ID,
|
||||
Created: common.NowJsonTime(),
|
||||
Updated: common.NowJsonTime(),
|
||||
}
|
||||
|
||||
if item.Port == 0 {
|
||||
item.Port = repository.DatabaseAssetRepository.GetDBPort(item.Type)
|
||||
}
|
||||
|
||||
if err := repository.DatabaseAssetRepository.Create(context.TODO(), item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var item map[string]interface{}
|
||||
if err := c.Bind(&item); err != nil {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Description string `json:"description"`
|
||||
GatewayType string `json:"gatewayType"`
|
||||
GatewayId string `json:"gatewayId"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = id
|
||||
return Success(c, item)
|
||||
|
||||
existing, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existing.Name = req.Name
|
||||
existing.Type = req.Type
|
||||
existing.Host = req.Host
|
||||
existing.Port = req.Port
|
||||
existing.Database = req.Database
|
||||
existing.Username = req.Username
|
||||
if req.Password != "" {
|
||||
existing.Password = req.Password
|
||||
}
|
||||
existing.Description = req.Description
|
||||
existing.GatewayType = req.GatewayType
|
||||
existing.GatewayId = req.GatewayId
|
||||
existing.Tags = strings.Join(req.Tags, ",")
|
||||
existing.Updated = common.NowJsonTime()
|
||||
|
||||
if err := repository.DatabaseAssetRepository.UpdateById(context.TODO(), &existing); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, existing)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
if err := repository.DatabaseAssetRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"name": "MySQL Server",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"database": "test_db",
|
||||
"username": "root",
|
||||
"description": "Test database",
|
||||
"status": "active",
|
||||
"statusText": "Active",
|
||||
"createdAt": 1700000000000,
|
||||
"updatedAt": 1700000000000,
|
||||
})
|
||||
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) DecryptEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"password": "decrypted_password",
|
||||
"password": item.Password,
|
||||
})
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) TagsEndpoint(c echo.Context) error {
|
||||
tags, err := repository.DatabaseAssetRepository.FindAllTags(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, tags)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user