优化结构
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
package adminauth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrTokenExpired = errors.New("token expired")
|
||||
ErrTokenMalformed = errors.New("token malformed")
|
||||
ErrTokenSignatureInvalid = errors.New("token signature invalid")
|
||||
ErrTokenNotValidYet = errors.New("token not valid yet")
|
||||
ErrTokenInvalid = errors.New("token invalid")
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
UUID string
|
||||
ID uint
|
||||
Username string
|
||||
NickName string
|
||||
AuthorityID uint `json:"AuthorityId"`
|
||||
BufferTime int64
|
||||
UserType string
|
||||
MustChangePwd bool `json:"mustChangePwd"`
|
||||
PasswordVersion int64 `json:"passwordVersion,omitempty"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func Generate(secret, issuer string, expires, buffer time.Duration, userID, authorityID uint, uuid, username, nickname string, mustChange bool, passwordVersion int64) (string, *Claims, error) {
|
||||
if secret == "" {
|
||||
return "", nil, errors.New("empty JWT signing key")
|
||||
}
|
||||
now := time.Now()
|
||||
claims := &Claims{UUID: uuid, ID: userID, Username: username, NickName: nickname, AuthorityID: authorityID, BufferTime: int64(buffer / time.Second), UserType: "admin", MustChangePwd: mustChange, PasswordVersion: passwordVersion, RegisteredClaims: jwt.RegisteredClaims{Audience: jwt.ClaimStrings{"KRA"}, Issuer: issuer, IssuedAt: jwt.NewNumericDate(now), NotBefore: jwt.NewNumericDate(now.Add(-1000)), ExpiresAt: jwt.NewNumericDate(now.Add(expires))}}
|
||||
token, err := Sign(secret, claims)
|
||||
return token, claims, err
|
||||
}
|
||||
|
||||
func Sign(secret string, claims *Claims) (string, error) {
|
||||
if secret == "" {
|
||||
return "", errors.New("empty JWT signing key")
|
||||
}
|
||||
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
func Parse(tokenString, secret string) (*Claims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (any, error) {
|
||||
if token.Method != jwt.SigningMethodHS256 {
|
||||
return nil, errors.New("unexpected signing method")
|
||||
}
|
||||
return []byte(secret), nil
|
||||
})
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, jwt.ErrTokenExpired):
|
||||
return nil, ErrTokenExpired
|
||||
case errors.Is(err, jwt.ErrTokenMalformed):
|
||||
return nil, ErrTokenMalformed
|
||||
case errors.Is(err, jwt.ErrTokenSignatureInvalid):
|
||||
return nil, ErrTokenSignatureInvalid
|
||||
case errors.Is(err, jwt.ErrTokenNotValidYet):
|
||||
return nil, ErrTokenNotValidYet
|
||||
default:
|
||||
return nil, ErrTokenInvalid
|
||||
}
|
||||
}
|
||||
if !token.Valid {
|
||||
return nil, ErrTokenInvalid
|
||||
}
|
||||
claims, ok := token.Claims.(*Claims)
|
||||
if !ok {
|
||||
return nil, errors.New("token claims are invalid")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
// Package gormkit contains reusable GORM integration helpers.
|
||||
package gormkit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
// JSON is a portable JSON value for GORM models. It selects a native JSON
|
||||
// column where the driver supports one and a textual fallback elsewhere.
|
||||
type JSON []byte
|
||||
|
||||
func (JSON) GormDataType() string { return "json" }
|
||||
func (JSON) GormDBDataType(db *gorm.DB, _ *schema.Field) string {
|
||||
switch db.Dialector.Name() {
|
||||
case "mysql", "sqlite":
|
||||
return "JSON"
|
||||
case "postgres":
|
||||
return "JSONB"
|
||||
case "sqlserver":
|
||||
return "NVARCHAR(MAX)"
|
||||
case "oracle":
|
||||
return "CLOB"
|
||||
default:
|
||||
return "TEXT"
|
||||
}
|
||||
}
|
||||
func (j JSON) Value() (driver.Value, error) {
|
||||
if len(j) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return string(j), nil
|
||||
}
|
||||
func (j *JSON) Scan(value any) error {
|
||||
switch raw := value.(type) {
|
||||
case nil:
|
||||
*j = nil
|
||||
case []byte:
|
||||
*j = append((*j)[:0], raw...)
|
||||
case string:
|
||||
*j = append((*j)[:0], raw...)
|
||||
default:
|
||||
return fmt.Errorf("cannot scan JSON from %T", value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Logger forwards GORM diagnostics through an application slog logger.
|
||||
type Logger struct {
|
||||
logger *slog.Logger
|
||||
slowThreshold time.Duration
|
||||
level logger.LogLevel
|
||||
}
|
||||
|
||||
func NewLogger(log *slog.Logger, level logger.LogLevel) *Logger {
|
||||
if log == nil {
|
||||
log = slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
return &Logger{logger: log, slowThreshold: 200 * time.Millisecond, level: level}
|
||||
}
|
||||
|
||||
func (g *Logger) LogMode(level logger.LogLevel) logger.Interface {
|
||||
next := *g
|
||||
next.level = level
|
||||
return &next
|
||||
}
|
||||
|
||||
func (g *Logger) Info(ctx context.Context, message string, args ...any) {
|
||||
g.logger.InfoContext(ctx, fmt.Sprintf(message, args...), "mod", "sql", "gorm_logger", true)
|
||||
}
|
||||
|
||||
func (g *Logger) Warn(ctx context.Context, message string, args ...any) {
|
||||
g.logger.WarnContext(ctx, fmt.Sprintf(message, args...), "mod", "sql", "gorm_logger", true)
|
||||
}
|
||||
|
||||
func (g *Logger) Error(ctx context.Context, message string, args ...any) {
|
||||
g.logger.ErrorContext(ctx, fmt.Sprintf(message, args...), "mod", "sql", "gorm_logger", true)
|
||||
}
|
||||
|
||||
func (g *Logger) Trace(ctx context.Context, begin time.Time, query func() (string, int64), queryErr error) {
|
||||
if g.level <= logger.Silent {
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(begin)
|
||||
sql, rows := query()
|
||||
fields := []any{"mod", "sql", "gorm_logger", true, "sql", sql, "rows", rows, "elapsed_ms", elapsed.Milliseconds()}
|
||||
switch {
|
||||
case queryErr != nil && g.level >= logger.Error && !errors.Is(queryErr, logger.ErrRecordNotFound):
|
||||
fields = append(fields, "error", queryErr)
|
||||
g.logger.ErrorContext(ctx, "SQL 执行错误", fields...)
|
||||
case elapsed > g.slowThreshold && g.level >= logger.Warn:
|
||||
g.logger.WarnContext(ctx, "SQL 慢查询", fields...)
|
||||
case g.level >= logger.Info:
|
||||
g.logger.InfoContext(ctx, "SQL", fields...)
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type contextFieldsKey struct{}
|
||||
|
||||
// ContextFields contains request metadata attached to logs emitted with a
|
||||
// request context.
|
||||
type ContextFields struct {
|
||||
RequestID string
|
||||
TraceID string
|
||||
SpanID string
|
||||
ParentSpanID string
|
||||
DeviceID string
|
||||
ClientIP string
|
||||
HTTPMethod string
|
||||
HTTPPath string
|
||||
}
|
||||
|
||||
func WithContextFields(ctx context.Context, fields *ContextFields) context.Context {
|
||||
return context.WithValue(ctx, contextFieldsKey{}, fields)
|
||||
}
|
||||
|
||||
func ContextFieldsFrom(ctx context.Context) *ContextFields {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
fields, _ := ctx.Value(contextFieldsKey{}).(*ContextFields)
|
||||
return fields
|
||||
}
|
||||
|
||||
type contextHandler struct{ handler slog.Handler }
|
||||
|
||||
func (h *contextHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
return h.handler.Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h *contextHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
if fields := ContextFieldsFrom(ctx); fields != nil {
|
||||
record.AddAttrs(
|
||||
slog.String("request_id", fields.RequestID),
|
||||
slog.String("trace_id", fields.TraceID),
|
||||
slog.String("device_id", fields.DeviceID),
|
||||
slog.String("client_ip", fields.ClientIP),
|
||||
slog.String("http_method", fields.HTTPMethod),
|
||||
slog.String("http_path", fields.HTTPPath),
|
||||
)
|
||||
if fields.SpanID != "" {
|
||||
record.AddAttrs(slog.String("span_id", fields.SpanID))
|
||||
}
|
||||
if fields.ParentSpanID != "" {
|
||||
record.AddAttrs(slog.String("parent_span_id", fields.ParentSpanID))
|
||||
}
|
||||
}
|
||||
return h.handler.Handle(ctx, record)
|
||||
}
|
||||
|
||||
func (h *contextHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return &contextHandler{handler: h.handler.WithAttrs(attrs)}
|
||||
}
|
||||
|
||||
func (h *contextHandler) WithGroup(name string) slog.Handler {
|
||||
return &contextHandler{handler: h.handler.WithGroup(name)}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DailyWriter writes log entries to <root>/<yyyy-mm-dd>/<name>. It keeps the
|
||||
// file handle open for the active day and rotates on the first write after
|
||||
// midnight, matching the directory layout consumed by the log viewer.
|
||||
type DailyWriter struct {
|
||||
mu sync.Mutex
|
||||
root string
|
||||
name string
|
||||
date string
|
||||
file *os.File
|
||||
retentionDay int
|
||||
}
|
||||
|
||||
func NewDailyWriter(root, name string, retentionDay int) *DailyWriter {
|
||||
w := &DailyWriter{root: root, name: name, retentionDay: retentionDay}
|
||||
w.removeExpired()
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *DailyWriter) removeExpired() {
|
||||
if w.retentionDay <= 0 {
|
||||
return
|
||||
}
|
||||
entries, err := os.ReadDir(w.root)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cutoff := time.Now().AddDate(0, 0, -w.retentionDay)
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
date, err := time.Parse("2006-01-02", entry.Name())
|
||||
if err == nil && date.Before(cutoff) {
|
||||
_ = os.RemoveAll(filepath.Join(w.root, entry.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Write(value []byte) (int, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
date := time.Now().Format("2006-01-02")
|
||||
if w.file == nil || w.date != date {
|
||||
if w.file != nil {
|
||||
_ = w.file.Close()
|
||||
}
|
||||
filePath := filepath.Join(w.root, date, filepath.Clean(w.name))
|
||||
directory := filepath.Dir(filePath)
|
||||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
w.file, w.date = file, date
|
||||
}
|
||||
return w.file.Write(value)
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Sync() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.file == nil {
|
||||
return nil
|
||||
}
|
||||
return w.file.Sync()
|
||||
}
|
||||
|
||||
func (w *DailyWriter) Close() error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.file == nil {
|
||||
return nil
|
||||
}
|
||||
err := w.file.Close()
|
||||
w.file = nil
|
||||
return err
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type stackFrame struct {
|
||||
File string
|
||||
Line int
|
||||
Func string
|
||||
}
|
||||
|
||||
var stackFileLinePattern = regexp.MustCompile(`\s*(.+\.go):(\d+)\s*$`)
|
||||
|
||||
func finalApplicationCaller(stack string) (stackFrame, bool) {
|
||||
if stack == "" {
|
||||
return stackFrame{}, false
|
||||
}
|
||||
functionName := ""
|
||||
for _, raw := range strings.Split(stack, "\n") {
|
||||
line := strings.TrimSpace(raw)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
matches := stackFileLinePattern.FindStringSubmatch(line)
|
||||
if matches == nil {
|
||||
functionName = line
|
||||
continue
|
||||
}
|
||||
lineNumber, _ := strconv.Atoi(matches[2])
|
||||
if skipStackFile(matches[1]) {
|
||||
functionName = ""
|
||||
continue
|
||||
}
|
||||
return stackFrame{File: matches[1], Line: lineNumber, Func: functionName}, true
|
||||
}
|
||||
return stackFrame{}, false
|
||||
}
|
||||
|
||||
func skipStackFile(filename string) bool {
|
||||
normalized := strings.ReplaceAll(filename, "\\", "/")
|
||||
for _, marker := range []string{
|
||||
"/go/pkg/mod/",
|
||||
"/go.uber.org/",
|
||||
"/gorm.io/",
|
||||
"/pkg/logging/",
|
||||
"/internal/server/middleware/",
|
||||
"/internal/server/router/",
|
||||
} {
|
||||
if strings.Contains(normalized, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return strings.Contains(normalized, "/src/") &&
|
||||
(strings.Contains(normalized, "/go/go") || strings.Contains(normalized, "/go/src/"))
|
||||
}
|
||||
|
||||
func functionSourceAt(filename string, line int) (name, source string, startLine, endLine int, err error) {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", "", 0, 0, fmt.Errorf("read file failed: %w", err)
|
||||
}
|
||||
files := token.NewFileSet()
|
||||
parsed, err := parser.ParseFile(files, filename, content, parser.ParseComments)
|
||||
if err != nil {
|
||||
return "", "", 0, 0, fmt.Errorf("parse file failed: %w", err)
|
||||
}
|
||||
var target *ast.FuncDecl
|
||||
ast.Inspect(parsed, func(node ast.Node) bool {
|
||||
declaration, ok := node.(*ast.FuncDecl)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
start := files.Position(declaration.Pos()).Line
|
||||
end := files.Position(declaration.End()).Line
|
||||
if line >= start && line <= end {
|
||||
target, startLine, endLine = declaration, start, end
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
if target == nil {
|
||||
return "", "", 0, 0, fmt.Errorf("no function encloses line %d in %s", line, filename)
|
||||
}
|
||||
start := files.Position(target.Pos()).Offset
|
||||
end := files.Position(target.End()).Offset
|
||||
if start < 0 || end > len(content) || start >= end {
|
||||
return "", "", 0, 0, fmt.Errorf("invalid offsets for function: start=%d end=%d len=%d", start, end, len(content))
|
||||
}
|
||||
return target.Name.Name, string(content[start:end]), startLine, endLine, nil
|
||||
}
|
||||
@@ -1,469 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/contrib/otel/v3/tracing"
|
||||
kratoslog "github.com/go-kratos/kratos/v3/log"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/exp/zapslog"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Level, Format, EncodeLevel, Prefix, StacktraceKey string
|
||||
LogInConsole, ShowLine bool
|
||||
RetentionDay int
|
||||
FileOnlyModules []string
|
||||
}
|
||||
|
||||
// ErrorEntry is the storage-neutral representation of an Error-level log.
|
||||
// Keeping it in pkg/logging lets the log core report failures without taking a
|
||||
// dependency on the application service or persistence layers.
|
||||
type ErrorEntry struct {
|
||||
Form, Info, Level, RequestID, TraceID string
|
||||
}
|
||||
|
||||
// ErrorSink receives Error-level log entries. The sink must not log failures
|
||||
// through the same logger, otherwise a storage failure could recurse forever.
|
||||
type ErrorSink interface {
|
||||
RecordLogError(context.Context, ErrorEntry) error
|
||||
}
|
||||
|
||||
type ErrorSinkFunc func(context.Context, ErrorEntry) error
|
||||
|
||||
func (f ErrorSinkFunc) RecordLogError(ctx context.Context, entry ErrorEntry) error {
|
||||
return f(ctx, entry)
|
||||
}
|
||||
|
||||
type errorSinkState struct {
|
||||
mu sync.RWMutex
|
||||
sink ErrorSink
|
||||
}
|
||||
|
||||
func (s *errorSinkState) record(entry ErrorEntry) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
s.mu.RLock()
|
||||
sink := s.sink
|
||||
s.mu.RUnlock()
|
||||
if sink != nil {
|
||||
_ = sink.RecordLogError(context.Background(), entry)
|
||||
}
|
||||
}
|
||||
|
||||
type handlerOperation struct {
|
||||
attrs []slog.Attr
|
||||
group string
|
||||
}
|
||||
|
||||
type reloadableHandlerState struct {
|
||||
mu sync.RWMutex
|
||||
handler slog.Handler
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
type reloadableHandler struct {
|
||||
state *reloadableHandlerState
|
||||
ops []handlerOperation
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) resolved() slog.Handler {
|
||||
current := h.state.handler
|
||||
for _, operation := range h.ops {
|
||||
if operation.group != "" {
|
||||
current = current.WithGroup(operation.group)
|
||||
} else {
|
||||
current = current.WithAttrs(operation.attrs)
|
||||
}
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
h.state.mu.RLock()
|
||||
defer h.state.mu.RUnlock()
|
||||
return h.resolved().Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||
h.state.mu.RLock()
|
||||
defer h.state.mu.RUnlock()
|
||||
return h.resolved().Handle(ctx, record)
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
next := append([]handlerOperation(nil), h.ops...)
|
||||
next = append(next, handlerOperation{attrs: append([]slog.Attr(nil), attrs...)})
|
||||
return &reloadableHandler{state: h.state, ops: next}
|
||||
}
|
||||
|
||||
func (h *reloadableHandler) WithGroup(name string) slog.Handler {
|
||||
next := append([]handlerOperation(nil), h.ops...)
|
||||
next = append(next, handlerOperation{group: name})
|
||||
return &reloadableHandler{state: h.state, ops: next}
|
||||
}
|
||||
|
||||
type ReloadableLogger struct {
|
||||
state *reloadableHandlerState
|
||||
filename string
|
||||
errorSink *errorSinkState
|
||||
}
|
||||
|
||||
func (l *ReloadableLogger) Reload(root string, options Options) {
|
||||
handler, cleanup := newZapHandler(root, l.filename, options, l.errorSink)
|
||||
l.state.mu.Lock()
|
||||
previous := l.state.cleanup
|
||||
l.state.handler = handler
|
||||
l.state.cleanup = cleanup
|
||||
l.state.mu.Unlock()
|
||||
if previous != nil {
|
||||
previous()
|
||||
}
|
||||
}
|
||||
|
||||
// SetErrorSink changes the database/audit target while keeping the current
|
||||
// logger and its hot-reload state intact.
|
||||
func (l *ReloadableLogger) SetErrorSink(sink ErrorSink) {
|
||||
if l == nil || l.errorSink == nil {
|
||||
return
|
||||
}
|
||||
l.errorSink.mu.Lock()
|
||||
l.errorSink.sink = sink
|
||||
l.errorSink.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *ReloadableLogger) Close() {
|
||||
l.state.mu.Lock()
|
||||
cleanup := l.state.cleanup
|
||||
l.state.cleanup = nil
|
||||
l.state.mu.Unlock()
|
||||
if cleanup != nil {
|
||||
cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
type moduleFilterCore struct {
|
||||
zapcore.Core
|
||||
fileOnly map[string]struct{}
|
||||
mod string
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
mod := c.mod
|
||||
if value := moduleField(fields); value != "" {
|
||||
mod = value
|
||||
}
|
||||
return &moduleFilterCore{Core: c.Core.With(fields), fileOnly: c.fileOnly, mod: mod}
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(entry.Level) {
|
||||
return checked.AddCore(entry, c)
|
||||
}
|
||||
return checked
|
||||
}
|
||||
|
||||
func (c *moduleFilterCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
|
||||
mod := c.mod
|
||||
if value := moduleField(fields); value != "" {
|
||||
mod = value
|
||||
}
|
||||
if _, excluded := c.fileOnly[mod]; excluded {
|
||||
return nil
|
||||
}
|
||||
return c.Core.Write(entry, fields)
|
||||
}
|
||||
|
||||
func moduleField(fields []zapcore.Field) string {
|
||||
for _, field := range fields {
|
||||
if field.Key == "mod" {
|
||||
if field.String != "" {
|
||||
return field.String
|
||||
}
|
||||
if field.Interface != nil {
|
||||
return fmt.Sprint(field.Interface)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type routedFileState struct {
|
||||
mu sync.Mutex
|
||||
writers map[string]*DailyWriter
|
||||
}
|
||||
|
||||
type routedFileCore struct {
|
||||
base zapcore.Core
|
||||
encoder zapcore.Encoder
|
||||
level zapcore.LevelEnabler
|
||||
root string
|
||||
retentionDay int
|
||||
state *routedFileState
|
||||
fields []zapcore.Field
|
||||
errorSink *errorSinkState
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Enabled(level zapcore.Level) bool { return c.level.Enabled(level) }
|
||||
|
||||
func (c *routedFileCore) With(fields []zapcore.Field) zapcore.Core {
|
||||
inherited := append([]zapcore.Field(nil), c.fields...)
|
||||
inherited = append(inherited, fields...)
|
||||
return &routedFileCore{base: c.base.With(fields), encoder: c.encoder, level: c.level, root: c.root, retentionDay: c.retentionDay, state: c.state, fields: inherited, errorSink: c.errorSink}
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Check(entry zapcore.Entry, checked *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||||
if c.Enabled(entry.Level) {
|
||||
return checked.AddCore(entry, c)
|
||||
}
|
||||
return checked
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
|
||||
baseErr := c.base.Write(entry, fields)
|
||||
allFields := append([]zapcore.Field(nil), c.fields...)
|
||||
allFields = append(allFields, fields...)
|
||||
if entry.Level >= zapcore.ErrorLevel && !isGORMLoggerEntry(entry.Caller.File, allFields) {
|
||||
c.errorSink.record(errorEntryFromZap(entry, allFields))
|
||||
}
|
||||
paths := routedLogPaths(moduleField(allFields), entry.Level)
|
||||
if len(paths) == 0 {
|
||||
return baseErr
|
||||
}
|
||||
buffer, err := c.encoder.Clone().EncodeEntry(entry, allFields)
|
||||
if err != nil {
|
||||
if baseErr != nil {
|
||||
return baseErr
|
||||
}
|
||||
return err
|
||||
}
|
||||
defer buffer.Free()
|
||||
for _, name := range paths {
|
||||
writer := c.writer(name)
|
||||
if _, writeErr := writer.Write(buffer.Bytes()); writeErr != nil && baseErr == nil {
|
||||
baseErr = writeErr
|
||||
}
|
||||
}
|
||||
return baseErr
|
||||
}
|
||||
|
||||
func isGORMLoggerEntry(filename string, fields []zapcore.Field) bool {
|
||||
for _, field := range fields {
|
||||
if field.Key == "gorm_logger" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
normalized := strings.ReplaceAll(filename, "\\", "/")
|
||||
return strings.HasSuffix(normalized, "/gorm_logger_writer.go") ||
|
||||
strings.HasSuffix(normalized, "/internal/data/gorm_logger.go")
|
||||
}
|
||||
|
||||
func errorEntryFromZap(entry zapcore.Entry, fields []zapcore.Field) ErrorEntry {
|
||||
requestID, traceID, errorText := "", "", ""
|
||||
for _, field := range fields {
|
||||
switch field.Key {
|
||||
case "request_id":
|
||||
if requestID == "" {
|
||||
requestID = zapFieldString(field)
|
||||
}
|
||||
case "trace_id":
|
||||
if traceID == "" {
|
||||
traceID = zapFieldString(field)
|
||||
}
|
||||
case "error", "err":
|
||||
if errorText == "" {
|
||||
errorText = zapFieldString(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
info := entry.Message
|
||||
if errorText != "" {
|
||||
info += " | 错误: " + errorText
|
||||
}
|
||||
if entry.Caller.File != "" {
|
||||
info += fmt.Sprintf(" \n 源文件:%s:%d", entry.Caller.File, entry.Caller.Line)
|
||||
}
|
||||
if entry.Stack != "" {
|
||||
info += " \n 调用栈:" + entry.Stack
|
||||
if frame, ok := finalApplicationCaller(entry.Stack); ok {
|
||||
functionName, source, startLine, endLine, err := functionSourceAt(frame.File, frame.Line)
|
||||
if err == nil {
|
||||
info += fmt.Sprintf(" \n 最终调用方法:%s:%d (%s lines %d-%d)\n----- 产生日志的方法代码如下 -----\n%s", frame.File, frame.Line, functionName, startLine, endLine, source)
|
||||
} else {
|
||||
info += fmt.Sprintf(" \n 最终调用方法:%s:%d (%s) | extract_err=%v", frame.File, frame.Line, functionName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ErrorEntry{Form: "后端", Info: info, Level: entry.Level.String(), RequestID: requestID, TraceID: traceID}
|
||||
}
|
||||
|
||||
func zapFieldString(field zapcore.Field) string {
|
||||
if field.String != "" {
|
||||
return field.String
|
||||
}
|
||||
if field.Interface != nil {
|
||||
return fmt.Sprint(field.Interface)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Sync() error {
|
||||
result := c.base.Sync()
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
for _, writer := range c.state.writers {
|
||||
if err := writer.Sync(); err != nil && result == nil {
|
||||
result = err
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *routedFileCore) writer(name string) *DailyWriter {
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
writer := c.state.writers[name]
|
||||
if writer == nil {
|
||||
writer = NewDailyWriter(c.root, name, c.retentionDay)
|
||||
c.state.writers[name] = writer
|
||||
}
|
||||
return writer
|
||||
}
|
||||
|
||||
func (c *routedFileCore) Close() {
|
||||
c.state.mu.Lock()
|
||||
defer c.state.mu.Unlock()
|
||||
for name, writer := range c.state.writers {
|
||||
_ = writer.Close()
|
||||
delete(c.state.writers, name)
|
||||
}
|
||||
}
|
||||
|
||||
func routedLogPaths(module string, level zapcore.Level) []string {
|
||||
paths := make([]string, 0, 2)
|
||||
if module = safeModuleName(module); module != "" {
|
||||
switch module {
|
||||
case "http":
|
||||
paths = append(paths, filepath.Join("http", "access.log"))
|
||||
case "timedTask":
|
||||
paths = append(paths, filepath.Join("timedTask", "task.log"))
|
||||
case "error":
|
||||
paths = append(paths, filepath.Join("error", "error.log"))
|
||||
default:
|
||||
paths = append(paths, filepath.Join(module, "application.log"))
|
||||
}
|
||||
}
|
||||
if level >= zapcore.ErrorLevel {
|
||||
errorPath := filepath.Join("error", "error.log")
|
||||
if len(paths) == 0 || paths[len(paths)-1] != errorPath {
|
||||
paths = append(paths, errorPath)
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
func safeModuleName(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Map(func(r rune) rune {
|
||||
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_' {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}, value)
|
||||
}
|
||||
|
||||
// NewZapLogger adapts a Zap core to the slog logger used by Kratos v3.
|
||||
// The file layout remains compatible with the administration log viewer.
|
||||
func newZapHandler(root, filename string, options Options, errorSink *errorSinkState) (slog.Handler, func()) {
|
||||
file := NewDailyWriter(root, filename, options.RetentionDay)
|
||||
encoder := zap.NewProductionEncoderConfig()
|
||||
encoder.EncodeTime = zapcore.RFC3339NanoTimeEncoder
|
||||
if options.StacktraceKey != "" {
|
||||
encoder.StacktraceKey = options.StacktraceKey
|
||||
}
|
||||
switch options.EncodeLevel {
|
||||
case "CapitalLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
case "CapitalColorLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
case "LowercaseColorLevelEncoder":
|
||||
encoder.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
||||
default:
|
||||
encoder.EncodeLevel = zapcore.LowercaseLevelEncoder
|
||||
}
|
||||
var outputEncoder zapcore.Encoder = zapcore.NewJSONEncoder(encoder)
|
||||
if options.Format != "" && options.Format != "json" {
|
||||
if options.Prefix != "" {
|
||||
encoder.EncodeTime = func(value time.Time, output zapcore.PrimitiveArrayEncoder) {
|
||||
output.AppendString(options.Prefix + value.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
}
|
||||
outputEncoder = zapcore.NewConsoleEncoder(encoder)
|
||||
}
|
||||
level := zap.DebugLevel
|
||||
// zapcore.Level.Set reports an error but does not make the intended
|
||||
// configuration handling obvious here. Parse the configured value directly
|
||||
// so debug/warn/error are actually applied after startup and hot reload.
|
||||
if value := strings.TrimSpace(strings.ToLower(options.Level)); value != "" {
|
||||
if err := level.UnmarshalText([]byte(value)); err != nil {
|
||||
level = zap.DebugLevel
|
||||
}
|
||||
}
|
||||
levelEnabler := zap.NewAtomicLevelAt(level)
|
||||
fileCore := zapcore.NewCore(
|
||||
outputEncoder.Clone(),
|
||||
zapcore.AddSync(file),
|
||||
levelEnabler,
|
||||
)
|
||||
core := zapcore.Core(fileCore)
|
||||
if options.LogInConsole {
|
||||
fileOnly := make(map[string]struct{}, len(options.FileOnlyModules))
|
||||
for _, module := range options.FileOnlyModules {
|
||||
fileOnly[module] = struct{}{}
|
||||
}
|
||||
consoleCore := zapcore.NewCore(outputEncoder.Clone(), zapcore.AddSync(os.Stdout), levelEnabler)
|
||||
core = zapcore.NewTee(fileCore, &moduleFilterCore{Core: consoleCore, fileOnly: fileOnly})
|
||||
}
|
||||
routed := &routedFileCore{base: core, encoder: outputEncoder.Clone(), level: levelEnabler, root: root, retentionDay: options.RetentionDay, state: &routedFileState{writers: map[string]*DailyWriter{}}, errorSink: errorSink}
|
||||
zapLogger := zap.New(routed)
|
||||
handlerOptions := []zapslog.HandlerOption{zapslog.AddStacktraceAt(slog.LevelError)}
|
||||
if options.ShowLine {
|
||||
handlerOptions = append(handlerOptions, zapslog.WithCaller(true))
|
||||
}
|
||||
handler := zapslog.NewHandler(zapLogger.Core(), handlerOptions...)
|
||||
cleanup := func() {
|
||||
_ = zapLogger.Sync()
|
||||
routed.Close()
|
||||
_ = file.Close()
|
||||
}
|
||||
return handler, cleanup
|
||||
}
|
||||
|
||||
// NewReloadableZapLogger keeps the slog/Kratos adapter stable while replacing
|
||||
// the underlying Zap core when the runtime configuration changes.
|
||||
func NewReloadableZapLogger(root, filename string, options Options, attrs ...any) (*slog.Logger, *ReloadableLogger) {
|
||||
errorSink := &errorSinkState{}
|
||||
baseHandler, cleanup := newZapHandler(root, filename, options, errorSink)
|
||||
state := &reloadableHandlerState{handler: baseHandler, cleanup: cleanup}
|
||||
control := &ReloadableLogger{state: state, filename: filename, errorSink: errorSink}
|
||||
handler := &contextHandler{handler: &reloadableHandler{state: state}}
|
||||
logger := kratoslog.NewLogger(handler, kratoslog.WithExtractor(tracing.TraceAttrs)).With(attrs...)
|
||||
return logger, control
|
||||
}
|
||||
|
||||
func NewZapLogger(root, filename string, options Options, attrs ...any) (*slog.Logger, func()) {
|
||||
logger, control := NewReloadableZapLogger(root, filename, options, attrs...)
|
||||
return logger, control.Close
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
func TestZapHandlerHonorsConfiguredLevel(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "error", Format: "json"}, nil)
|
||||
defer cleanup()
|
||||
|
||||
if handler.Enabled(context.Background(), slog.LevelInfo) {
|
||||
t.Fatal("info should be disabled when level is error")
|
||||
}
|
||||
if !handler.Enabled(context.Background(), slog.LevelError) {
|
||||
t.Fatal("error should be enabled when level is error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerUsesDebugFallbackForInvalidLevel(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "not-a-level", Format: "json"}, nil)
|
||||
defer cleanup()
|
||||
|
||||
if !handler.Enabled(context.Background(), slog.LevelDebug) {
|
||||
t.Fatal("invalid levels should use the reference debug fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerRoutesHTTPAndErrorLogs(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
handler, cleanup := newZapHandler(root, "application.log", Options{Level: "info", Format: "json"}, nil)
|
||||
logger := slog.New(handler)
|
||||
logger.Info("request", "mod", "http", "request_id", "req-1")
|
||||
logger.Error("failed", "mod", "users")
|
||||
cleanup()
|
||||
|
||||
dateEntries, err := os.ReadDir(root)
|
||||
if err != nil || len(dateEntries) != 1 {
|
||||
t.Fatalf("expected one daily log directory, entries=%v err=%v", len(dateEntries), err)
|
||||
}
|
||||
date := dateEntries[0].Name()
|
||||
paths := []string{
|
||||
filepath.Join(root, date, "application.log"),
|
||||
filepath.Join(root, date, "http", "access.log"),
|
||||
filepath.Join(root, date, "users", "application.log"),
|
||||
filepath.Join(root, date, "error", "error.log"),
|
||||
}
|
||||
for _, path := range paths {
|
||||
if info, err := os.Stat(path); err != nil || info.Size() == 0 {
|
||||
t.Fatalf("expected non-empty routed log %s: info=%v err=%v", path, info, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestZapHandlerRecordsEveryErrorThroughSink(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
var entries []ErrorEntry
|
||||
logger, control := NewReloadableZapLogger(root, "application.log", Options{Level: "info", Format: "json"})
|
||||
defer control.Close()
|
||||
control.SetErrorSink(ErrorSinkFunc(func(_ context.Context, entry ErrorEntry) error {
|
||||
entries = append(entries, entry)
|
||||
return nil
|
||||
}))
|
||||
logger.Error("task execution failed", "mod", "timedTask", "request_id", "request-1", "trace_id", "trace-1", "error", os.ErrPermission)
|
||||
control.Reload(t.TempDir(), Options{Level: "info", Format: "json"})
|
||||
logger.Error("reloaded logger failure", "mod", "system")
|
||||
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("expected two error entries across reload, got %d", len(entries))
|
||||
}
|
||||
entry := entries[0]
|
||||
if entry.Form != "后端" || entry.Level != "error" || entry.RequestID != "request-1" || entry.TraceID != "trace-1" || !strings.Contains(entry.Info, "task execution failed") || !strings.Contains(entry.Info, os.ErrPermission.Error()) {
|
||||
t.Fatalf("unexpected error entry: %+v", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorEntryIncludesFinalApplicationSource(t *testing.T) {
|
||||
filename := filepath.Join(t.TempDir(), "worker.go")
|
||||
content := "package sample\n\nfunc execute() {\n\tprintln(\"failed\")\n}\n"
|
||||
if err := os.WriteFile(filename, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entry := errorEntryFromZap(zapcore.Entry{Message: "task failed", Level: zapcore.ErrorLevel, Stack: "kra/internal/worker.execute\n" + filename + ":4"}, nil)
|
||||
if !strings.Contains(entry.Info, "最终调用方法:"+filename+":4 (execute lines 3-5)") || !strings.Contains(entry.Info, "func execute()") {
|
||||
t.Fatalf("expected final caller source in error entry: %s", entry.Info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorSinkSkipsGORMBridge(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
var entries []ErrorEntry
|
||||
state := &errorSinkState{sink: ErrorSinkFunc(func(_ context.Context, entry ErrorEntry) error {
|
||||
entries = append(entries, entry)
|
||||
return nil
|
||||
})}
|
||||
base := zapcore.NewNopCore()
|
||||
core := &routedFileCore{base: base, encoder: zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), level: zapcore.ErrorLevel, root: root, state: &routedFileState{writers: map[string]*DailyWriter{}}, errorSink: state}
|
||||
defer core.Close()
|
||||
for _, filename := range []string{"/tmp/gorm_logger_writer.go", "/workspace/internal/data/gorm_logger.go"} {
|
||||
if err := core.Write(zapcore.Entry{Level: zapcore.ErrorLevel, Message: "database failed", Caller: zapcore.EntryCaller{Defined: true, File: filename, Line: 10}}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := core.Write(zapcore.Entry{Level: zapcore.ErrorLevel, Message: "database failed"}, []zapcore.Field{zap.Bool("gorm_logger", true)}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("gorm bridge error must not recurse into sys_error: %+v", entries)
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package osskit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
)
|
||||
|
||||
// ComposeStreams concatenates objects into destination while calculating the
|
||||
// MD5 of the exact bytes written. Provider-specific storage clients stay out
|
||||
// of this package; callbacks keep this helper independent from biz and SDKs.
|
||||
func ComposeStreams(
|
||||
ctx context.Context,
|
||||
names []string,
|
||||
open func(context.Context, string) (io.ReadCloser, error),
|
||||
put func(context.Context, string, io.Reader) error,
|
||||
remove func(context.Context, string) error,
|
||||
destination string,
|
||||
) (string, error) {
|
||||
reader, writer := io.Pipe()
|
||||
hash := md5.New()
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
defer writer.Close()
|
||||
for _, name := range names {
|
||||
if err := ctx.Err(); err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
file, err := open(ctx, name)
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
return
|
||||
}
|
||||
_, copyErr := io.Copy(io.MultiWriter(writer, hash), file)
|
||||
_ = file.Close()
|
||||
if copyErr != nil {
|
||||
errCh <- copyErr
|
||||
return
|
||||
}
|
||||
}
|
||||
errCh <- nil
|
||||
}()
|
||||
putErr := put(ctx, destination, reader)
|
||||
composeErr := <-errCh
|
||||
if putErr != nil {
|
||||
_ = remove(ctx, destination)
|
||||
return "", putErr
|
||||
}
|
||||
if composeErr != nil {
|
||||
_ = remove(ctx, destination)
|
||||
return "", composeErr
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package osskit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestComposeStreams(t *testing.T) {
|
||||
objects := map[string]string{"a": "hello ", "b": "world"}
|
||||
var stored string
|
||||
hash, err := ComposeStreams(context.Background(), []string{"a", "b"}, func(_ context.Context, name string) (io.ReadCloser, error) {
|
||||
value, ok := objects[name]
|
||||
if !ok {
|
||||
return nil, errors.New("missing object")
|
||||
}
|
||||
return io.NopCloser(strings.NewReader(value)), nil
|
||||
}, func(_ context.Context, _ string, reader io.Reader) error {
|
||||
value, err := io.ReadAll(reader)
|
||||
stored = string(value)
|
||||
return err
|
||||
}, func(context.Context, string) error { return nil }, "out")
|
||||
if err != nil || stored != "hello world" {
|
||||
t.Fatalf("compose = %q, %v", stored, err)
|
||||
}
|
||||
want := md5.Sum([]byte(stored))
|
||||
if hash != hex.EncodeToString(want[:]) {
|
||||
t.Fatalf("hash = %q", hash)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeStreamsRemovesPartialDestination(t *testing.T) {
|
||||
removed := false
|
||||
_, err := ComposeStreams(context.Background(), []string{"missing"}, func(context.Context, string) (io.ReadCloser, error) {
|
||||
return nil, errors.New("open failed")
|
||||
}, func(_ context.Context, _ string, reader io.Reader) error {
|
||||
_, _ = io.ReadAll(reader)
|
||||
return nil
|
||||
}, func(context.Context, string) error {
|
||||
removed = true
|
||||
return nil
|
||||
}, "out")
|
||||
if err == nil || !removed {
|
||||
t.Fatalf("compose error = %v, removed = %v", err, removed)
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
// Package pagination contains database-agnostic page calculations shared by
|
||||
// repository implementations.
|
||||
package pagination
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// LimitOffset normalizes a page request and applies the optional maximum page
|
||||
// size. A non-positive size means that no limit should be applied.
|
||||
func LimitOffset(page, size, maxSize int) (limit, offset int) {
|
||||
limit = size
|
||||
if maxSize > 0 && size > maxSize {
|
||||
limit = maxSize
|
||||
}
|
||||
if limit <= 0 {
|
||||
return 0, 0
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
return limit, (page - 1) * limit
|
||||
}
|
||||
|
||||
// Apply applies pagination only when the caller supplied a positive size.
|
||||
func Apply(db *gorm.DB, page, size, maxSize int) *gorm.DB {
|
||||
limit, offset := LimitOffset(page, size, maxSize)
|
||||
if limit == 0 {
|
||||
return db
|
||||
}
|
||||
return db.Offset(offset).Limit(limit)
|
||||
}
|
||||
|
||||
// ApplyRequired always applies LIMIT/OFFSET, including LIMIT 0. This keeps
|
||||
// the existing repository contract for APIs whose page size is mandatory.
|
||||
func ApplyRequired(db *gorm.DB, page, size, maxSize int) *gorm.DB {
|
||||
limit, offset := LimitOffset(page, size, maxSize)
|
||||
return db.Offset(offset).Limit(limit)
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ParseIntegerAmount parses an integer amount expressed in the smallest
|
||||
// currency unit. Floating point is intentionally not involved.
|
||||
func ParseIntegerAmount(value string) (int64, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return 0, errors.New("支付金额为空")
|
||||
}
|
||||
return strconv.ParseInt(value, 10, 64)
|
||||
}
|
||||
|
||||
// ParseDecimalAmount converts a major-unit decimal string to an integer amount
|
||||
// using a power-of-ten scale, without using floating point.
|
||||
func ParseDecimalAmount(value string, scale int64) (int64, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || scale <= 0 {
|
||||
return 0, errors.New("支付金额或换算比例无效")
|
||||
}
|
||||
if strings.HasPrefix(value, "-") {
|
||||
return 0, errors.New("支付金额不能为负数")
|
||||
}
|
||||
parts := strings.Split(value, ".")
|
||||
if len(parts) > 2 || parts[0] == "" {
|
||||
return 0, fmt.Errorf("支付金额格式错误: %s", value)
|
||||
}
|
||||
digits, err := scaleDigits(scale)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
whole, err := strconv.ParseInt(parts[0], 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fraction := ""
|
||||
if len(parts) == 2 {
|
||||
fraction = parts[1]
|
||||
}
|
||||
if len(fraction) > digits {
|
||||
return 0, fmt.Errorf("支付金额精度超过 %d 位", digits)
|
||||
}
|
||||
fraction += strings.Repeat("0", digits-len(fraction))
|
||||
fractionValue := int64(0)
|
||||
if fraction != "" {
|
||||
fractionValue, err = strconv.ParseInt(fraction, 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
if whole > (int64(^uint64(0)>>1)-fractionValue)/scale {
|
||||
return 0, errors.New("支付金额超出范围")
|
||||
}
|
||||
return whole*scale + fractionValue, nil
|
||||
}
|
||||
|
||||
func FormatDecimalAmount(amount, scale int64) (string, error) {
|
||||
if amount <= 0 || scale <= 0 {
|
||||
return "", errors.New("支付金额或换算比例无效")
|
||||
}
|
||||
digits, err := scaleDigits(scale)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if digits == 0 {
|
||||
return strconv.FormatInt(amount, 10), nil
|
||||
}
|
||||
return fmt.Sprintf("%d.%0*d", amount/scale, digits, amount%scale), nil
|
||||
}
|
||||
|
||||
func scaleDigits(scale int64) (int, error) {
|
||||
digits := 0
|
||||
for n := scale; n > 1 && n%10 == 0; n /= 10 {
|
||||
digits++
|
||||
}
|
||||
if scale <= 0 || scale != pow10(digits) {
|
||||
return 0, errors.New("支付金额换算比例必须是 10 的幂")
|
||||
}
|
||||
return digits, nil
|
||||
}
|
||||
|
||||
func pow10(digits int) int64 {
|
||||
value := int64(1)
|
||||
for i := 0; i < digits; i++ {
|
||||
if value > (int64(^uint64(0)>>1))/10 {
|
||||
return 0
|
||||
}
|
||||
value *= 10
|
||||
}
|
||||
return value
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDecimalAmountUsesIntegerMath(t *testing.T) {
|
||||
got, err := ParseDecimalAmount("12.34", 100)
|
||||
if err != nil || got != 1234 {
|
||||
t.Fatalf("ParseDecimalAmount = %d, %v", got, err)
|
||||
}
|
||||
formatted, err := FormatDecimalAmount(got, 100)
|
||||
if err != nil || formatted != "12.34" {
|
||||
t.Fatalf("FormatDecimalAmount = %q, %v", formatted, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalAmountRejectsUnsafeInput(t *testing.T) {
|
||||
for _, value := range []string{"-1.00", "1.001", "1.2.3"} {
|
||||
if _, err := ParseDecimalAmount(value, 100); err == nil {
|
||||
t.Fatalf("ParseDecimalAmount(%q) accepted unsafe input", value)
|
||||
}
|
||||
}
|
||||
if _, err := ParseDecimalAmount("1", 3); err == nil {
|
||||
t.Fatal("non power-of-ten scale accepted")
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func JSONObject(raw []byte) map[string]any {
|
||||
var value any
|
||||
if json.Unmarshal(raw, &value) != nil {
|
||||
return nil
|
||||
}
|
||||
object, _ := value.(map[string]any)
|
||||
return object
|
||||
}
|
||||
|
||||
func NestedString(value any, keys ...string) string {
|
||||
found := nestedValue(value, keys...)
|
||||
if found == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(fmt.Sprint(found))
|
||||
}
|
||||
|
||||
func StringAtPath(value any, path string) string {
|
||||
found := ValueAtPath(value, path)
|
||||
if found == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(fmt.Sprint(found))
|
||||
}
|
||||
|
||||
func ValueAtPath(value any, path string) any {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
current := value
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
object, ok := current.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
current = object[part]
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func nestedValue(value any, keys ...string) any {
|
||||
object, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
for _, key := range keys {
|
||||
if found, exists := object[key]; exists && found != nil && fmt.Sprint(found) != "" {
|
||||
return found
|
||||
}
|
||||
}
|
||||
for _, nestedKey := range []string{"data", "result", "order", "transaction", "response"} {
|
||||
if found := nestedValue(object[nestedKey], keys...); found != nil {
|
||||
return found
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNestedAndPathValues(t *testing.T) {
|
||||
object := JSONObject([]byte(`{"data":{"status":"SUCCESS","amount":100}}`))
|
||||
if got := NestedString(object, "status"); got != "SUCCESS" {
|
||||
t.Fatalf("NestedString = %q", got)
|
||||
}
|
||||
if got := StringAtPath(object, "data.amount"); got != "100" {
|
||||
t.Fatalf("StringAtPath = %q", got)
|
||||
}
|
||||
if got := StringAtPath(object, "data.missing"); got != "" {
|
||||
t.Fatalf("missing path = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func WechatV2Sign(values map[string]string, key, signType string) string {
|
||||
keys := make([]string, 0, len(values))
|
||||
for k, v := range values {
|
||||
if k != "sign" && v != "" {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
parts := make([]string, 0, len(keys))
|
||||
for _, k := range keys {
|
||||
parts = append(parts, k+"="+values[k])
|
||||
}
|
||||
raw := strings.Join(parts, "&") + "&key=" + key
|
||||
if strings.EqualFold(signType, "HMAC-SHA256") {
|
||||
return HMACSHA256Hex([]byte(raw), key, true)
|
||||
}
|
||||
sum := md5.Sum([]byte(raw))
|
||||
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
||||
}
|
||||
|
||||
func MD5Canonical(values map[string]any, secret string) string {
|
||||
keys := make([]string, 0, len(values))
|
||||
for key, value := range values {
|
||||
if key != "sign" && value != nil && fmt.Sprint(value) != "" {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
query := url.Values{}
|
||||
for _, key := range keys {
|
||||
query.Set(key, fmt.Sprint(values[key]))
|
||||
}
|
||||
sum := md5.Sum([]byte(query.Encode() + "&key=" + secret))
|
||||
return strings.ToUpper(hex.EncodeToString(sum[:]))
|
||||
}
|
||||
|
||||
func HMACSHA256Hex(raw []byte, secret string, upper bool) string {
|
||||
h := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = h.Write(raw)
|
||||
result := hex.EncodeToString(h.Sum(nil))
|
||||
if upper {
|
||||
return strings.ToUpper(result)
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestWechatV2SignIsStableAndExcludesSign(t *testing.T) {
|
||||
values := map[string]string{"b": "2", "a": "1", "sign": "old"}
|
||||
first := WechatV2Sign(values, "secret", "MD5")
|
||||
values["sign"] = "different"
|
||||
if second := WechatV2Sign(values, "secret", "MD5"); first != second {
|
||||
t.Fatalf("signature changed when sign field changed: %q != %q", first, second)
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NormalizeStatus(value, fallback string) string {
|
||||
switch strings.ToUpper(strings.TrimSpace(value)) {
|
||||
case "SUCCESS", "PAID", "TRADE_SUCCESS", "TRADE_FINISHED", "COMPLETED", "PAY_SUCCESS":
|
||||
return "success"
|
||||
case "WAIT_BUYER_PAY", "USERPAYING", "NOTPAY", "PROCESSING", "PENDING", "CREATED", "ACCEPT", "PAYING":
|
||||
return "pending"
|
||||
case "CLOSED", "TRADE_CLOSED", "CANCELLED", "CANCELED", "REVOKED", "REFUND", "REFUNDED", "FAILED", "FAIL", "PAYERROR":
|
||||
return "failed"
|
||||
default:
|
||||
return fallback
|
||||
}
|
||||
}
|
||||
|
||||
func ConfiguredInt64(values map[string]any, key string, fallback int64) int64 {
|
||||
value, ok := values[key]
|
||||
if !ok {
|
||||
return fallback
|
||||
}
|
||||
parsed, err := ParseIntegerAmount(strings.TrimSpace(toString(value)))
|
||||
if err != nil || parsed <= 0 {
|
||||
return fallback
|
||||
}
|
||||
return parsed
|
||||
}
|
||||
|
||||
func ConfiguredValues(values map[string]any, key string) []string {
|
||||
result := []string{}
|
||||
for _, item := range strings.Split(toString(values[key]), ",") {
|
||||
item = strings.ToUpper(strings.TrimSpace(item))
|
||||
if item != "" {
|
||||
result = append(result, item)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func ContainsFold(values []string, value string) bool {
|
||||
value = strings.ToUpper(strings.TrimSpace(value))
|
||||
for _, item := range values {
|
||||
if item == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func toString(value any) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(strings.ReplaceAll(strings.TrimSpace(stringify(value)), "<nil>", ""))
|
||||
}
|
||||
|
||||
func stringify(value any) string {
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package paymentkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type xmlValue struct {
|
||||
XMLName xml.Name
|
||||
Value string `xml:",chardata"`
|
||||
}
|
||||
|
||||
func XMLValues(raw []byte) (map[string]string, error) {
|
||||
decoder := xml.NewDecoder(bytes.NewReader(raw))
|
||||
result := map[string]string{}
|
||||
for {
|
||||
token, err := decoder.Token()
|
||||
if err == io.EOF {
|
||||
return result, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start, ok := token.(xml.StartElement)
|
||||
if !ok || start.Name.Local == "xml" {
|
||||
continue
|
||||
}
|
||||
var value xmlValue
|
||||
if err = decoder.DecodeElement(&value, &start); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value.Value != "" {
|
||||
result[start.Name.Local] = value.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func XMLEncode(values map[string]string) []byte {
|
||||
var b strings.Builder
|
||||
b.WriteString("<xml>")
|
||||
keys := make([]string, 0, len(values))
|
||||
for key := range values {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
b.WriteString("<")
|
||||
b.WriteString(key)
|
||||
b.WriteString("><![CDATA[")
|
||||
b.WriteString(values[key])
|
||||
b.WriteString("]]></")
|
||||
b.WriteString(key)
|
||||
b.WriteString(">")
|
||||
}
|
||||
b.WriteString("</xml>")
|
||||
return []byte(b.String())
|
||||
}
|
||||
Reference in New Issue
Block a user