mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 06:16:37 +08:00
ac7b776378
-transition `of_config_versions` primary key from `id` to `version` string. -add database migration files `202606270001_make_version_primary_key.sql` for PostgreSQL and SQLite. -introduce AfterFind/AfterCreate GORM hooks to preserve JSON backward compatibility. -refactor API controllers, logics, and front-end typescript definitions to receive `string` parameter.
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
openrestyrender "github.com/Rain-kl/Wavelet/pkg/render/openresty"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func getActiveConfigMeta(ctx context.Context) (*ActiveConfigMeta, error) {
|
|
version, err := model.GetActiveConfigVersion(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ActiveConfigMeta{
|
|
Version: version.Version,
|
|
Checksum: version.Checksum,
|
|
}, nil
|
|
}
|
|
|
|
func getActiveConfigForAgent(ctx context.Context) (*ConfigResponse, error) {
|
|
version, err := model.GetActiveConfigVersion(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var supportFiles []SupportFile
|
|
if strings.TrimSpace(version.SupportFilesJSON) != "" {
|
|
if err = json.Unmarshal([]byte(version.SupportFilesJSON), &supportFiles); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &ConfigResponse{
|
|
Version: version.Version,
|
|
Checksum: version.Checksum,
|
|
SourceConfigJSON: version.SnapshotJSON,
|
|
SupportFiles: sourceSupportFiles(supportFiles),
|
|
CreatedAt: version.CreatedAt,
|
|
}, nil
|
|
}
|
|
|
|
func sourceSupportFiles(files []SupportFile) []SupportFile {
|
|
if len(files) == 0 {
|
|
return nil
|
|
}
|
|
result := make([]SupportFile, 0, len(files))
|
|
for _, file := range files {
|
|
if isRuntimeGeneratedSupportFile(file.Path) {
|
|
continue
|
|
}
|
|
result = append(result, file)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func isRuntimeGeneratedSupportFile(path string) bool {
|
|
switch strings.TrimSpace(path) {
|
|
case "pow_config.json", "waf_config.json", openrestyrender.SourceConfigFileName: // pow_config.json is legacy; waf_config.json is canonical
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func isActiveConfigNotFound(err error) bool {
|
|
return errors.Is(err, gorm.ErrRecordNotFound)
|
|
}
|