Skip to content

Commit

Permalink
feat(service.url): check url reach (host) if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Apr 4, 2021
1 parent ea8d41d commit 769303c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions service/url/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package url

import (
"encoding/json"
"net"
"net/http"
"os"

Expand Down Expand Up @@ -30,7 +31,7 @@ func CreateURLShortCodeFromRequest(req *http.Request) (string, error) {
// CreateURLShortCode creates a new short code using request.URLInput
// It returns created short code or error if any.
func CreateURLShortCode(input request.URLInput) (string, error) {
if shortCode, err := validateURLInput(input, allowDupeURL()); err != nil {
if shortCode, err := validateURLInput(input); err != nil {
return shortCode, err
}

Expand Down Expand Up @@ -87,18 +88,31 @@ func allowDupeURL() bool {
return os.Getenv("APP_ALLOW_DUPE_URL") == "1"
}

// checkUrlReach checks is app is configured to check if origin url host is reachable
func checkUrlReach() bool {
return os.Getenv("APP_CHECK_URL_REACH") == "1"
}

// validateURLInput validates given request.URLInput
// It returns existing short code for input url if exists and validation error if any.
func validateURLInput(input request.URLInput, allowDup bool) (string, error) {
if err := input.Validate(); err != nil || allowDup {
func validateURLInput(input request.URLInput) (string, error) {
if err := input.Validate(); err != nil {
return "", err
}

if shortCode := getShortCodeByOriginURL(input.URL); shortCode != "" {
return shortCode, common.ErrURLAlreadyShort
if !allowDupeURL() {
if shortCode := getShortCodeByOriginURL(input.URL); shortCode != "" {
return shortCode, common.ErrURLAlreadyShort
}
}

if input.Host == "" || !checkUrlReach() {
return "", nil
}

return "", nil
_, err := net.LookupIP(input.Host)

return "", err
}

// getUniqueShortCode gets unique random string to use as short code
Expand Down

0 comments on commit 769303c

Please sign in to comment.