~netlandish/links

26c64411ee4c77c5a35d7c7f4a6d43e44220ce65 — Peter Sanchez a month ago 3a84af9
Add 30 second timeout to Web Archive request call
1 files changed, 14 insertions(+), 1 deletions(-)

M helpers.go
M helpers.go => helpers.go +14 -1
@@ 19,6 19,7 @@ import (
	"regexp"
	"strconv"
	"strings"
	"time"

	"git.sr.ht/~emersion/gqlclient"
	"github.com/99designs/gqlgen/graphql"


@@ 614,10 615,22 @@ func GetDisabledElementsByOrg(ctx context.Context, org *models.Organization) (*C
}

func ArchiveURLSnapshot(ctx context.Context, orgLink *models.OrgLink) error {
	resp, err := http.Get(fmt.Sprintf("https://web.archive.org/save/%s", orgLink.URL))
	httpClient := &http.Client{
		Timeout: time.Duration(30) * time.Second,
	}
	req, err := http.NewRequest(
		"GET",
		fmt.Sprintf("https://web.archive.org/save/%s", orgLink.URL),
		nil,
	)
	if err != nil {
		return err
	}
	resp, err := httpClient.Do(req)
	if err != nil {
		return err
	}

	if resp.StatusCode == http.StatusOK && resp.Request.URL.String() != "" {
		orgLink.ArchiveURL = resp.Request.URL.String()
		return orgLink.Store(ctx)