feat: switched to backend based redirection
This commit is contained in:
parent
639f174840
commit
86c4719b54
2 changed files with 13 additions and 3 deletions
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue