func ShortenURL(db *database.Database) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		// Long URL named parameter
		var req ShortenReq
		// Read Request Body, parse it as JSON and get long URL
		err := parseJSON(r.Body, &req)
		defer r.Body.Close()
		if err != nil {
			// Error Reading Request Body
			sendErrorResponse(w, r, http.StatusBadRequest, err)
			return
		}
		// Long URL Not Supplied
		if req.LongURL == "" {
			jsonResp, err := createJSON(ErrorResp{Error: "long_url parameter is required"})
			if err != nil {
				log.Fatal(err)
			}
			sendResponse(w, r, http.StatusBadRequest, jsonResp)
			return
		}
		// Convert long_url to short_url
		longURL, shortURL, err := shortener.Encode(req.LongURL)
		if err != nil {
			// Encoding Error
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}

		// Check if (short_url, long_url) entry is in Database (To detect Collision Error)
		longURLTest, err := database.GetEntryDB(db, shortURL)
		if err != nil {
			// Database Error
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}
		// If same short_url (key) is found but different long_url (value), Collision Error
		if longURLTest != longURL && longURLTest != "" {
			msg := fmt.Sprintf("URL Collision, Found 2 values for %s key: %s, %s", shortURL, longURLTest, longURL)
			log.Print(msg)
			// TMP: Send JSON response
			jsonResp, err := createJSON(MessageResp{Message: msg})
			if err != nil {
				log.Fatal(err)
			}
			sendResponse(w, r, http.StatusInternalServerError, jsonResp)
			return
		}

		// Save to Database
		err = database.PutEntryDB(db, shortURL, longURL)
		if err != nil {
			// Database Error
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}
		// Return short_url in JSON
		jsonResp, err := createJSON(ShortenResp{
			LongURL:  longURL,
			ShortURL: shortURL,
		})
		if err != nil {
			log.Fatal(err)
		}
		sendResponse(w, r, http.StatusOK, jsonResp)
	}
}
// ShortenURL handles requests for /shorten path
ShortenURL handles requests for /shorten path
// ShortenURL handles requests for / shorten path
func ShortenURL(db *database.Database) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		var req ShortenReq
		err := parseJSON(r.Body, &req)
		defer r.Body.Close()
		if err != nil {
			sendErrorResponse(w, r, http.StatusBadRequest, err)
			return
		}
		if req.LongURL == "" {
			jsonResp, err := createJSON(ErrorResp{Error: "long_url parameter is required"})
			if err != nil {
				log.Fatal(err)
			}
			sendResponse(w, r, http.StatusBadRequest, jsonResp)
			return
		}
		longURL, shortURL, err := shortener.Encode(req.LongURL)
		if err != nil {
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}
		longURLTest, err := database.GetEntryDB(db, shortURL)
		if err != nil {
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}
		if longURLTest != longURL && longURLTest != "" {
			msg := fmt.Sprintf("URL Collision, Found 2 values for %s key: %s, %s", shortURL, longURLTest, longURL)
			log.Print(msg)
			jsonResp, err := createJSON(MessageResp{Message: msg})
			if err != nil {
				log.Fatal(err)
			}
			sendResponse(w, r, http.StatusInternalServerError, jsonResp)
			return
		}
		err = database.PutEntryDB(db, shortURL, longURL)
		if err != nil {
			sendErrorResponse(w, r, http.StatusInternalServerError, err)
			return
		}
		jsonResp, err := createJSON(ShortenResp{
			LongURL:  longURL,
			ShortURL: shortURL,
		})
		if err != nil {
			log.Fatal(err)
		}
		sendResponse(w, r, http.StatusOK, jsonResp)
	}
}
func ShortenURL ( db * database . Database ) httprouter . Handle { return func ( w http . ResponseWriter , r * http . Request , ps httprouter . Params ) { var req ShortenReq err := parseJSON ( r . Body , & req ) defer r . Body . Close ( ) if err != nil { sendErrorResponse ( w , r , http . StatusBadRequest , err ) return } if req . LongURL == " " { jsonResp , err := createJSON ( ErrorResp { Error : " " } ) if err != nil { log . Fatal ( err ) } sendResponse ( w , r , http . StatusBadRequest , jsonResp ) return } longURL , shortURL , err := shortener . Encode ( req . LongURL ) if err != nil { sendErrorResponse ( w , r , http . StatusInternalServerError , err ) return } longURLTest , err := database . GetEntryDB ( db , shortURL ) if err != nil { sendErrorResponse ( w , r , http . StatusInternalServerError , err ) return } if longURLTest != longURL && longURLTest != " " { msg := fmt . Sprintf ( " " , shortURL , longURLTest , longURL ) log . Print ( msg ) jsonResp , err := createJSON ( MessageResp { Message : msg } ) if err != nil { log . Fatal ( err ) } sendResponse ( w , r , http . StatusInternalServerError , jsonResp ) return } err = database . PutEntryDB ( db , shortURL , longURL ) if err != nil { sendErrorResponse ( w , r , http . StatusInternalServerError , err ) return } jsonResp , err := createJSON ( ShortenResp { LongURL : longURL , ShortURL : shortURL , } ) if err != nil { log . Fatal ( err ) } sendResponse ( w , r , http . StatusOK , jsonResp ) } }
ShortenURL handles requests for /shorten path
ShortenURL handles requests for / shorten path
// Long URL named parameter // Read Request Body, parse it as JSON and get long URL // Error Reading Request Body // Long URL Not Supplied // Convert long_url to short_url // Encoding Error // Check if (short_url, long_url) entry is in Database (To detect Collision Error) // Database Error // If same short_url (key) is found but different long_url (value), Collision Error // TMP: Send JSON response // Save to Database // Database Error // Return short_url in JSON