Skip to content

Conversation

@AdheipSingh
Copy link
Collaborator

@AdheipSingh AdheipSingh commented May 13, 2025

Summary by CodeRabbit

  • New Features
    • Updating the domain now also updates URLs in the 404 error page, ensuring consistency when the domain changes.
  • Bug Fixes
    • Improved handling of domain updates to prevent outdated links on the 404 error page.

@coderabbitai
Copy link

coderabbitai bot commented May 13, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The SetDomain function in hossted/setDomain.go was updated to call a new helper function, Update404, after updating the MOTD file. The new Update404 function locates and edits the 404.html error page, replacing occurrences of the old domain with the new domain in relevant URLs. Error handling and logging were implemented for various failure scenarios.

Changes

File(s) Change Summary
hossted/setDomain.go Added Update404 function to update domain references in 404.html; integrated its call into SetDomain.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SetDomain
    participant UpdateMOTD
    participant Update404

    User->>SetDomain: Invoke with existingDomain, newDomain
    SetDomain->>UpdateMOTD: Update MOTD file
    SetDomain->>Update404: Update 404.html with new domain
    Update404-->>SetDomain: Return success or error
    SetDomain-->>User: Return success or error
Loading

Poem

In the warren of code, a new path we pave,
Domains in 404s now get a wave.
With clever search and gentle replace,
Old links hop out, new ones take their place.
A bunny’s delight—no errors to fear,
Our pages are fresh, the message is clear!
🐇✨


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@AdheipSingh AdheipSingh changed the base branch from main to dev May 13, 2025 08:39
Copy link

@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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 318b50d and 207a85a.

📒 Files selected for processing (1)
  • hossted/setDomain.go (2 hunks)
🔇 Additional comments (3)
hossted/setDomain.go (3)

135-139: Appropriate extension of domain update functionality

Good addition of the 404.html update functionality with proper error handling consistent with the rest of the codebase. The error wrapping with context is well implemented.


416-426: LGTM!

The file reading setup follows Go best practices with proper error handling and deferred closing of the file.


456-477: LGTM!

The file writing and completion logic is well implemented. The function properly handles the case when no domain instances were found and provides clear status messages.

Comment on lines +399 to +414
// Update404 searches for the existing domain in the 404.html file and replaces it with the new domain.
func Update404(existingDomain, newDomain string) error {
// Get the project name dynamically
projectName, err := getProjectName()
if err != nil {
return fmt.Errorf("error getting project name: %s", err)
}

// Construct the path to the 404.html file
error404FilePath := filepath.Join("/opt", projectName, "hossted", "error-pages", "404.html")

// Check if the file exists
if _, err := os.Stat(error404FilePath); os.IsNotExist(err) {
fmt.Printf("404.html file not found at %s, skipping update\n", error404FilePath)
return nil
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add check for empty project name

The function correctly handles a non-existent 404.html file, but it doesn't handle the case where getProjectName() returns an empty string without an error (which can happen based on lines 272-275).

// Update404 searches for the existing domain in the 404.html file and replaces it with the new domain.
func Update404(existingDomain, newDomain string) error {
	// Get the project name dynamically
	projectName, err := getProjectName()
	if err != nil {
		return fmt.Errorf("error getting project name: %s", err)
	}

+	// Check if project name is empty
+	if projectName == "" {
+		fmt.Println("Project name is empty, skipping 404.html update")
+		return nil
+	}
+
	// Construct the path to the 404.html file
	error404FilePath := filepath.Join("/opt", projectName, "hossted", "error-pages", "404.html")

Comment on lines +427 to +455
for scanner.Scan() {
line := scanner.Text()

// Check if the line contains the iframe src with url parameter
if strings.Contains(line, "url=https://") {
// Find the url parameter value and replace the domain
// Look for pattern like url=https://domain.example.com
start := strings.Index(line, "url=https://")
if start != -1 {
// Find the end of the domain (either & or " or end of string)
urlStart := start + len("url=https://")
end := urlStart
for end < len(line) && line[end] != '&' && line[end] != '"' && line[end] != ' ' {
end++
}

if end > urlStart {
oldURL := line[urlStart:end]
// Check if this URL contains our existing domain
if strings.Contains(oldURL, existingDomain) {
// Replace the domain while preserving the URL structure
newURL := strings.Replace(oldURL, existingDomain, newDomain, 1)
line = line[:urlStart] + newURL + line[end:]
updated = true
}
}
}
}

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve URL parsing and domain replacement logic

The current implementation has some limitations when parsing and replacing domain names in URLs:

  1. It only handles "url=https://" and not "url=http://" links
  2. The URL boundary detection might miss some valid URL formats
  3. It only replaces the first occurrence of the domain in a URL
	for scanner.Scan() {
		line := scanner.Text()

-		// Check if the line contains the iframe src with url parameter
-		if strings.Contains(line, "url=https://") {
+		// Check if the line contains the iframe src with url parameter (both http and https)
+		if strings.Contains(line, "url=http") {
			// Find the url parameter value and replace the domain
-			// Look for pattern like url=https://domain.example.com
-			start := strings.Index(line, "url=https://")
+			// Look for pattern like url=https://domain.example.com or url=http://domain.example.com
+			start := -1
+			if strings.Contains(line, "url=https://") {
+				start = strings.Index(line, "url=https://")
+			} else if strings.Contains(line, "url=http://") {
+				start = strings.Index(line, "url=http://")
+			}
+			
			if start != -1 {
				// Find the end of the domain (either & or " or end of string)
-				urlStart := start + len("url=https://")
+				protocol := "http://"
+				if strings.Contains(line[start:start+12], "https") {
+					protocol = "https://"
+				}
+				urlStart := start + len("url=") + len(protocol)
				end := urlStart
				for end < len(line) && line[end] != '&' && line[end] != '"' && line[end] != ' ' {
					end++
				}

				if end > urlStart {
					oldURL := line[urlStart:end]
					// Check if this URL contains our existing domain
-					if strings.Contains(oldURL, existingDomain) {
+					// More precise check to ensure we're replacing domains, not substrings
+					domainStart := strings.Index(oldURL, existingDomain)
+					if domainStart != -1 && (domainStart == 0 || oldURL[domainStart-1] == '.' || oldURL[domainStart-1] == '/') {
						// Replace the domain while preserving the URL structure
						newURL := strings.Replace(oldURL, existingDomain, newDomain, 1)
						line = line[:urlStart] + newURL + line[end:]
						updated = true
					}
				}

@liorkesos liorkesos merged commit 21b4c95 into dev Jun 8, 2025
3 checks passed
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.

3 participants