Skip to content

Commit

Permalink
feat: refactor struct
Browse files Browse the repository at this point in the history
  • Loading branch information
robertstojs committed Jan 18, 2024
1 parent 76c0cc1 commit 237bf84
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

type URLMapping struct {
ShortURL string `json:"shortURL"`
OriginalURL string `json:"originalURL"`
RegexPattern string `json:"regexPattern,omitempty"`
RedirectPath string `json:"redirectPath"`
DestinationURL string `json:"destinationURL"`
RegexPattern string `json:"regexPattern,omitempty"`
}

var (
Expand Down Expand Up @@ -54,9 +54,9 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
requestedURL := r.URL.Path[1:]

for _, mapping := range urlMappings {
if mapping.ShortURL == requestedURL {
http.Redirect(w, r, mapping.OriginalURL, http.StatusFound)
logMessage := fmt.Sprintf("Redirected '%s' to '%s'", requestedURL, mapping.OriginalURL)
if mapping.RedirectPath == requestedURL {
http.Redirect(w, r, mapping.DestinationURL, http.StatusFound)
logMessage := fmt.Sprintf("Redirected '%s' to '%s'", requestedURL, mapping.DestinationURL)
log.Println(logMessage)
sysLogger.Info(logMessage)
return
Expand All @@ -71,8 +71,8 @@ func redirectHandler(w http.ResponseWriter, r *http.Request) {
continue
}
if matched {
http.Redirect(w, r, mapping.OriginalURL, http.StatusFound)
logMessage := fmt.Sprintf("Redirected via route '%s' to '%s' using pattern '%s'", requestedURL, mapping.OriginalURL, mapping.RegexPattern)
http.Redirect(w, r, mapping.DestinationURL, http.StatusFound)
logMessage := fmt.Sprintf("Redirected via route '%s' to '%s' using pattern '%s'", requestedURL, mapping.DestinationURL, mapping.RegexPattern)
log.Println(logMessage)
sysLogger.Info(logMessage)
return
Expand Down

0 comments on commit 237bf84

Please sign in to comment.