迁移komari-common到common

This commit is contained in:
Akizon77
2025-04-27 17:28:39 +08:00
parent d36ea2db1d
commit 07ceb1b036
7 changed files with 139 additions and 34 deletions

View File

@@ -4,15 +4,15 @@ import (
"net/http"
"time"
"github.com/akizon77/komari/common"
"github.com/akizon77/komari/database/clients"
"github.com/akizon77/komari/database/history"
"github.com/akizon77/komari_common"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
func AddClient(c *gin.Context) {
var config komari_common.ClientConfig
var config common.ClientConfig
if err := c.ShouldBindJSON(&config); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error": err.Error()})
return
@@ -29,10 +29,10 @@ func AddClient(c *gin.Context) {
func EditClient(c *gin.Context) {
var req struct {
UUID string `json:"uuid" binding:"required"`
ClientName string `json:"client_name,omitempty"`
Token string `json:"token,omitempty"`
Config komari_common.ClientConfig `json:"config,omitempty"`
UUID string `json:"uuid" binding:"required"`
ClientName string `json:"client_name,omitempty"`
Token string `json:"token,omitempty"`
Config common.ClientConfig `json:"config,omitempty"`
}
if err := c.ShouldBindJSON(&req); err != nil {

View File

@@ -9,9 +9,9 @@ import (
"log"
"net/http"
"github.com/akizon77/komari/common"
"github.com/akizon77/komari/database/clients"
"github.com/akizon77/komari/ws"
"github.com/akizon77/komari_common"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
@@ -31,7 +31,7 @@ func UploadReport(c *gin.Context) {
return
}
// Save report to database
var report komari_common.Report
var report common.Report
err = json.Unmarshal(bodyBytes, &report)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
@@ -125,7 +125,7 @@ func WebSocketReport(c *gin.Context) {
break
}
report := komari_common.Report{}
report := common.Report{}
err = json.Unmarshal(message, &report)
if err != nil {
break

105
common/modules.go Normal file
View File

@@ -0,0 +1,105 @@
package common
import "time"
type Message struct {
Type string `json:"type"`
Content string `json:"content"`
Sender string `json:"sender"`
Timestamp int64 `json:"timestamp"`
}
type ClientConfig struct {
ClientUUID string `json:"client_uuid" gorm:"type:uuid;primaryKey;foreignKey:ClientUUID;references:UUID;constraint:OnDelete:CASCADE"`
CPU bool `json:"cpu" gorm:"default:true"`
GPU bool `json:"gpu" gorm:"default:true"`
RAM bool `json:"ram" gorm:"default:true"`
SWAP bool `json:"swap" gorm:"default:true"`
LOAD bool `json:"load" gorm:"default:true"`
UPTIME bool `json:"uptime" gorm:"default:true"`
TEMP bool `json:"temp" gorm:"default:true"`
OS bool `json:"os" gorm:"default:true"`
DISK bool `json:"disk" gorm:"default:true"`
NET bool `json:"net" gorm:"default:true"`
PROCESS bool `json:"process" gorm:"default:true"`
Connections bool `json:"connections" gorm:"default:true"`
Interval int `json:"interval" gorm:"default:3;check:Interval >= 1"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ClientInfo stores static client information
type ClientInfo struct {
ClientUUID string `gorm:"type:uuid;primaryKey;foreignKey:ClientUUID;references:UUID;constraint:OnDelete:CASCADE"`
CPUNAME string `gorm:"type:varchar(100)"`
CPUARCH string `gorm:"type:varchar(50)"`
CPUCORES int
OS string `gorm:"type:varchar(100)"`
GPUNAME string `gorm:"type:varchar(100)"`
IPv4 string `gorm:"type:varchar(100)"`
IPv6 string `gorm:"type:varchar(100)"`
CreatedAt time.Time
UpdatedAt time.Time
}
type IPAddress struct {
Ipv4 string `json:"ipv4"`
Ipv6 string `json:"ipv6"`
}
type Report struct {
UUID string `json:"uuid,omitempty"`
Token string `json:"token,omitempty"`
CPU CPUReport `json:"cpu"`
OS string `json:"os"`
Ram RamReport `json:"ram"`
Swap RamReport `json:"swap"`
Load LoadReport `json:"load"`
Disk DiskReport `json:"disk"`
Network NetworkReport `json:"network"`
Connections ConnectionsReport `json:"connections"`
Uptime int64 `json:"uptime"`
Process int `json:"process"`
Message string `json:"message"`
Method string `json:"method"`
UpdatedAt time.Time `json:"updated_at"`
}
type CPUReport struct {
Name string `json:"name"`
Cores int `json:"cores"`
Arch string `json:"arch"`
Usage float64 `json:"usage"`
}
type GPUReport struct {
Name string `json:"name"`
Usage float64 `json:"usage"`
}
type RamReport struct {
Total int64 `json:"total"`
Used int64 `json:"used"`
}
type LoadReport struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}
type DiskReport struct {
Total int64 `json:"total"`
Used int64 `json:"used"`
}
type NetworkReport struct {
Up int64 `json:"up"`
Down int64 `json:"down"`
TotalUp int64 `json:"totalUp"`
TotalDown int64 `json:"totalDown"`
}
type ConnectionsReport struct {
TCP int `json:"tcp"`
UDP int `json:"udp"`
}

View File

@@ -3,10 +3,10 @@ package clients
import (
"time"
"github.com/akizon77/komari/common"
"github.com/akizon77/komari/database/dbcore"
"github.com/akizon77/komari/database/models"
"github.com/akizon77/komari/utils"
"github.com/akizon77/komari_common"
"github.com/google/uuid"
"gorm.io/gorm"
@@ -15,7 +15,7 @@ import (
// 删除指定 UUID 的客户端配置
func DeleteClientConfig(clientUuid string) error {
db := dbcore.GetDBInstance()
err := db.Delete(&komari_common.ClientConfig{ClientUUID: clientUuid}).Error
err := db.Delete(&common.ClientConfig{ClientUUID: clientUuid}).Error
if err != nil {
return err
}
@@ -33,7 +33,7 @@ func UpdateOrInsertBasicInfo(cbi ClientBasicInfo) error {
}
// 更新客户端设置
func UpdateClientConfig(config komari_common.ClientConfig) error {
func UpdateClientConfig(config common.ClientConfig) error {
db := dbcore.GetDBInstance()
err := db.Save(&config).Error
if err != nil {
@@ -52,9 +52,9 @@ func EditClientName(clientUUID, clientName string) error {
}
// UpdateClientByUUID 更新指定 UUID 的客户端配置
func UpdateClientByUUID(config komari_common.ClientConfig) error {
func UpdateClientByUUID(config common.ClientConfig) error {
db := dbcore.GetDBInstance()
result := db.Model(&komari_common.ClientConfig{}).Where("client_uuid = ?", config.ClientUUID).Updates(config)
result := db.Model(&common.ClientConfig{}).Where("client_uuid = ?", config.ClientUUID).Updates(config)
if result.Error != nil {
return result.Error
}
@@ -74,7 +74,7 @@ func EditClientToken(clientUUID, token string) error {
}
// CreateClient 创建新客户端
func CreateClient(config komari_common.ClientConfig) (clientUUID, token string, err error) {
func CreateClient(config common.ClientConfig) (clientUUID, token string, err error) {
db := dbcore.GetDBInstance()
token = utils.GenerateToken()
clientUUID = uuid.New().String()
@@ -119,39 +119,39 @@ func GetClientByUUID(uuid string) (client models.Client, err error) {
}
// GetClientConfig 获取指定 UUID 的客户端配置
func GetClientConfig(uuid string) (client komari_common.ClientConfig, err error) {
func GetClientConfig(uuid string) (client common.ClientConfig, err error) {
db := dbcore.GetDBInstance()
err = db.Where("client_uuid = ?", uuid).First(&client).Error
if err != nil {
return komari_common.ClientConfig{}, err
return common.ClientConfig{}, err
}
return client, nil
}
// ClientBasicInfo 客户端基本信息(假设的结构体,需根据实际定义调整)
type ClientBasicInfo struct {
CPU komari_common.CPUReport `json:"cpu"`
GPU komari_common.GPUReport `json:"gpu"`
IpAddress komari_common.IPAddress `json:"ip"`
OS string `json:"os"`
CPU common.CPUReport `json:"cpu"`
GPU common.GPUReport `json:"gpu"`
IpAddress common.IPAddress `json:"ip"`
OS string `json:"os"`
}
// GetClientBasicInfo 获取指定 UUID 的客户端基本信息
func GetClientBasicInfo(uuid string) (client ClientBasicInfo, err error) {
db := dbcore.GetDBInstance()
var clientInfo komari_common.ClientInfo
var clientInfo common.ClientInfo
err = db.Where("client_uuid = ?", uuid).First(&clientInfo).Error
if err != nil {
return client, err
}
client = ClientBasicInfo{
CPU: komari_common.CPUReport{
CPU: common.CPUReport{
Name: clientInfo.CPUNAME,
Arch: clientInfo.CPUARCH,
Cores: clientInfo.CPUCORES,
},
GPU: komari_common.GPUReport{
GPU: common.GPUReport{
Name: clientInfo.GPUNAME,
},
OS: clientInfo.OS,

View File

@@ -4,9 +4,9 @@ import (
"encoding/json"
"fmt"
"github.com/akizon77/komari/common"
"github.com/akizon77/komari/database/dbcore"
"github.com/akizon77/komari/database/models"
"github.com/akizon77/komari_common"
"gorm.io/gorm"
)
@@ -41,20 +41,20 @@ func GetClientUUIDByToken(token string) (clientUUID string, err error) {
return clientUUID, nil
}
func ParseReport(data map[string]interface{}) (report komari_common.Report, err error) {
func ParseReport(data map[string]interface{}) (report common.Report, err error) {
jsonData, err := json.Marshal(data)
if err != nil {
return komari_common.Report{}, err
return common.Report{}, err
}
err = json.Unmarshal(jsonData, &report)
if err != nil {
return komari_common.Report{}, err
return common.Report{}, err
}
return report, nil
}
// SaveClientReport 保存客户端报告到 History 表
func SaveClientReport(clientUUID string, report komari_common.Report) (err error) {
func SaveClientReport(clientUUID string, report common.Report) (err error) {
db := dbcore.GetDBInstance()
history := models.History{

View File

@@ -7,8 +7,8 @@ import (
"sync"
"github.com/akizon77/komari/cmd/flags"
"github.com/akizon77/komari/common"
"github.com/akizon77/komari/database/models"
"github.com/akizon77/komari_common"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
@@ -52,12 +52,12 @@ func GetDBInstance() *gorm.DB {
log.Fatalf("Failed to connect to SQLite3 database: %v", err)
}
err = instance.AutoMigrate(
&komari_common.ClientConfig{},
&common.ClientConfig{},
&models.User{},
&models.Client{},
&models.Session{},
&models.History{},
&komari_common.ClientInfo{},
&common.ClientInfo{},
&models.Config{},
&models.Custom{},
)

4
go.mod
View File

@@ -9,10 +9,10 @@ require (
github.com/spf13/cobra v1.9.1
gorm.io/driver/sqlite v1.5.7
gorm.io/gorm v1.25.12
github.com/akizon77/komari_common v0.0.0
github.com/akizon77/common v0.0.0
)
replace github.com/akizon77/komari_common => ../komari_common
replace github.com/akizon77/common => ../common
require (
github.com/bytedance/sonic v1.11.6 // indirect