Skip to content

Commit

Permalink
Revert "Feature (#3)"
Browse files Browse the repository at this point in the history
This reverts commit b88a4b4.
  • Loading branch information
viveksahu26 committed Sep 14, 2022
1 parent b88a4b4 commit 687b44d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,60 @@
1) go run main.go

2)
Enter your URL after *http://localhost:8080/sort-url?longURL=*
Enter your URL after *http://localhost:8080/enterLongURL?longURL=*
And let's say your URL is *http://google.com/1346461234567890123456789/get/vivekkumarsahu*
Finally you complete URL will look like:
http://localhost:8080/enterLongURL?longURL=http://google.com/1346461234567890123456789/get/vivekkumarsahu
```http://localhost:8080/enterLongURL?longURL=http://google.com/1346461234567890123456789/get/vivekkumarsahu```

3) You will get output
```
{"originalURL":"http://google.com/1346461234567890123456789/get/vivekkumarsahu","shortURL":"http://localhost:8080/RpP^goh8"}
```
```

# How to run directly from Docker Image !!
## Step:1

`docker pull viveksahu26/my-url-shortner:latest`

## Step2:

```
docker run --name my-short-url1 -d -p 8090:8080 viveksahu26/my-url-shortner:latest
```

## OR


## Step:2

Create container from image:

`docker run -d --name vivek1224 -P viveksahu26/my-url-shortner`

`docker ps`

Under PORT section you will see something like:

0.0.0.0:49154->8080/tcp

Your PORT Number is: 49154
And URL is: "http://google.com/1346461234567890123456789/get/viveksahu26"

Paste it to your Browser :
http://localhost:<replace_by_your_port_no>/short-url?longURL=http:<enter_your_URL>

Example:
Now, copy and paste to your browser:

## Step:3
http://localhost:8090/enterLongURL?longURL=http://google.com/1346461234567890123456789/get/viveksahu26

OR

http://localhost:49154/enterLongURL?longURL=http://google.com/1346461234567890123456789/get/viveksahu26



Docker Image: https://hub.docker.com/repository/docker/viveksahu26/my-url-shortner:v1.0.0

*NOTE*: Do not forget to change port number(49154) and also replace this url(http://google.com/1346461234567890123456789/get/viveksahu26) with your URL.
28 changes: 8 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package main
import (
"fmt"
"net/http"
"time"

"github.com/viveksahu26/url_shortner/src"
)

func handleShortURL(writer http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
}
// get original URL from GET method by quering
originalURL := req.URL.Query().Get("longURL")
fmt.Println("originalURL: ", originalURL)
Expand All @@ -22,27 +20,18 @@ func handleShortURL(writer http.ResponseWriter, req *http.Request) {
// save short and long URL to file
src.SaveInFile(shortURL, originalURL)

host := req.Host
host := "www.simplifyurl.com"

// build Response
resp := src.BuildURLWithResponse(host, shortURL, originalURL)
fmt.Println("response: ", resp)

err := src.RespondWithJSON(writer, 200, resp)
if err != nil {
writer.Write([]byte("Failed to respond with JSON"))
}
}

func healthCheckUp(w http.ResponseWriter, r *http.Request) {
method := r.Method
if method == "GET" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Health of Server is UP & Running..."))
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

const addr = "localhost:8080"

func main() {
Expand All @@ -51,15 +40,14 @@ func main() {
// multiplexer: It provides seperate server interface for each request.
serveMux := http.NewServeMux()
srv := http.Server{
Handler: serveMux,
Addr: addr,
Handler: serveMux,
Addr: addr,
WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second,
}

// handleShortUrl function mapped to /enterLongURL
serveMux.HandleFunc("/sort-url", handleShortURL)

// healthCheckUp function mapped to /health
http.HandleFunc("/health", healthCheckUp)
serveMux.HandleFunc("/enterLongURL", handleShortURL)

// Server Listing on "localhost:8080"
srv.ListenAndServe()
Expand Down
3 changes: 2 additions & 1 deletion src/saveInfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func SaveInFile(shortURL string, longURL string) {

} else {
fmt.Printf("File %s already exist.", fileName)
_, _, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)
_, msg, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)
fmt.Println("\n", msg)

if !fileContainLongURL {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
Expand Down
1 change: 1 addition & 0 deletions src/shortURLGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func GenerateShortURL(longURL string) string {

if isFilePresent {
shortAndLongURLKeyValuePair, _, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)

if fileContainLongURL {
// then retrieve ShortURL from there.
if shorturl, ok := shortAndLongURLKeyValuePair[longURL]; ok {
Expand Down

0 comments on commit 687b44d

Please sign in to comment.