🛠️ Refactor: modularize API handler into internal packages#142
🛠️ Refactor: modularize API handler into internal packages#142
Conversation
Decomposed the monolithic `api/index.go` into domain-specific internal packages to improve maintainability, testability, and separation of concerns. - `internal/transport`: HTTP client configuration and SSRF protection (`NewSafeClient`). - `internal/article`: Article fetching and parsing logic (`Fetch`). - `internal/request`: Request parsing, validation, and URL reconstruction (`NormalizeURL`, `ReconstructURL`, `GetFormat`). - `internal/formatter`: Output formatting and rendering (`Render`, `Template`). - Updated `api/index.go` to serve as a clean orchestration layer using these packages. - Migrated and split tests into their respective package test files. - Added integration test in `api/index_test.go` to verify wiring. This refactoring adheres to the Single Responsibility Principle and makes the codebase easier to navigate and extend. Co-authored-by: lucasew <15693688+lucasew@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Summary of ChangesHello @lucasew, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the application's API handler by breaking down a monolithic Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request does a great job of refactoring the monolithic API handler into well-defined internal packages. The new structure improves separation of concerns, testability, and maintainability. The logic has been moved correctly, and the corresponding tests have been updated, ensuring no regressions. I have one suggestion to improve the randomness of user-agent selection, but overall, this is a solid improvement.
| func getRandomUserAgent() string { | ||
| return userAgentPool[rand.Intn(len(userAgentPool))] | ||
| } |
There was a problem hiding this comment.
Using math/rand can lead to predictable user-agent selection if not seeded properly, which can be tricky in serverless environments and with Go versions before 1.20. For better randomness and to avoid any ambiguity with seeding, it's recommended to use the cryptographically secure crypto/rand package.
This change will require updating your imports to include "crypto/rand", "log", and "math/big", while removing "math/rand".
func getRandomUserAgent() string {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(userAgentPool))))
if err != nil {
log.Printf("crypto/rand failed: %v; falling back to first user agent", err)
return userAgentPool[0]
}
return userAgentPool[n.Int64()]
}
This PR refactors the
api/index.gofile by extracting logic into a modularinternal/directory structure.Changes:
internal/transportfor HTTP client and SSRF logic.internal/articlefor fetching and parsing logic.internal/requestfor request utility functions (URL normalization, format detection, LLM detection).internal/formatterfor response formatting (HTML, Markdown, JSON, etc.).api/index.goto import and use these packages.Justification:
Fetchaccepts a client).Verification:
go test ./...to ensure all tests pass.mise run cito verify linting and formatting.PR created automatically by Jules for task 7015727293619554448 started by @lucasew