Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall authored Dec 13, 2024
1 parent ac9cb2e commit c96c927
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions internal/handlers/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,6 @@ func CheckMyWork(w http.ResponseWriter, r *http.Request) {
"Full Title",
}
urlCheckCache := sync.Map{}

checkURL := func(url string) bool {
if result, ok := urlCheckCache.Load(url); ok {
return result.(bool)
}

client := &http.Client{
Timeout: 10 * time.Second,
}

resp, err := client.Head(url)
if err != nil {
urlCheckCache.Store(url, false)
return false
}
defer resp.Body.Close()

result := resp.StatusCode == http.StatusOK
urlCheckCache.Store(url, result)
return result
}

uploadIds := map[string]bool{}
for rowIndex, row := range csvData[1:] {
for colIndex, col := range row {
Expand Down Expand Up @@ -139,13 +117,13 @@ func CheckMyWork(w http.ResponseWriter, r *http.Request) {
}
if column == "Parent Collection" {
url := fmt.Sprintf("https://preserve.lehigh.edu/node/%d?_format=json", id)
if !checkURL(url) {
if !checkURL(url, urlCheckCache) {

Check failure on line 120 in internal/handlers/check.go

View workflow job for this annotation

GitHub Actions / lint-test

cannot use urlCheckCache (variable of type sync.Map) as *sync.Map value in argument to checkURL

Check failure on line 120 in internal/handlers/check.go

View workflow job for this annotation

GitHub Actions / lint-test

cannot use urlCheckCache (variable of type sync.Map) as *sync.Map value in argument to checkURL
errors[i] = fmt.Sprintf("Could not identify parent collection %d", id)
}
}
if column == "Node ID" {
url := fmt.Sprintf("https://preserve.lehigh.edu/node/%d?_format=json", id)
if !checkURL(url) {
if !checkURL(url, urlCheckCache) {

Check failure on line 126 in internal/handlers/check.go

View workflow job for this annotation

GitHub Actions / lint-test

cannot use urlCheckCache (variable of type sync.Map) as *sync.Map value in argument to checkURL) (typecheck)

Check failure on line 126 in internal/handlers/check.go

View workflow job for this annotation

GitHub Actions / lint-test

cannot use urlCheckCache (variable of type sync.Map) as *sync.Map value in argument to checkURL (typecheck)
errors[i] = fmt.Sprintf("Could not find node ID %d", id)
}
}
Expand Down Expand Up @@ -384,6 +362,27 @@ func ColumnValue(value string, header, row []string) string {
return row[i]
}

func checkURL(url string, cache *sync.Map) bool {
if result, ok := cache.Load(url); ok {
return result.(bool)
}

client := &http.Client{
Timeout: 10 * time.Second,
}

resp, err := client.Head(url)
if err != nil {
cache.Store(url, false)
return false
}
defer resp.Body.Close()

result := resp.StatusCode == http.StatusOK
cache.Store(url, result)
return result
}

func validRelators() []string {
// todo: fetch this from field_linked_agent config
return []string{
Expand Down

0 comments on commit c96c927

Please sign in to comment.