diff --git a/go.mod b/go.mod index 7350ffb..0ae0377 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module github.com/viveksahu26/url_shortner go 1.19 + +require gotest.tools v2.2.0+incompatible + +require ( + github.com/google/go-cmp v0.5.9 // indirect + github.com/pkg/errors v0.9.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6890ff7 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= +gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= diff --git a/main.go b/main.go index 1880c75..3e4daed 100644 --- a/main.go +++ b/main.go @@ -49,28 +49,26 @@ func healthCheckUp(w http.ResponseWriter, r *http.Request) { w.Write([]byte("

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

")) } -const addr = "localhost:8080" +// const addr = "localhost:8080" func main() { 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, + port := os.Getenv("PORT") + if port == "" { + port = "3000" } + fmt.Println("PORT is: ", port) - // handleShortUrl function mapped to /enterLongURL - serveMux.HandleFunc("/sort-url", handleShortURL) + // /health endpoint is mapped to healthCheckUp + http.HandleFunc("/health", healthCheckUp) - // healthCheckUp function mapped to /health - serveMux.HandleFunc("/health", healthCheckUp) + // /short-url endpoint is mapped to handleShortURL + http.HandleFunc("/short-url", handleShortURL) - // Server Listing on "localhost:8080" - err := srv.ListenAndServe() + // Server Listening on localhost:8080 + err := http.ListenAndServe(":"+port, nil) if err != nil { - fmt.Println("Could not serve.") - os.Exit(1) + panic(err) } }