mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-04 00:06:22 +00:00
- Updated `getPostById` function to accept an optional authorization token. - Modified `PostPage` to retrieve the token from cookies and pass it to the API call. - Added smooth transition effects for background and text colors in `globals.css`. - Cleaned up imports and formatting in `blog-home.tsx`. - Refactored `blog-post.tsx` to use `MDXRemote` for rendering markdown content. - Introduced `blog-comment.tsx` and `blog-post-header.client.tsx` components for better structure. - Added a switch component for dark/light mode toggle in the navbar. - Updated `Post` model to include a description field.
97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/joho/godotenv"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/snowykami/neo-blog/pkg/constant"
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
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
|
|
}
|
|
}
|