Build a simple service to power the References on articles
The frontend sends a link
The service makes a request to the link and returns the following:
Title
SEO image
Short description
Thought process
I'll be trying a different approach this time around. Rather than spending time doing a lot of research, I'll come up with a quick solution first, then research on areas improvements. Here's the quick solution:
Make a request to the specified endpoint
Check for 2xx status code
Parse HTML document
Return the parsed content to the client
Implementation
I need an endpoint to make a request to the specified URL and return the parsed content. For start, I need two functions:
FetchPageDetails(): An HTTP handler
ParseHTML(): An internal function that processes the result of the HTTP request
Project setup
Create a main.go file in the root directory
Create bookmark_test.go and bookmark.go as well
Fetching the HTML
Using TDD,
Parsing the HTML
Write the test:
This gives an insight into the implmentation of the feature. I'll need a HTML parser that allows me fetch the details needed in the frontend, which are:
Title
Description
ImageURL (optional)
I found GoQuery, which built on top of the net/html library, to handle the HTML parsing:
With GoQuery installed, I can work on the parsing logic:
Refactoring
With the parsing logic in place, I can now refactor the fetch test and finalise the function implementation:
Tying it all up
Create the main() function in the main.go file
Add the router
Questions
How do I handle pages that have anti-bot?
How should I handle missing og:image?
Where should I deploy? Coolify? Or a general cloud provider?
How should persisitency be handled? DB or Cache? (not needed now)
For the frontend, should the bookmark be loaded at build time or runtime?