From 9ed8499c39d76f8adb24de9c3c774a8292c0c75b Mon Sep 17 00:00:00 2001 From: Vivek Kumar Sahu Date: Fri, 9 Feb 2024 23:57:52 +0530 Subject: [PATCH] refractor packages Signed-off-by: Vivek Kumar Sahu --- .../cli/response}/buildURLresponse.go | 2 +- .../cli/response}/buildURLresponse_test.go | 7 +- .../cli/response}/respondWithJSON.go | 2 +- .../url_shortner/cli/save}/saveInfile.go | 2 +- cmd/url_shortner/cli/short/short.go | 156 ++++++++++++++++++ .../cli/short}/shortURLGenerator.go | 12 +- cmd/url_shortner/main.go | 54 ++++++ src/shortner.go | 110 ------------ 8 files changed, 224 insertions(+), 121 deletions(-) rename {src => cmd/url_shortner/cli/response}/buildURLresponse.go (95%) rename {src => cmd/url_shortner/cli/response}/buildURLresponse_test.go (51%) rename {src => cmd/url_shortner/cli/response}/respondWithJSON.go (95%) rename {src => cmd/url_shortner/cli/save}/saveInfile.go (99%) create mode 100644 cmd/url_shortner/cli/short/short.go rename {src => cmd/url_shortner/cli/short}/shortURLGenerator.go (79%) create mode 100644 cmd/url_shortner/main.go delete mode 100644 src/shortner.go diff --git a/src/buildURLresponse.go b/cmd/url_shortner/cli/response/buildURLresponse.go similarity index 95% rename from src/buildURLresponse.go rename to cmd/url_shortner/cli/response/buildURLresponse.go index c9043bb..82685fe 100644 --- a/src/buildURLresponse.go +++ b/cmd/url_shortner/cli/response/buildURLresponse.go @@ -1,4 +1,4 @@ -package src +package response type URLResponse struct { OriginalURL string `json:"originalURL"` diff --git a/src/buildURLresponse_test.go b/cmd/url_shortner/cli/response/buildURLresponse_test.go similarity index 51% rename from src/buildURLresponse_test.go rename to cmd/url_shortner/cli/response/buildURLresponse_test.go index b0fe5e6..05a169b 100644 --- a/src/buildURLresponse_test.go +++ b/cmd/url_shortner/cli/response/buildURLresponse_test.go @@ -1,14 +1,15 @@ -package src +package response_test import ( "testing" + "github.com/viveksahu26/url_shortner/cmd/url_shortner/cli/response" "gotest.tools/assert" ) func TestBuildResonse(t *testing.T) { - expected := BuildURLWithResponse("localhost:8080", "duj3P^+d*C", "http://viveksahu.com") - actual := &URLResponse{ + expected := response.BuildURLWithResponse("localhost:8080", "duj3P^+d*C", "http://viveksahu.com") + actual := response.URLResponse{ OriginalURL: "http://viveksahu.com", ShortURL: "http://localhost:8080/duj3P^+d*C", } diff --git a/src/respondWithJSON.go b/cmd/url_shortner/cli/response/respondWithJSON.go similarity index 95% rename from src/respondWithJSON.go rename to cmd/url_shortner/cli/response/respondWithJSON.go index 448abdc..216ab00 100644 --- a/src/respondWithJSON.go +++ b/cmd/url_shortner/cli/response/respondWithJSON.go @@ -1,4 +1,4 @@ -package src +package response import ( "encoding/json" diff --git a/src/saveInfile.go b/cmd/url_shortner/cli/save/saveInfile.go similarity index 99% rename from src/saveInfile.go rename to cmd/url_shortner/cli/save/saveInfile.go index 72781a0..ca8bc5a 100644 --- a/src/saveInfile.go +++ b/cmd/url_shortner/cli/save/saveInfile.go @@ -1,4 +1,4 @@ -package src +package save import ( "errors" diff --git a/cmd/url_shortner/cli/short/short.go b/cmd/url_shortner/cli/short/short.go new file mode 100644 index 0000000..597b3af --- /dev/null +++ b/cmd/url_shortner/cli/short/short.go @@ -0,0 +1,156 @@ +package short + +import ( + "fmt" + "net/http" + + "github.com/viveksahu26/url_shortner/cmd/url_shortner/cli/response" + "github.com/viveksahu26/url_shortner/cmd/url_shortner/cli/save" +) + +// http://localhost:8080/long-url?sortURL=xtNFxaBwCG + +// This was earlier main.go program. + +// import ( +// "fmt" +// "net/http" +// "os" + +// "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 := CheckWhetherShortURLisPresentORNot(originalURL) + fmt.Println("longURL: ", longURL) + + if longURL == "" { + writer.Write([]byte("

HSorry, No corresponding longURL is present to the shortURL. !!

")) + } + writer.Write([]byte(longURL)) + } +} + +func HandleShortURL(writer http.ResponseWriter, req *http.Request) { + if req.Method != "GET" { + writer.WriteHeader(http.StatusMethodNotAllowed) + } else { + // get original URL from GET method by quering + originalURL := req.URL.Query().Get("longURL") + fmt.Println("originalURL: ", originalURL) + + // generate random shortURL + shortURL := GenerateShortURL(originalURL) + fmt.Println("shortURL: ", shortURL) + + // save short and long URL to file + save.SaveInFile(shortURL, originalURL) + + host := req.Host + + // build Response + resp := response.BuildURLWithResponse(host, shortURL, originalURL) + + err := response.RespondWithJSON(writer, 200, resp) + if err != nil { + writer.Write([]byte("

Failed to respond with JSON

")) + } + } +} + +func HealthCheckUp(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/health" { + http.Error(w, "404 not found", http.StatusNotFound) + return + } + + if r.Method != "GET" { + http.Error(w, "Method other than GET not Supported...", http.StatusNotFound) + return + } + + w.Write([]byte("

Health of Server is UP & Running... !!

")) +} + +// // const addr = "localhost:8080" + +// func main() { +// fmt.Println("URL Shorten Service Starts ...") + +// port := os.Getenv("PORT") +// if port == "" { +// port = "8080" +// } +// fmt.Println("PORT is: ", port) + +// // /health endpoint is mapped to healthCheckUp +// http.HandleFunc("/health", healthCheckUp) + +// // /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 { +// panic(err) +// } +// } + +// next part of main.go +// package main + +// import ( +// "flag" +// "fmt" +// "os" + +// "github.com/viveksahu26/url_shortner/src" // Adjust the import path according to your project structure +// ) + +// func main() { +// // Define command line flags +// shortCmd := flag.NewFlagSet("short", flag.ExitOnError) +// longURL := shortCmd.String("url", "", "URL to shorten") + +// // Check the command provided +// if len(os.Args) < 2 { +// fmt.Println("expected 'short' subcommand") +// os.Exit(1) +// } + +// switch os.Args[1] { +// case "short": +// shortCmd.Parse(os.Args[2:]) +// if *longURL == "" { +// fmt.Println("Please provide a URL to shorten using --url=\"http://example.com\"") +// os.Exit(1) +// } +// shortURL := src.GenerateShortURL(*longURL) +// fmt.Printf("Your short url: %s\n", shortURL) +// default: +// fmt.Println("expected 'short' subcommand") +// os.Exit(1) +// } +// } diff --git a/src/shortURLGenerator.go b/cmd/url_shortner/cli/short/shortURLGenerator.go similarity index 79% rename from src/shortURLGenerator.go rename to cmd/url_shortner/cli/short/shortURLGenerator.go index a60c1ac..01963a4 100644 --- a/src/shortURLGenerator.go +++ b/cmd/url_shortner/cli/short/shortURLGenerator.go @@ -1,8 +1,10 @@ -package src +package short import ( "fmt" "math/rand" + + "github.com/viveksahu26/url_shortner/cmd/url_shortner/cli/save" ) type Result struct { @@ -16,10 +18,10 @@ type Result struct { // Otherwise generate new ShortURL func GenerateShortURL(longURL string) string { fileName := "url.properties" - isFilePresent, _ := IsFileExist(fileName) + isFilePresent, _ := save.IsFileExist(fileName) if isFilePresent { - shortAndLongURLKeyValuePair, _, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL) + shortAndLongURLKeyValuePair, _, fileContainLongURL := save.IsLongURLPresentInFile(fileName, longURL) if fileContainLongURL { // then retrieve ShortURL from there. if shorturl, ok := shortAndLongURLKeyValuePair[longURL]; ok { @@ -47,10 +49,10 @@ func CheckWhetherShortURLisPresentORNot(shorturl string) string { fmt.Println("Yes, you are inside CheckWhetherShortURLisPresentORNot") fileName := "url.properties" - isFilePresent, _ := IsFileExist(fileName) + isFilePresent, _ := save.IsFileExist(fileName) if isFilePresent { - shortAndLongURLKeyValuePair, _, fileContainShortURL := IsLongURLPresentInFile(fileName, shorturl) + shortAndLongURLKeyValuePair, _, fileContainShortURL := save.IsLongURLPresentInFile(fileName, shorturl) if fileContainShortURL { // then retrieve ShortURL from there. if longurl, ok := shortAndLongURLKeyValuePair[shorturl]; ok { diff --git a/cmd/url_shortner/main.go b/cmd/url_shortner/main.go new file mode 100644 index 0000000..a11e405 --- /dev/null +++ b/cmd/url_shortner/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "os" + + "github.com/viveksahu26/url_shortner/cmd/url_shortner/cli/short" +) + +func main() { + // CLI part + shortCmd := flag.NewFlagSet("short", flag.ExitOnError) + longURL := shortCmd.String("url", "", "URL to shorten (CLI mode)") + + // Web server part + serverCmd := flag.NewFlagSet("server", flag.ExitOnError) + port := serverCmd.String("port", "8080", "Port to run the web server on") + + if len(os.Args) < 2 { + fmt.Println("expected 'short' or 'server' subcommands") + os.Exit(1) + } + + switch os.Args[1] { + case "short": + shortCmd.Parse(os.Args[2:]) + if *longURL == "" { + fmt.Println("Please provide a URL to shorten using --url=\"http://example.com\"") + os.Exit(1) + } + shortURL := short.GenerateShortURL(*longURL) + fmt.Printf("Your short url: %s\n", shortURL) + case "server": + serverCmd.Parse(os.Args[2:]) + fmt.Printf("Starting server on port %s...\n", *port) + http.HandleFunc("/health", short.HealthCheckUp) + http.HandleFunc("/short-url", short.HandleShortURL) + http.HandleFunc("/long-url", short.HandleLongURL) + // setupRoutes() // Function to set up web routes + log.Fatal(http.ListenAndServe(":"+*port, nil)) + default: + fmt.Println("expected 'short' or 'server' subcommands") + os.Exit(1) + } +} + +// func setupRoutes() { +// http.HandleFunc("/health", src.HealthCheckUp) +// http.HandleFunc("/short-url", src.HandleShortURL) +// http.HandleFunc("/long-url", src.HandleLongURL) +// } diff --git a/src/shortner.go b/src/shortner.go deleted file mode 100644 index 816544a..0000000 --- a/src/shortner.go +++ /dev/null @@ -1,110 +0,0 @@ -// // http://localhost:8080/long-url?sortURL=xtNFxaBwCG -package src - -// This was earlier main.go program. - -// import ( -// "fmt" -// "net/http" -// "os" - -// "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("

HSorry, No corresponding longURL is present to the shortURL. !!

")) -// } -// writer.Write([]byte(longURL)) -// } -// } - -// func handleShortURL(writer http.ResponseWriter, req *http.Request) { -// if req.Method != "GET" { -// writer.WriteHeader(http.StatusMethodNotAllowed) -// } else { -// // get original URL from GET method by quering -// originalURL := req.URL.Query().Get("longURL") -// fmt.Println("originalURL: ", originalURL) - -// // generate random shortURL -// shortURL := src.GenerateShortURL(originalURL) -// fmt.Println("shortURL: ", shortURL) - -// // save short and long URL to file -// src.SaveInFile(shortURL, originalURL) - -// host := req.Host - -// // build Response -// resp := src.BuildURLWithResponse(host, shortURL, originalURL) - -// 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) { -// if r.URL.Path != "/health" { -// http.Error(w, "404 not found", http.StatusNotFound) -// return -// } - -// if r.Method != "GET" { -// http.Error(w, "Method other than GET not Supported...", http.StatusNotFound) -// return -// } - -// w.Write([]byte("

Health of Server is UP & Running... !!

")) -// } - -// // const addr = "localhost:8080" - -// func main() { -// fmt.Println("URL Shorten Service Starts ...") - -// port := os.Getenv("PORT") -// if port == "" { -// port = "8080" -// } -// fmt.Println("PORT is: ", port) - -// // /health endpoint is mapped to healthCheckUp -// http.HandleFunc("/health", healthCheckUp) - -// // /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 { -// panic(err) -// } -// }