Files
neo-blog/pkg/utils/env.go
Snowykami dd7641bf6e
All checks were successful
Push to Helm Chart Repository / build (push) Successful in 13s
feat: implement advanced comment features including reply and like functionality
- Added support for nested comments with reply functionality.
- Implemented like/unlike feature for comments and posts.
- Enhanced comment DTO to include reply count, like count, and like status.
- Updated comment and like services to handle new functionalities.
- Created new API endpoints for toggling likes and listing comments.
- Improved UI components for comments to support replies and likes with animations.
- Added localization for new comment-related messages.
- Introduced a TODO list for future enhancements in the comment module.
2025-09-09 00:24:25 +08:00

98 lines
2.0 KiB
Go

package utils
import (
"fmt"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/snowykami/neo-blog/pkg/constant"
)
var (
IsDevMode = false
)
func init() {
err := godotenv.Load()
if err != nil {
logrus.Warnf("Error loading .env file: %v", err)
}
logrus.Infof("env loaded")
// Init env
IsDevMode = Env.Get(constant.EnvKeyMode, constant.ModeProd) == constant.ModeDev
// Set log level
logrus.SetLevel(getLogLevel(Env.Get(constant.EnvKeyLogLevel, "info")))
if logrus.GetLevel() == logrus.DebugLevel {
logrus.Debug("Debug mode is enabled, printing environment variables:")
for _, e := range os.Environ() {
if len(e) > 0 && e[0] == '_' {
// Skip environment variables that start with '_'
continue
}
fmt.Printf("%s ", e)
}
}
}
type envUtils struct{}
var Env envUtils
func (e *envUtils) Get(key string, defaultValue ...string) string {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
return value
}
func (e *envUtils) GetAsInt(key string, defaultValue ...int) int {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
intValue, err := strconv.Atoi(value)
if err != nil && len(defaultValue) > 0 {
return defaultValue[0]
}
return intValue
}
func (e *envUtils) GetAsBool(key string, defaultValue ...bool) bool {
value := os.Getenv(key)
if value == "" && len(defaultValue) > 0 {
return defaultValue[0]
}
boolValue, err := strconv.ParseBool(value)
if err != nil {
if len(defaultValue) > 0 {
return defaultValue[0]
}
return false
}
return boolValue
}
func getLogLevel(levelString string) logrus.Level {
switch levelString {
case "debug":
return logrus.DebugLevel
case "info":
return logrus.InfoLevel
case "warn":
return logrus.WarnLevel
case "error":
return logrus.ErrorLevel
case "fatal":
return logrus.FatalLevel
case "panic":
return logrus.PanicLevel
default:
logrus.Warnf("Unknown log level: %s, defaulting to InfoLevel", levelString)
return logrus.InfoLevel
}
}