Fixed short url return and improved .env loading
This commit is contained in:
@@ -5,8 +5,11 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logrus.New()
|
||||
|
||||
type RedisConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
@@ -22,15 +25,6 @@ type DatabaseConfig struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func LoadEnv() error {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load .env file: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func GetEnv(key string, defaultValue string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
@@ -39,6 +33,15 @@ func GetEnv(key string, defaultValue string) string {
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
func LoadDotEnv() {
|
||||
if err := godotenv.Load(); err != nil {
|
||||
// .env file not found, continue using environment variables
|
||||
log.Info("No .env file found, using environment variables")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func DatabaseUrl() string {
|
||||
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", GetEnv("DB_USERNAME", "postgres"),
|
||||
GetEnv("DB_PASSWORD", "postgres"), GetEnv("DB_HOST", "localhost"), GetEnv("DB_PORT", "5432"), GetEnv("DB_NAME", "postgres"))
|
||||
@@ -49,11 +52,6 @@ func RedisUrl() string {
|
||||
}
|
||||
|
||||
func LoadRedisConfig() (RedisConfig, error) {
|
||||
err := LoadEnv()
|
||||
if err != nil {
|
||||
return RedisConfig{}, err
|
||||
}
|
||||
|
||||
return RedisConfig{
|
||||
Host: GetEnv("REDIS_HOST", "localhost"),
|
||||
Port: GetEnv("REDIS_PORT", "6379"),
|
||||
@@ -63,11 +61,6 @@ func LoadRedisConfig() (RedisConfig, error) {
|
||||
}
|
||||
|
||||
func LoadDbConfig() (DatabaseConfig, error) {
|
||||
err := LoadEnv()
|
||||
if err != nil {
|
||||
return DatabaseConfig{}, err
|
||||
}
|
||||
|
||||
return DatabaseConfig{
|
||||
Host: GetEnv("POSTGRES_HOST", "localhost"),
|
||||
Port: GetEnv("POSTGRES_PORT", "5432"),
|
||||
|
||||
Reference in New Issue
Block a user