Skip to content

Conversation

@Vaishnavi-Iyer67
Copy link

@Vaishnavi-Iyer67 Vaishnavi-Iyer67 commented Jan 19, 2026

What this PR does

  • Extracts Swagger UI HTML out of main.go into a dedicated template
  • Embeds the template using go:embed
  • Makes Swagger UI version configurable via SWAGGER_UI_VERSION
  • Keeps a safe default (5.11.0) when env var is missing or invalid

Why this change

  • Avoids hard-coding large HTML inside Go code
  • Makes Swagger UI upgrades easier without touching logic
  • Improves separation of concerns and maintainability

How to test

cd gateway
go test ./...


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Refactor**
  * Updated Swagger UI rendering to use templated approach instead of hard-coded HTML, improving maintainability and making version management more flexible.

<sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

<!-- greptile_comment -->

<h3>Greptile Summary</h3>


This PR successfully extracts Swagger UI HTML from `main.go` into a dedicated template file (`gateway/templates/swagger.html`) using Go's `embed` package, improving code maintainability and separation of concerns. The implementation adds environment variable configuration for Swagger UI version with regex validation for semantic versioning.

**Key Changes:**
- Extracted inline Swagger UI HTML into embedded template with dynamic version variable
- Added `SWAGGER_UI_VERSION` environment variable with validation (`^\d+\.\d+\.\d+$`) and safe default (5.11.0)
- Template version validation prevents XSS attacks by only allowing semantic version format
- Minor whitespace cleanup across multiple files (trailing spaces and indentation fixes)
- Removed unused `runtime` import from `main.go`

**Issues Found:**
- Missing `Content-Type: text/html` header when executing template - original code explicitly set this header and it should be preserved to ensure proper browser interpretation

<h3>Confidence Score: 3/5</h3>


- PR has one actionable issue: missing Content-Type header for HTML responses which should be added before merging.
- The refactoring is well-structured and the template embedding follows Go best practices. The version validation regex provides good XSS protection. However, the missing Content-Type header is a functional regression from the original code that will cause the /docs endpoint to not be served with proper MIME type, potentially breaking browser rendering. This is a straightforward fix but must be resolved. The whitespace-only changes are clean with no functional impact.
- gateway/main.go - Add Content-Type header to Swagger UI response handler

<h3>Important Files Changed</h3>




| Filename | Overview |
|----------|----------|
| gateway/main.go | Added Swagger UI template embedding with version configuration. Extracted inline HTML into template file with `go:embed`, added regex validation for `SWAGGER_UI_VERSION` env var. Minor whitespace cleanup. The template rendering follows Go best practices with proper error handling. |
| gateway/templates/swagger.html | New template file for Swagger UI with version variable. Simple, clean HTML structure using `{{.Version}}` templating variable. No security issues with embedded content. |
| gateway/cache.go | Whitespace cleanup (removed trailing spaces and fixed indentation on comments). No functional changes. |
| gateway/redis.go | Whitespace cleanup (removed trailing spaces). No functional changes. |

</details>



<h3>Sequence Diagram</h3>

```mermaid
sequenceDiagram
    participant Client
    participant Gateway
    participant goEmbed as go:embed<br/>swagger.html
    participant EnvVar as SWAGGER_UI_VERSION<br/>Environment

    Client->>Gateway: GET /docs
    Gateway->>EnvVar: Read SWAGGER_UI_VERSION
    EnvVar-->>Gateway: Return env value or ""
    Gateway->>Gateway: Validate with regex ^d+.d+.d+$
    Gateway->>Gateway: Use validated or default "5.11.0"
    Gateway->>goEmbed: Load embedded template
    goEmbed-->>Gateway: Return parsed template
    Gateway->>Gateway: Execute template with Version data
    Gateway->>Gateway: Render HTML with dynamic CDN URL
    Gateway-->>Client: HTTP 200<br/>HTML with @{{Version}} substituted<br/>in script/link tags
    Client->>Client: Parse & render HTML<br/>Load Swagger UI from CDN

Context used:

  • Context from dashboard - AGENTS.md (source)

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 19, 2026

Warning

Rate limit exceeded

@Vaishnavi-Iyer67 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 10 minutes and 23 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between ed83c29 and 0e21ae2.

📒 Files selected for processing (2)
  • gateway/main.go
  • gateway/templates/swagger.html

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

This PR refactors Swagger UI documentation rendering from hard-coded inline HTML to an external embedded template file. The /docs route now renders swagger.html with dynamically injected Swagger UI version (default 5.11.0) extracted via helper function.

Changes

Cohort / File(s) Summary
Swagger UI Template Refactoring
gateway/main.go, gateway/templates/swagger.html
Migrates inline Swagger HTML to embedded template system. Adds embed.FS for template loading, getSwaggerUIVersion() helper with regex-based version extraction (default 5.11.0), and /docs route handler that renders swagger.html with version data. Introduces template execution error handling (500 response on failure). New imports: embed, html/template, regexp.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Suggested labels

documentation

Suggested reviewers

  • AnkanMisra

Poem

🐰 A template springs forth from the code,
No longer inline, but modular and bold,
Swagger swirls with version grace,
A cleaner docs, a better place! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: extracting hardcoded Swagger UI HTML from main.go into a dedicated template file.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@gateway/main.go`:
- Around line 141-152: The /docs handler currently calls
swaggerTmpl.Execute(c.Writer, data) which can leave Content-Type unset and may
write partial output before an error; update the handler that uses
getSwaggerUIVersion() and swaggerTmpl to render into a bytes.Buffer first, check
Execute error before writing to the gin.ResponseWriter, set the Content-Type
header to "text/html; charset=utf-8" on the gin.Context (c.Header or
c.Writer.Header) and only write the buffered content to c.Writer if Execute
succeeded, otherwise return a proper 500 response without sending partial HTML.
🧹 Nitpick comments (1)
gateway/templates/swagger.html (1)

1-18: LGTM! Clean Swagger UI template.

The template correctly uses Go's template syntax for version injection, and the version is validated by regex in main.go preventing injection attacks.

Consider these optional enhancements for better HTML standards compliance:

📝 Optional: Add charset and lang attribute
 <!DOCTYPE html>
-<html>
+<html lang="en">
 <head>
+  <meta charset="UTF-8">
   <title>MicroAI Paygate API Docs</title>

@Vaishnavi-Iyer67 Vaishnavi-Iyer67 force-pushed the extract-swagger-ui branch 3 times, most recently from 4518952 to 4b16ea5 Compare January 19, 2026 09:41
@AnkanMisra
Copy link
Owner

@Vaishnavi-Iyer67 what are the uses of this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants