Skip to content

Commit

Permalink
chore: remove unnecessary mapping of longurl and shorturl in code, ma…
Browse files Browse the repository at this point in the history
…ke functions unexported
  • Loading branch information
Prachi-Jamdade committed Nov 14, 2024
1 parent af08fe8 commit 597ce6a
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions examples/url-shortener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"encoding/json"
"os"
"fmt"

"log"
"net/http"
Expand All @@ -13,22 +15,30 @@ import (
)

type URL struct {
ID string `json:"id"`
LongURL string `json:"long_url"`
ShortURL string `json:"short_url"`
}

var db *dicedb.Client

// Initialize DiceDB connection
func init() {
dhost := "localhost"
if val := os.Getenv("DICEDB_HOST"); val != "" {
dhost = val
}

dport := "7379"
if val := os.Getenv("DICEDB_PORT"); val != "" {
dport = val
}

db = dicedb.NewClient(&dicedb.Options{
Addr: "localhost:7379",
Addr: fmt.Sprintf("%s:%s", dhost, dport),
})
}

// Creates a short URL from a given long URL
func CreateShortURL(c *gin.Context) {
func createShortURL(c *gin.Context) {
var requestBody URL
if err := c.ShouldBindJSON(&requestBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
Expand All @@ -37,8 +47,7 @@ func CreateShortURL(c *gin.Context) {

// Generate unique short ID and construct the short URL
shortID := uuid.New().String()[:8]
requestBody.ID = shortID
requestBody.ShortURL = "http://localhost:8080/" + shortID
shortURL := "http://localhost:8080/" + shortID

// Serialize URL struct to JSON and store it in DiceDB
urlData, err := json.Marshal(requestBody)
Expand All @@ -52,11 +61,11 @@ func CreateShortURL(c *gin.Context) {
return
}

c.JSON(http.StatusCreated, gin.H{"short_url": requestBody.ShortURL})
c.JSON(http.StatusCreated, gin.H{"short_url": shortURL})
}

// Redirects to the original URL based on the short URL ID
func RedirectURL(c *gin.Context) {
func redirectURL(c *gin.Context) {
id := c.Param("id")

// Retrieve stored URL data from DiceDB
Expand All @@ -81,8 +90,8 @@ func main() {
router := gin.Default()

// Define endpoints for creating short URLs and redirecting
router.POST("/shorten", CreateShortURL)
router.GET("/:id", RedirectURL)
router.POST("/shorten", createShortURL)
router.GET("/:id", redirectURL)

// Start the server on port 8080
if err := router.Run(":8080"); err != nil {
Expand Down

0 comments on commit 597ce6a

Please sign in to comment.