Skip to content

Commit

Permalink
Implement URL redirection handler with Redis integration and error ha…
Browse files Browse the repository at this point in the history
…ndling
  • Loading branch information
SartajBhuvaji committed Nov 22, 2024
1 parent 1f8248f commit 60d0f21
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
41 changes: 39 additions & 2 deletions api/redirect.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
package api

import (
"encoding/json"
"net/http"

"github.com/SartajBhuvaji/database"
)

type RedirectURLRequest struct {
LongURL string `json:"long_url"`
}

// handle the short --> long URL redirection
func RedirectHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not implemented", http.StatusNotImplemented)
func RedirectHandler(w http.ResponseWriter, r *http.Request, redisClient *database.RedisClient) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}

var req ShortenURLRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}

if req.URL == "" {
http.Error(w, "URL is required", http.StatusBadRequest)
return
}
shortUrl := req.URL
longUrl, err := redisClient.Get(shortUrl)
if err != nil {
http.Error(w, "Error fetching URL from Reddis", http.StatusInternalServerError)
return
}
//http.Redirect(w, r, longUrl, http.StatusMovedPermanently)

response := RedirectURLRequest{
LongURL: longUrl,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Error encoding response", http.StatusInternalServerError)
return
}
}
2 changes: 1 addition & 1 deletion api/shorten.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ShortenURLHandler(w http.ResponseWriter, r *http.Request, redisClient *data
shortURL := "something/" + enc

// Update the counter counter++
err = redisClient.SetCounter("counter", counter+1)
redisClient.SetCounter("counter", counter+1)

// Add the short URL to the database
err = redisClient.Set(shortURL, req.URL)
Expand Down
2 changes: 2 additions & 0 deletions database/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -93,6 +94,7 @@ func (r *RedisClient) Get(shortURL string) (string, error) {
var urlValue URLValue
err := r.client.Get(r.ctx, shortURL).Scan(&urlValue)
if err != nil {
log.Println("Error fetching URL from Redis: ", err)
return "", err
}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func main() {
})

// shortURL --> longURL
http.HandleFunc("/redirect", api.RedirectHandler)
http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
api.RedirectHandler(w, r, redisClient)
})

log.Println("Server started on :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
Expand Down

0 comments on commit 60d0f21

Please sign in to comment.