-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vivek Kumar Sahu <vivekkumarsahu650@gmail.com>
- Loading branch information
1 parent
910132e
commit 9ed8499
Showing
8 changed files
with
224 additions
and
121 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/buildURLresponse.go → ...shortner/cli/response/buildURLresponse.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package src | ||
package response | ||
|
||
type URLResponse struct { | ||
OriginalURL string `json:"originalURL"` | ||
|
7 changes: 4 additions & 3 deletions
7
src/buildURLresponse_test.go → ...ner/cli/response/buildURLresponse_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/respondWithJSON.go → ..._shortner/cli/response/respondWithJSON.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package src | ||
package response | ||
|
||
import ( | ||
"encoding/json" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package src | ||
package save | ||
|
||
import ( | ||
"errors" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<h1>HSorry, No corresponding longURL is present to the shortURL. !!</h1>")) | ||
} | ||
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("<h1>Failed to respond with JSON</h1>")) | ||
} | ||
} | ||
} | ||
|
||
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("<h1>Health of Server is UP & Running... !!</h1>")) | ||
} | ||
|
||
// // 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) | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
// } |
This file was deleted.
Oops, something went wrong.