Skip to content

Commit

Permalink
added functionality to retrieve existing longURL from shortURL (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>

Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>
  • Loading branch information
viveksahu26 committed Sep 16, 2022
1 parent 2b6041a commit bbb51df
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
**URL shortner endpoint - returns a short URL**

It is a service which takes long URL from the user and returns Short URL.
It provides 3 service.

1) health checks: it ensures that app is running.
Example: `http://localhost:8080/health`

2) short url : it takes long url and returns short url
Example: `http://localhost:8080/short-url?longURL=http://google.com/1346461234567890123456789/get/viveksahu26`

3) long url : it takes short url and returns long url
Example: `http://localhost:8080/long-url?sortURL=xtNFxaBwCG`

## How URL Shortner Works !!
It replaces long URL by randomly generated characters of size 10.
Expand Down Expand Up @@ -33,7 +43,7 @@ The advantage of saving Short URL and Long URL in the file is to retrieved that

http://localhost:8080/health

5) Go to browser:
5) Convert longURL into ShortURL:

Enter your URL after *http://localhost:8080/sort-url?longURL=*

Expand All @@ -43,12 +53,23 @@ The advantage of saving Short URL and Long URL in the file is to retrieved that

http://localhost:8080/short-url?longURL=http://google.com/1346461234567890123456789/get/viveksahu26

3) You will get output
http://localhost:8080/long-url?sortURL=xtNFxaBwCG

You will get output

```
{"originalURL":"http://google.com/1346461234567890123456789/get/viveksahu26","shortURL":"http://localhost:8080/xtNFxaBwCG"}
```

6) Convert ShortURL into LongURL:

Let's say shortURL=xtNFxaBwCG
To get it's longURL enter below URL in the browser
http://localhost:8080/long-url?sortURL=xtNFxaBwCG

You will get return value as:
http://google.com/1346461234567890123456789/get/viveksahu26

## Steps to reproduce Using Docker Image
### Step:1
Docker Image: viveksahu26/urlshortner:stable
Expand Down
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import (
"github.com/viveksahu26/url_shortner/src"
)

// http://localhost:8080/long-url?sortURL=xtNFxaBwCG
func handleLongURL(writer http.ResponseWriter, req *http.Request) {
// Procedure:
// Finally got Complete url
// Retrieve shortURL from it by Querying it.
// Once retrieve, check check in file whether it contains short url or not.
// If file exist, then read that file:
// 1. loop over it's content.
// 2. Check line by line,
// 3. short URL is present in that line or not.
// 4. If present retun map and true boolean
// 5. Else retuen false and empty value

if req.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
} else {
// get original--> shortURL
originalURL := req.URL.Query().Get("sortURL")
fmt.Println("originalURL: ", originalURL)

longURL := src.CheckWhetherShortURLisPresentORNot(originalURL)
fmt.Println("longURL: ", longURL)

if longURL == "" {
writer.Write([]byte("<h1>HSorry, No corresponding longURL is present to the shortURL. !!</h1>"))
}
writer.Write([]byte(longURL))
}
}

func handleShortURL(writer http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
Expand Down Expand Up @@ -66,6 +96,9 @@ func main() {
// /short-url endpoint is mapped to handleShortURL
http.HandleFunc("/short-url", handleShortURL)

// /long-url endpoint is mapped to handleLongURL
http.HandleFunc("/long-url", handleLongURL)

// Server Listening on localhost:8080
err := http.ListenAndServe(":"+port, nil)
if err != nil {
Expand Down
30 changes: 28 additions & 2 deletions src/saveInfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,53 @@ func IsFileExist(fileName string) (bool, error) {
return true, nil
}

func IsLongURLPresentInFile(fileName, longURL string) (map[string]string, string, bool) {
// It can check whether LongURL is present in the file or not.
// As well as also it can check whether shortURL is present in the file or not.
func IsLongURLPresentInFile(fileName, url string) (map[string]string, string, bool) {
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalln(err)
}
fileContent := strings.Split(string(fileBytes), "\n")

incommingLongURLisShortURL := false
if len(url) == 10 {
// way of differentialting bewtween URL: whether the argument url
// is actual longURL or shortURL
// ShortURL is of length 10

// yes, it's a shortURL
incommingLongURLisShortURL = true
}
fmt.Println("incommingLongURLisShortURL: ", incommingLongURLisShortURL)

fileContainLongURL := false
var message string
ShortAndLongURLValue := make(map[string]string)

for _, line := range fileContent {
totalURL := strings.Split(line, "=")
if len(totalURL) == 2 {
// if Long URL, then store key as LongURL and value as ShortURL in map
ShortAndLongURLValue[totalURL[1]] = totalURL[0]

// if short URL, then store key as shortURL and value as LongURL in map
if incommingLongURLisShortURL {
fmt.Println("storing map values for shortURL as key and longURL as value")
ShortAndLongURLValue[totalURL[0]] = totalURL[1]
}
}

if strings.HasSuffix(line, longURL) {
if strings.HasSuffix(line, url) && !incommingLongURLisShortURL {
message = fmt.Sprintln("Yes, URL is already present in the file")
fileContainLongURL = true
return ShortAndLongURLValue, message, fileContainLongURL
} else if strings.HasPrefix(line, url) && incommingLongURLisShortURL {
message = fmt.Sprintln("Yes, URL is already present in the file")
fileContainLongURL = true
return ShortAndLongURLValue, message, fileContainLongURL
}

message = fmt.Sprintln("URL is not present in the file.")
}
fmt.Println("\n", message)
Expand Down
28 changes: 28 additions & 0 deletions src/shortURLGenerator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package src

import (
"fmt"
"math/rand"
)

Expand Down Expand Up @@ -33,3 +34,30 @@ func GenerateShortURL(longURL string) string {
}
return string(randomChar)
}

// If file exist, then read that file, otherwise return empty:
// 1. loop over it's content.
// 2. Check line by line,
// 3. that short URL is present in that line or not.
// 4. retun.

// Check whether Short URL is present or not.
// If present returns shorturl, else empty string
func CheckWhetherShortURLisPresentORNot(shorturl string) string {
fmt.Println("Yes, you are inside CheckWhetherShortURLisPresentORNot")

fileName := "url.properties"
isFilePresent, _ := IsFileExist(fileName)

if isFilePresent {
shortAndLongURLKeyValuePair, _, fileContainShortURL := IsLongURLPresentInFile(fileName, shorturl)
if fileContainShortURL {
// then retrieve ShortURL from there.
if longurl, ok := shortAndLongURLKeyValuePair[shorturl]; ok {
return longurl
}
}
}

return ""
}

0 comments on commit bbb51df

Please sign in to comment.