Add cursor rules and initial configuration for Tender Management Go Backend

This commit is contained in:
n.nakhostin
2025-08-02 10:43:47 +03:30
parent ad9db7bcce
commit 5a1ceb0bd6
35 changed files with 4123 additions and 4550 deletions
+18 -70
View File
@@ -8,8 +8,6 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"tm/internal/infrastructure"
)
// Logger interface defines logging methods
@@ -30,8 +28,23 @@ type ZapLogger struct {
fields map[string]interface{}
}
type Config struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
Output string `mapstructure:"output"`
File FileConfig `mapstructure:"file"`
}
type FileConfig struct {
Path string `mapstructure:"path"`
MaxSize int `mapstructure:"max_size"` // MB
MaxBackups int `mapstructure:"max_backups"`
MaxAge int `mapstructure:"max_age"` // days
Compress bool `mapstructure:"compress"`
}
// NewLogger creates a new logger instance with Zap and Lumberjack
func NewLogger(config *infrastructure.LoggingConfig) Logger {
func NewLogger(config *Config) Logger {
// Parse log level
level := parseLogLevel(config.Level)
@@ -62,7 +75,7 @@ func NewLogger(config *infrastructure.LoggingConfig) Logger {
}
// createWriter creates the appropriate writer based on configuration
func createWriter(config *infrastructure.LoggingConfig) zapcore.WriteSyncer {
func createWriter(config *Config) zapcore.WriteSyncer {
switch config.Output {
case "stdout":
return zapcore.AddSync(os.Stdout)
@@ -77,7 +90,7 @@ func createWriter(config *infrastructure.LoggingConfig) zapcore.WriteSyncer {
}
// createFileWriter creates a file writer with rotation using lumberjack
func createFileWriter(fileConfig infrastructure.LogFileConfig) zapcore.WriteSyncer {
func createFileWriter(fileConfig FileConfig) zapcore.WriteSyncer {
// Ensure log directory exists
logDir := filepath.Dir(fileConfig.Path)
if err := os.MkdirAll(logDir, 0755); err != nil {
@@ -237,68 +250,3 @@ func (l *ZapLogger) mergeFields(fields map[string]interface{}) map[string]interf
return merged
}
// Global logger instance
var globalLogger Logger
// InitLogger initializes the global logger
func InitLogger(config *infrastructure.LoggingConfig) {
globalLogger = NewLogger(config)
}
// GetLogger returns the global logger instance
func GetLogger() Logger {
if globalLogger == nil {
// Fallback configuration
config := &infrastructure.LoggingConfig{
Level: "info",
Format: "json",
Output: "stdout",
File: infrastructure.LogFileConfig{
Path: "./logs/app.log",
MaxSize: 100,
MaxBackups: 5,
MaxAge: 30,
Compress: true,
},
}
globalLogger = NewLogger(config)
}
return globalLogger
}
// Cleanup flushes and closes the logger
func Cleanup() {
if globalLogger != nil {
globalLogger.Sync()
}
}
// Helper functions for convenience
func Debug(msg string, fields map[string]interface{}) {
GetLogger().Debug(msg, fields)
}
func Info(msg string, fields map[string]interface{}) {
GetLogger().Info(msg, fields)
}
func Warn(msg string, fields map[string]interface{}) {
GetLogger().Warn(msg, fields)
}
func Error(msg string, fields map[string]interface{}) {
GetLogger().Error(msg, fields)
}
func Fatal(msg string, fields map[string]interface{}) {
GetLogger().Fatal(msg, fields)
}
func WithFields(fields map[string]interface{}) Logger {
return GetLogger().WithFields(fields)
}
func Sync() error {
return GetLogger().Sync()
}