* fix: code error * feat: opz security * feat: Optimize batch key encryption * feat: Optimize batch test key * feat: Perfect encryption key * docs: failed-to-decrypt * feat: Security alert * feat: Encryption migrate command * fix: migrate command error * feat: Adjust data length * feat: Adjust data length * docs: input text * fix: Deterministic Encryption * feat: Optimize security algorithms * feat: Support KeyHash * fix: Fix migration script field order issue * fix: Fix log export key issue * fix: code style * fix: migrate order offset * fix: Resolve migration script compatibility issues * feat: Adjust Redis cleanup policy * fix: migrate for sqlite * fix: migrate * fix: rm group_key_inx * fix: rm group_key_inx * fix: filter null hash data * docs: log warn * docs: update readme
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
app_errors "gpt-load/internal/errors"
|
|
"gpt-load/internal/models"
|
|
"gpt-load/internal/response"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// LogResponse defines the structure for log entries in the API response
|
|
type LogResponse struct {
|
|
models.RequestLog
|
|
}
|
|
|
|
// GetLogs handles fetching request logs with filtering and pagination.
|
|
func (s *Server) GetLogs(c *gin.Context) {
|
|
query := s.LogService.GetLogsQuery(c)
|
|
|
|
var logs []models.RequestLog
|
|
query = query.Order("timestamp desc")
|
|
pagination, err := response.Paginate(c, query, &logs)
|
|
if err != nil {
|
|
response.Error(c, app_errors.ParseDBError(err))
|
|
return
|
|
}
|
|
|
|
// 解密所有日志中的密钥用于前端显示
|
|
for i := range logs {
|
|
if logs[i].KeyValue != "" {
|
|
decryptedValue, err := s.EncryptionSvc.Decrypt(logs[i].KeyValue)
|
|
if err != nil {
|
|
logrus.WithError(err).WithField("log_id", logs[i].ID).Error("Failed to decrypt log key value")
|
|
logs[i].KeyValue = "failed-to-decrypt"
|
|
} else {
|
|
logs[i].KeyValue = decryptedValue
|
|
}
|
|
}
|
|
}
|
|
|
|
pagination.Items = logs
|
|
response.Success(c, pagination)
|
|
}
|
|
|
|
// ExportLogs handles exporting filtered log keys to a CSV file.
|
|
func (s *Server) ExportLogs(c *gin.Context) {
|
|
filename := fmt.Sprintf("log_keys_export_%s.csv", time.Now().Format("20060102150405"))
|
|
c.Header("Content-Disposition", "attachment; filename="+filename)
|
|
c.Header("Content-Type", "text/csv; charset=utf-8")
|
|
|
|
// Stream the response
|
|
err := s.LogService.StreamLogKeysToCSV(c, c.Writer)
|
|
if err != nil {
|
|
log.Printf("Failed to stream log keys to CSV: %v", err)
|
|
c.JSON(500, gin.H{"error": "Failed to export logs"})
|
|
return
|
|
}
|
|
}
|