refactor(repo): consolidate openflare-server to root and move subprojects to internal/apps

- Merge all files inside openflare-server to the repository root directory.
- Relocate agent, relay, and flared subprojects from internal/ to internal/apps/.
- Combine docker-compose files and update build context paths to root.
- Update GitHub workflows and Dockerfiles to refer to new directories and package names.
- Rewrite Go package imports across all files.
- Resolve database renew test race condition and clean up docs.
This commit is contained in:
ryan
2026-06-19 14:23:29 +08:00
parent 19d476ed7f
commit 63cd906cfc
1064 changed files with 366 additions and 1397 deletions
@@ -0,0 +1,8 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package apply_log
const (
errRetentionDaysOutOfRange = "retention_days 必须在 1 到 3650 之间"
)
+128
View File
@@ -0,0 +1,128 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package apply_log
import (
"context"
"errors"
"strings"
"time"
"github.com/Rain-kl/Wavelet/internal/model"
)
const (
defaultApplyLogPageSize = 20
maxApplyLogPageSize = 200
maxApplyLogRetentionDays = 3650
)
// ListQuery filters apply logs for paginated listing.
type ListQuery struct {
NodeID string `json:"node_id"`
PageNo int `json:"pageNo"`
PageSize int `json:"pageSize"`
}
// ListResult is the paginated apply log list response.
type ListResult struct {
Rows []*model.OpenFlareApplyLog `json:"rows"`
Current int `json:"current"`
Total int `json:"total"`
TotalPage int `json:"totalPage"`
}
// CleanupInput controls apply log cleanup behavior.
type CleanupInput struct {
DeleteAll bool `json:"delete_all"`
RetentionDays int `json:"retention_days"`
}
// CleanupResult reports apply log cleanup outcome.
type CleanupResult struct {
DeleteAll bool `json:"delete_all"`
RetentionDays int `json:"retention_days"`
DeletedCount int64 `json:"deleted_count"`
Cutoff *time.Time `json:"cutoff,omitempty"`
}
// ListPage returns paginated apply logs with optional node_id filter.
func ListPage(ctx context.Context, input ListQuery) (*ListResult, error) {
pageNo := normalizePageNo(input.PageNo)
pageSize := normalizePageSize(input.PageSize)
nodeID := strings.TrimSpace(input.NodeID)
rows, err := model.ListOpenFlareApplyLogs(ctx, model.OpenFlareApplyLogQuery{
NodeID: nodeID,
PageNo: pageNo,
PageSize: pageSize,
})
if err != nil {
return nil, err
}
total, err := model.CountOpenFlareApplyLogs(ctx, nodeID)
if err != nil {
return nil, err
}
totalPage := 0
if total > 0 {
totalPage = int((total + int64(pageSize) - 1) / int64(pageSize))
}
return &ListResult{
Rows: rows,
Current: pageNo,
Total: int(total),
TotalPage: totalPage,
}, nil
}
// Cleanup removes old apply logs or deletes all records.
func Cleanup(ctx context.Context, input CleanupInput) (*CleanupResult, error) {
if input.DeleteAll {
deleted, err := model.DeleteAllOpenFlareApplyLogs(ctx)
if err != nil {
return nil, err
}
return &CleanupResult{
DeleteAll: true,
DeletedCount: deleted,
}, nil
}
if input.RetentionDays <= 0 || input.RetentionDays > maxApplyLogRetentionDays {
return nil, errors.New(errRetentionDaysOutOfRange)
}
cutoff := time.Now().UTC().Add(-time.Duration(input.RetentionDays) * 24 * time.Hour)
deleted, err := model.DeleteOpenFlareApplyLogsBefore(ctx, cutoff)
if err != nil {
return nil, err
}
return &CleanupResult{
RetentionDays: input.RetentionDays,
DeletedCount: deleted,
Cutoff: &cutoff,
}, nil
}
func normalizePageNo(pageNo int) int {
if pageNo <= 0 {
return 1
}
return pageNo
}
func normalizePageSize(pageSize int) int {
if pageSize <= 0 {
return defaultApplyLogPageSize
}
if pageSize > maxApplyLogPageSize {
return maxApplyLogPageSize
}
return pageSize
}
@@ -0,0 +1,105 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package apply_log
import (
"context"
"testing"
"time"
"github.com/Rain-kl/Wavelet/internal/db"
"github.com/Rain-kl/Wavelet/internal/model"
"github.com/glebarez/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)
func setupApplyLogTestDB(t *testing.T) func() {
sqliteDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
require.NoError(t, err)
err = sqliteDB.AutoMigrate(&model.OpenFlareApplyLog{})
require.NoError(t, err)
db.SetDB(sqliteDB)
return func() {
db.SetDB(nil)
}
}
func TestListPageAndCleanup(t *testing.T) {
cleanup := setupApplyLogTestDB(t)
defer cleanup()
ctx := context.Background()
now := time.Now().UTC()
logs := []model.OpenFlareApplyLog{
{NodeID: "node-logs", Version: "v1", Result: "success", Message: "1", CreatedAt: now.Add(-10 * 24 * time.Hour)},
{NodeID: "node-logs", Version: "v2", Result: "success", Message: "2", CreatedAt: now.Add(-5 * 24 * time.Hour)},
{NodeID: "node-logs", Version: "v3", Result: "success", Message: "3", CreatedAt: now},
}
for i := range logs {
require.NoError(t, db.DB(ctx).Create(&logs[i]).Error)
}
pageResult, err := ListPage(ctx, ListQuery{
NodeID: "node-logs",
PageNo: 1,
PageSize: 2,
})
require.NoError(t, err)
assert.Equal(t, 3, pageResult.Total)
assert.Len(t, pageResult.Rows, 2)
assert.Equal(t, 2, pageResult.TotalPage)
assert.Equal(t, 1, pageResult.Current)
cleanupResult, err := Cleanup(ctx, CleanupInput{
DeleteAll: false,
RetentionDays: 7,
})
require.NoError(t, err)
assert.Equal(t, int64(1), cleanupResult.DeletedCount)
assert.NotNil(t, cleanupResult.Cutoff)
remaining, err := model.ListOpenFlareApplyLogs(ctx, model.OpenFlareApplyLogQuery{
NodeID: "node-logs",
PageNo: 1,
PageSize: 10,
})
require.NoError(t, err)
assert.Len(t, remaining, 2)
cleanupAll, err := Cleanup(ctx, CleanupInput{DeleteAll: true})
require.NoError(t, err)
assert.Equal(t, int64(2), cleanupAll.DeletedCount)
assert.True(t, cleanupAll.DeleteAll)
finalLogs, err := model.ListOpenFlareApplyLogs(ctx, model.OpenFlareApplyLogQuery{
NodeID: "node-logs",
PageNo: 1,
PageSize: 10,
})
require.NoError(t, err)
assert.Empty(t, finalLogs)
}
func TestCleanupInvalidRetentionDays(t *testing.T) {
cleanup := setupApplyLogTestDB(t)
defer cleanup()
ctx := context.Background()
_, err := Cleanup(ctx, CleanupInput{RetentionDays: 0})
require.Error(t, err)
assert.Equal(t, errRetentionDaysOutOfRange, err.Error())
_, err = Cleanup(ctx, CleanupInput{RetentionDays: 4000})
require.Error(t, err)
assert.Equal(t, errRetentionDaysOutOfRange, err.Error())
}
@@ -0,0 +1,77 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
package apply_log
import (
"net/http"
"strconv"
"github.com/Rain-kl/Wavelet/internal/apps/openflare/apiutil"
"github.com/Rain-kl/Wavelet/internal/common/response"
"github.com/gin-gonic/gin"
)
// GetApplyLogs lists apply logs with pagination and optional node_id filter.
// @Summary 获取配置下发日志
// @Description 分页返回节点配置下发记录,支持按节点 ID 筛选,需要管理员权限
// @Tags openflare-apply-log
// @Produce json
// @Security SessionCookie
// @Param node_id query string false "节点 ID 筛选"
// @Param pageNo query int false "页码"
// @Param page_no query int false "页码(别名)"
// @Param pageSize query int false "每页数量"
// @Param page_size query int false "每页数量(别名)"
// @Success 200 {object} response.Any{data=apply_log.ListResult} "下发日志列表"
// @Failure 400 {object} response.Any "参数错误"
// @Failure 401 {object} response.Any "未登录"
// @Failure 404 {object} response.Any "无权限或不存在"
// @Router /api/v1/d/apply-logs [get]
func GetApplyLogs(c *gin.Context) {
result, err := ListPage(c.Request.Context(), ListQuery{
NodeID: c.Query("node_id"),
PageNo: readIntQuery(c, "pageNo", "page_no"),
PageSize: readIntQuery(c, "pageSize", "page_size"),
})
if apiutil.AbortBadRequestOnError(c, err) {
return
}
c.JSON(http.StatusOK, response.OK(result))
}
// CleanupApplyLogs removes old apply logs or deletes all records.
// @Summary 清理配置下发日志
// @Description 按保留天数清理历史下发记录,或删除全部记录,需要管理员权限
// @Tags openflare-apply-log
// @Accept json
// @Produce json
// @Security SessionCookie
// @Param body body apply_log.CleanupInput true "清理参数"
// @Success 200 {object} response.Any{data=apply_log.CleanupResult} "清理结果"
// @Failure 400 {object} response.Any "参数错误"
// @Failure 401 {object} response.Any "未登录"
// @Failure 404 {object} response.Any "无权限或不存在"
// @Router /api/v1/d/apply-logs/cleanup [post]
func CleanupApplyLogs(c *gin.Context) {
var input CleanupInput
if !apiutil.BindJSON(c, &input) {
return
}
result, err := Cleanup(c.Request.Context(), input)
if apiutil.AbortBadRequestOnError(c, err) {
return
}
c.JSON(http.StatusOK, response.OK(result))
}
func readIntQuery(c *gin.Context, primary, secondary string) int {
value := c.Query(primary)
if value == "" {
value = c.Query(secondary)
}
parsed, _ := strconv.Atoi(value)
return parsed
}