Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #5

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 3 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,12 @@
1) go run main.go

2)
Enter your URL after *http://localhost:8080/enterLongURL?longURL=*
Enter your URL after *http://localhost:8080/sort-url?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.
```
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ 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 @@ -20,18 +22,22 @@ func handleShortURL(writer http.ResponseWriter, req *http.Request) {
// save short and long URL to file
src.SaveInFile(shortURL, originalURL)

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

// 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) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Health of Server is UP & Running..."))
}

const addr = "localhost:8080"

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

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

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

// Server Listing on "localhost:8080"
srv.ListenAndServe()
Expand Down
16 changes: 16 additions & 0 deletions src/buildURLresponse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package src

import (
"testing"

"gotest.tools/assert"
)

func TestBuildResonse(t *testing.T) {
expected := BuildURLWithResponse("localhost:8080", "duj3P^+d*C", "http://viveksahu.com")
actual := &URLResponse{
OriginalURL: "http://viveksahu.com",
ShortURL: "http://localhost:8080/duj3P^+d*C",
}
assert.Equal(t, expected.ShortURL, actual.ShortURL)
}
3 changes: 1 addition & 2 deletions src/saveInfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func SaveInFile(shortURL string, longURL string) {

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

if !fileContainLongURL {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
Expand Down
1 change: 0 additions & 1 deletion src/shortURLGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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