aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlaster4385 <blaster4385@tablaster.dev>2024-04-30 11:13:55 +0530
committerBlaster4385 <blaster4385@tablaster.dev>2024-04-30 11:18:22 +0530
commit86c4719b540b96541c5369d24d574a027ba3e34b (patch)
tree56c92f4a63d6f2756f28a74202fca9f9945a06c6
parent639f1748403dfb3a7c8cfc4f0dcca70bc4850473 (diff)
feat: switched to backend based redirection
-rw-r--r--client/src/components/Editor/Editor.jsx3
-rw-r--r--server/main.go13
2 files changed, 13 insertions, 3 deletions
diff --git a/client/src/components/Editor/Editor.jsx b/client/src/components/Editor/Editor.jsx
index 247d060..868036f 100644
--- a/client/src/components/Editor/Editor.jsx
+++ b/client/src/components/Editor/Editor.jsx
@@ -99,9 +99,6 @@ const Editor = () => {
const isURL = URL_REGEX.test(data.content);
if (isURL) {
setText(`Your shortened URL: ${BASE_URL}/r/${id}`);
- if (location.pathname === `/r/${id}`) {
- window.location.href = data.content;
- }
} else {
setLanguage(data.language);
setText(data.content);
diff --git a/server/main.go b/server/main.go
index a48611f..1ce935c 100644
--- a/server/main.go
+++ b/server/main.go
@@ -45,6 +45,7 @@ func RegisterHandlers(e *echo.Echo) {
e.Use(middleware.CORS())
e.POST("/bin", postBin)
e.GET("/bin/:id", getBin)
+ e.GET("/r/:id", redirectToURL)
}
func main() {
@@ -96,6 +97,18 @@ func getBin(echoContext echo.Context) error {
return echoContext.JSON(http.StatusOK, bin)
}
+func redirectToURL(echoContext echo.Context) error {
+ id := echoContext.Param("id")
+ bin, err := getBinById(id)
+ if err != nil {
+ echoContext.Logger().Error(err)
+ return err
+ }
+
+ url := bin.Content
+ return echoContext.Redirect(http.StatusFound, url)
+}
+
func createTable() error {
_, err := db.Exec("CREATE TABLE IF NOT EXISTS bins (id TEXT PRIMARY KEY, content TEXT, language TEXT)")
return err