From 71ab13c78d08bed905e138e8cdd2019f4c9240db Mon Sep 17 00:00:00 2001 From: Vivek Kumar Sahu Date: Fri, 9 Feb 2024 23:05:07 +0530 Subject: [PATCH] added cli support for url_shortner Signed-off-by: Vivek Kumar Sahu --- main.go | 116 +++++++++--------------------------------------- src/shortner.go | 110 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 94 deletions(-) create mode 100644 src/shortner.go diff --git a/main.go b/main.go index 40a9a30..4939d23 100644 --- a/main.go +++ b/main.go @@ -1,107 +1,35 @@ package main import ( + "flag" "fmt" - "net/http" "os" - "github.com/viveksahu26/url_shortner/src" + "github.com/viveksahu26/url_shortner/src" // Adjust the import path according to your project structure ) -// 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" + // 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) } - 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) + 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/shortner.go b/src/shortner.go new file mode 100644 index 0000000..816544a --- /dev/null +++ b/src/shortner.go @@ -0,0 +1,110 @@ +// // 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) +// } +// }