Skip to content

Commit

Permalink
updating main with simpler readbility
Browse files Browse the repository at this point in the history
Signed-off-by: viveksahu26 <vivekkumarsahu650@gmail.com>
  • Loading branch information
viveksahu26 committed Sep 14, 2022
1 parent 8a05ea2 commit 6c68afc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
40 changes: 22 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
}
4 changes: 2 additions & 2 deletions src/buildURLresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/savetofile.go → src/saveInfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/shortURLGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6c68afc

Please sign in to comment.