-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement URL redirection handler with Redis integration and error ha…
…ndling
- Loading branch information
1 parent
1f8248f
commit 60d0f21
Showing
4 changed files
with
45 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters