Skip to content

Commit

Permalink
Added health chack up function
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 6abc1cd commit 98ffe10
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
28 changes: 20 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package main
import (
"fmt"
"net/http"
"time"

"github.com/viveksahu26/url_shortner/src"
)

func handleShortURL(writer http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
writer.WriteHeader(http.StatusMethodNotAllowed)
}
// get original URL from GET method by quering
originalURL := req.URL.Query().Get("longURL")
fmt.Println("originalURL: ", originalURL)
Expand All @@ -20,18 +22,27 @@ func handleShortURL(writer http.ResponseWriter, req *http.Request) {
// save short and long URL to file
src.SaveInFile(shortURL, originalURL)

host := "www.simplifyurl.com"
host := req.Host

// build Response
resp := src.BuildURLWithResponse(host, shortURL, originalURL)
fmt.Println("response: ", resp)

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) {
method := r.Method
if method == "GET" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Health of Server is UP & Running..."))
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

const addr = "localhost:8080"

func main() {
Expand All @@ -40,14 +51,15 @@ func main() {
// 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,
Handler: serveMux,
Addr: addr,
}

// handleShortUrl function mapped to /enterLongURL
serveMux.HandleFunc("/enterLongURL", handleShortURL)
serveMux.HandleFunc("/sort-url", handleShortURL)

// healthCheckUp function mapped to /health
http.HandleFunc("/health", healthCheckUp)

// Server Listing on "localhost:8080"
srv.ListenAndServe()
Expand Down
3 changes: 1 addition & 2 deletions src/saveInfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func SaveInFile(shortURL string, longURL string) {

} else {
fmt.Printf("File %s already exist.", fileName)
_, msg, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)
fmt.Println("\n", msg)
_, _, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)

if !fileContainLongURL {
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
Expand Down
1 change: 0 additions & 1 deletion src/shortURLGenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func GenerateShortURL(longURL string) string {

if isFilePresent {
shortAndLongURLKeyValuePair, _, fileContainLongURL := IsLongURLPresentInFile(fileName, longURL)

if fileContainLongURL {
// then retrieve ShortURL from there.
if shorturl, ok := shortAndLongURLKeyValuePair[longURL]; ok {
Expand Down

0 comments on commit 98ffe10

Please sign in to comment.