Skip to content

Commit

Permalink
error fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
JubaerHossain committed Sep 19, 2024
1 parent 10c67ca commit d98b3a8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"gorm",
"Hossain",
"jackc",
"mapstructure",
"oneof",
"Payables",
"pgxpool",
Expand Down
8 changes: 3 additions & 5 deletions pkg/core/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"net/http"
"os" // Add this import
"os/signal" // Add this import
"syscall" // Add this import
"strings"
"syscall" // Add this import
"time"

"github.com/JubaerHossain/rootx/pkg/core/cache"
Expand Down Expand Up @@ -56,11 +57,10 @@ func StartApp() (*App, error) {

// Initialize database and cache asynchronously


var pgDB *pgxpool.Pool
var mySQLDB *sql.DB

if cfg.DBType == "postgres" {
if strings.TrimSpace(cfg.DBType) == "postgres" {
db, err := InitPqDatabase(cfg)
if err != nil {
return nil, err
Expand All @@ -75,8 +75,6 @@ func StartApp() (*App, error) {
mySQLDB = mdb
}



cacheService, err := InitCache()
if err != nil {
return nil, err
Expand Down
48 changes: 46 additions & 2 deletions pkg/core/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"os"
"sync"

"github.com/spf13/viper"
Expand Down Expand Up @@ -49,8 +50,8 @@ var (
configMutex sync.Mutex
)

// LoadConfig reads and parses the configuration file and environment variables
func LoadConfig() (*Config, error) {
// Lock the mutex to ensure thread safety during configuration loading
configMutex.Lock()
defer configMutex.Unlock()

Expand All @@ -72,6 +73,11 @@ func LoadConfig() (*Config, error) {
// Set default values for configuration fields
setDefaultValues(&cfg)

// Validate configuration values
if err := validateConfig(&cfg); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}

// Set the global configuration variable
GlobalConfig = &cfg

Expand All @@ -80,8 +86,46 @@ func LoadConfig() (*Config, error) {

// setDefaultValues sets default values for configuration fields
func setDefaultValues(cfg *Config) {
if cfg.AppPort == 0 {
cfg.AppPort = 8080 // Default port if not set
}
if cfg.RedisDB == 0 {
cfg.RedisDB = 0 // Default Redis database
}
// Add default values for other configuration fields as needed
if cfg.RateLimit == 0 {
cfg.RateLimit = 100 // Default rate limit
}
if cfg.StorageDisk == "" {
cfg.StorageDisk = "local" // Default storage disk
}
if cfg.JwtExpiration == "" {
cfg.JwtExpiration = "1h" // Default JWT expiration
}
}

// validateConfig validates critical configuration values
func validateConfig(cfg *Config) error {
if cfg.DBHost == "" {
return fmt.Errorf("DBHost must be set")
}
if cfg.DBPort <= 0 {
return fmt.Errorf("DBPort must be a positive integer")
}
if cfg.StorageDisk != "local" && cfg.StorageDisk != "s3" {
return fmt.Errorf("StorageDisk must be either 'local' or 's3'")
}
if cfg.JwtSecretKey == "" {
return fmt.Errorf("JWTSecretKey must be set")
}
// Add other validation rules as needed
return nil
}

// LoadSecretFromEnv loads a secret from an environment variable
func LoadSecretFromEnv(key string) (string, error) {
value := os.Getenv(key)
if value == "" {
return "", fmt.Errorf("environment variable %s not set", key)
}
return value, nil
}
5 changes: 3 additions & 2 deletions pkg/core/filesystem/fileupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/JubaerHossain/rootx/pkg/core/config"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (s *FileUploadService) FileUpload(r *http.Request, formKey string, folder s
uniqueFileName := s.generateUniqueFileName(handler.Filename)

// Determine storage destination based on app config
if s.Config.StorageDisk == "s3" {
if strings.TrimSpace(s.Config.StorageDisk) == "s3" {
filePath := folder + "/" + uniqueFileName
return s.uploadToS3(file, s.Config, filePath, handler.Filename)
} else {
Expand Down Expand Up @@ -189,7 +190,7 @@ func (s *FileUploadService) DeleteFromS3(filePath string, cfg *config.Config) er
// Example usage to delete an image (assuming you have the filePath stored)
func (s *FileUploadService) DeleteImage(filePath string) error {
var err error
if s.Config.StorageDisk == "s3" {
if strings.TrimSpace(s.Config.StorageDisk) == "s3" {
err = s.DeleteFromS3(filePath, s.Config)
} else {
err = s.DeleteFromLocal(filePath)
Expand Down

0 comments on commit d98b3a8

Please sign in to comment.