From 6c68afcc5be4f8fd2b007bba1f8623d479285035 Mon Sep 17 00:00:00 2001 From: viveksahu26 Date: Wed, 14 Sep 2022 21:01:13 +0530 Subject: [PATCH 1/2] updating main with simpler readbility Signed-off-by: viveksahu26 --- main.go | 40 +++++++++++++++------------- src/buildURLresponse.go | 4 +-- src/{savetofile.go => saveInfile.go} | 3 ++- src/shortURLGenerator.go | 11 ++++---- 4 files changed, 32 insertions(+), 26 deletions(-) rename src/{savetofile.go => saveInfile.go} (90%) diff --git a/main.go b/main.go index c62aa60..396aa41 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,14 @@ package main import ( - "encoding/json" "fmt" "net/http" + "time" "github.com/viveksahu26/url_shortner/src" ) -func handleShortUrl(writer http.ResponseWriter, req *http.Request) { +func handleShortURL(writer http.ResponseWriter, req *http.Request) { // get original URL from GET method by quering originalURL := req.URL.Query().Get("longURL") fmt.Println("originalURL: ", originalURL) @@ -18,33 +18,37 @@ func handleShortUrl(writer http.ResponseWriter, req *http.Request) { fmt.Println("shortURL: ", shortURL) // save short and long URL to file - src.SaveToFile(shortURL, originalURL) + src.SaveInFile(shortURL, originalURL) - host := req.Host + host := "www.simplifyurl.com" // build Response - resp := src.BuildURLResponse(host, shortURL, originalURL) + resp := src.BuildURLWithResponse(host, shortURL, originalURL) fmt.Println("response: ", resp) - // Converting response JSON form - jsonBytes, err := json.Marshal(resp) + err := src.RespondWithJSON(writer, 200, resp) if err != nil { - writer.Write([]byte("Failed to generate response")) + writer.Write([]byte("Failed to respond with JSON")) } - - writer.Header().Set("Content-Type", "application/json") - writer.WriteHeader(http.StatusOK) - writer.Write(jsonBytes) } +const addr = "localhost:8080" + func main() { - fmt.Println("URL Shorten Service") + fmt.Println("URL Shorten Service Starts ...") + + // 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, + } // handleShortUrl function mapped to /short-url - http.HandleFunc("/short-url", handleShortUrl) + serveMux.HandleFunc("/short-url", handleShortURL) - err := http.ListenAndServe(":8080", nil) - if err != nil { - panic(err) - } + // Server Listing on "localhost:8080" + srv.ListenAndServe() } diff --git a/src/buildURLresponse.go b/src/buildURLresponse.go index 52a5601..c9043bb 100644 --- a/src/buildURLresponse.go +++ b/src/buildURLresponse.go @@ -5,8 +5,8 @@ type URLResponse struct { ShortURL string `json:"shortURL"` } -// It returns short URL as response -func BuildURLResponse(host string, shortURL string, originalURL string) *URLResponse { +// builds shortURL as response +func BuildURLWithResponse(host string, shortURL string, originalURL string) *URLResponse { resp := &URLResponse{ OriginalURL: originalURL, ShortURL: "http://" + host + "/" + shortURL, diff --git a/src/savetofile.go b/src/saveInfile.go similarity index 90% rename from src/savetofile.go rename to src/saveInfile.go index c09c13b..557df27 100644 --- a/src/savetofile.go +++ b/src/saveInfile.go @@ -8,7 +8,8 @@ import ( "strings" ) -func SaveToFile(shortURL string, longURL string) { +// Saving LongURL corresponding to ShortURL +func SaveInFile(shortURL string, longURL string) { fileName := "url.properties" prop := shortURL + "=" + longURL if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) { diff --git a/src/shortURLGenerator.go b/src/shortURLGenerator.go index 9874f4d..f6c83fb 100644 --- a/src/shortURLGenerator.go +++ b/src/shortURLGenerator.go @@ -9,11 +9,12 @@ type Result struct { ShortURL string `json:"shortURL"` } +// Generate Random URL of 10 characters func GenerateShortURL() string { - letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789%$#@!*^+") - b := make([]rune, 8) - for i := range b { - b[i] = letterRunes[rand.Intn(len(letterRunes))] + allCharacters := []rune("%$#@!*^+0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + randomChar := make([]rune, 10) + for i := range randomChar { + randomChar[i] = allCharacters[rand.Intn(len(allCharacters))] } - return string(b) + return string(randomChar) } From 45cea2d704803188b5835c570b2a63586e78b14d Mon Sep 17 00:00:00 2001 From: viveksahu26 Date: Wed, 14 Sep 2022 21:01:33 +0530 Subject: [PATCH 2/2] respond with JSON format Signed-off-by: viveksahu26 --- src/respondWithJSON.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/respondWithJSON.go diff --git a/src/respondWithJSON.go b/src/respondWithJSON.go new file mode 100644 index 0000000..448abdc --- /dev/null +++ b/src/respondWithJSON.go @@ -0,0 +1,18 @@ +package src + +import ( + "encoding/json" + "net/http" +) + +// Responding with JSON format +func RespondWithJSON(w http.ResponseWriter, status int, resp interface{}) error { + response, err := json.Marshal(resp) + if err != nil { + return err + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + w.Write(response) + return nil +}