Skip to content

feat(endpoints): Added new endpoints#16

Open
hammad-afzall wants to merge 6 commits into
mainfrom
feat/new-endpoints
Open

feat(endpoints): Added new endpoints#16
hammad-afzall wants to merge 6 commits into
mainfrom
feat/new-endpoints

Conversation

@hammad-afzall
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown
Member

@lissy93 lissy93 left a comment

Choose a reason for hiding this comment

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

Thanks for this! :)
I'll do a proper review later this weekend...

Functionality looks all good, might be room to improve the structure of the code, implement some best practices and make it a bit more efficient. Looks like tests are missing too.

Error handling too, there are instances where errors aren't getting caught

Comment thread handlers/archives.go Outdated
func getWaybackData(url string) (map[string]interface{}, error) {
cdxUrl := fmt.Sprintf("%s?url=%s&output=json&fl=timestamp,statuscode,digest,length,offset", archiveAPIURL, url)

resp, err := http.Get(cdxUrl)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sometimes requests just hang, so we should include a timeout to prevent that

Comment thread handlers/archives.go Outdated
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
urlParam := r.URL.Query().Get("url")
if urlParam == "" {
http.Error(w, "missing 'url' parameter", http.StatusBadRequest)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't forget, if there's an error anywhere, we need to still respond with JSON, like { error: "Missing 'url' parameter" }. Same goes for the other error responses below.

Comment thread handlers/archives.go
// Remove the header row
data = data[1:]

firstScan, err := convertTimestampToDate(data[0][0])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to do a length check, before accessing data[0] and data[len(data)-1] to avoid potential panics?

Comment thread handlers/archives.go
Comment on lines +15 to +41
func convertTimestampToDate(timestamp string) (time.Time, error) {
year, err := strconv.Atoi(timestamp[0:4])
if err != nil {
return time.Time{}, err
}
month, err := strconv.Atoi(timestamp[4:6])
if err != nil {
return time.Time{}, err
}
day, err := strconv.Atoi(timestamp[6:8])
if err != nil {
return time.Time{}, err
}
hour, err := strconv.Atoi(timestamp[8:10])
if err != nil {
return time.Time{}, err
}
minute, err := strconv.Atoi(timestamp[10:12])
if err != nil {
return time.Time{}, err
}
second, err := strconv.Atoi(timestamp[12:14])
if err != nil {
return time.Time{}, err
}
return time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC), nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a small thing, but I'd probably put the minuteIndex, hourIndex, dayIndex, etc as variables, to avoid magic numbers. Because, for example hour, err := strconv.Atoi(timestamp[8:10]) is a bit hard to read.

const (
	yearIndex          = 0
	monthIndex         = 4
	dayIndex           = 6
	hourIndex          = 8
	minuteIndex        = 10
	secondIndex        = 12
	timestampLength    = 14
	averagePageSizeDiv = 100
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The golang way :)

mask := "20060102150405"
return time.Parse(mask, timestamp)

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 9, 2024

Codecov Report

Attention: Patch coverage is 1.57687% with 749 lines in your changes missing coverage. Please review.

Flag Coverage Δ
unittests 35.04% <1.57%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
server/server.go 88.00% <100.00%> (ø)
handlers/ssl.go 0.00% <0.00%> (ø)
handlers/tech-stack.go 0.00% <0.00%> (ø)
handlers/txt-records.go 0.00% <0.00%> (ø)
handlers/screenshot.go 0.00% <0.00%> (ø)
handlers/robots-txt.go 0.00% <0.00%> (ø)
handlers/security-txt.go 0.00% <0.00%> (ø)
handlers/status.go 0.00% <0.00%> (ø)
handlers/sitemap.go 0.00% <0.00%> (ø)
handlers/whois.go 0.00% <0.00%> (ø)
... and 3 more

Comment thread handlers/archives.go Outdated
}

func getScanFrequency(firstScan, lastScan time.Time, totalScans, changeCount int) map[string]float64 {
formatToTwoDecimal := func(num float64) float64 {
Copy link
Copy Markdown
Collaborator

@kynrai kynrai Jun 10, 2024

Choose a reason for hiding this comment

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

can use

fmt.Sprintf("%.2f", num)

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