Skip to content

Make Request Body Size Configurable #116

@AnkanMisra

Description

@AnkanMisra

Problem

The Gateway currently enforces a hardcoded request body size limit of 10MB in gateway/main.go.

// gateway/main.go line ~363
const maxBodySize = 10 * 1024 * 1024

Hardcoded limits are an anti-pattern for production services. Different deployments may need to handle larger documents for summarization or, conversely, restrict payloads to very small sizes to prevent DoS attacks on memory.

Solution

Refactor this constant into an environment variable MAX_REQUEST_BODY_MB with a default value of 10.

Implementation

  1. Add Helper: Add a configuration helper getMaxBodySize() in gateway/config.go.
    • Read MAX_REQUEST_BODY_MB (int).
    • Default to 10.
    • Return bytes (val * 1024 * 1024).
  2. Update Handler: Replace the const maxBodySize in handleSummarize with the dynamic value from the helper.
  3. Update Documentation: Add the new variable to .env.example.

Code Hint

// gateway/config.go
func getMaxBodySize() int64 {
    mb := getEnvAsInt("MAX_REQUEST_BODY_MB", 10)
    return int64(mb) * 1024 * 1024
}

// gateway/main.go
func handleSummarize(c *gin.Context) {
    // ...
    maxSize := getMaxBodySize()
    c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSize)
    // ...
}

Acceptance Criteria

  • MAX_REQUEST_BODY_MB environment variable controls the limit.
  • Default remains 10MB if variable is unset.
  • .env.example includes the new variable.

Testing

# 1. Set MAX_REQUEST_BODY_MB=1 in .env
# 2. Restart Gateway
# 3. Send a 2MB payload
curl -X POST localhost:3000/api/ai/summarize -d @large_file.json
# Expect: 413 Request Entity Too Large

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions