Skip to content

Commit

Permalink
Adding support for generic command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifercr07 committed Oct 5, 2024
1 parent f5b8348 commit c35eea0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func LoadConfig() *Config {
ServerPort: getEnv("SERVER_PORT", ":8080"), // Default server port
RequestLimitPerMin: getEnvInt("REQUEST_LIMIT_PER_MIN", 1000), // Default request limit
RequestWindowSec: getEnvFloat64("REQUEST_WINDOW_SEC", 60), // Default request window in float64
AllowedOrigins: getEnvArray("ALLOWED_ORIGINS", []string{"http://localhost:8080"}), // Default allowed origins
AllowedOrigins: getEnvArray("ALLOWED_ORIGINS", []string{"http://localhost:3000"}), // Default allowed origins
}
}

Expand Down
13 changes: 10 additions & 3 deletions internal/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,37 @@ import (
"server/config"
)

func enableCors(w http.ResponseWriter, r *http.Request) {
// Updated enableCors function to return a boolean indicating if OPTIONS was handled
func handleCors(w http.ResponseWriter, r *http.Request) bool {
configValue := config.LoadConfig()
allAllowedOrigins := configValue.AllowedOrigins
origin := r.Header.Get("Origin")
allowed := false

for _, allowedOrigin := range allAllowedOrigins {
if origin == allowedOrigin || allowedOrigin == "*" || origin == "" {
allowed = true
break
}
}

if !allowed {
http.Error(w, "CORS: origin not allowed", http.StatusForbidden)
return
return true
}

w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE, PATCH")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, Content-Length")

// If the request is an OPTIONS request, handle it and stop further processing
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Max-Age", "86400")
w.WriteHeader(http.StatusOK)
return
return true
}

// Continue processing other requests
w.Header().Set("Content-Type", "application/json")
return false
}
10 changes: 8 additions & 2 deletions internal/middleware/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
// RateLimiter middleware to limit requests based on a specified limit and duration
func RateLimiter(client *db.DiceDB, next http.Handler, limit int64, window float64) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
enableCors(w, r)
if handleCors(w, r) {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand Down Expand Up @@ -79,7 +82,10 @@ func RateLimiter(client *db.DiceDB, next http.Handler, limit int64, window float

func MockRateLimiter(client *mock.DiceDBMock, next http.Handler, limit int64, window float64) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
enableCors(w, r)
if handleCors(w, r) {
return
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand Down

0 comments on commit c35eea0

Please sign in to comment.