From 80b45bf034d451144791259d4397620497550cda Mon Sep 17 00:00:00 2001 From: httpjamesm <51917118+httpjamesm@users.noreply.github.com> Date: Thu, 13 Jun 2024 02:18:51 -0400 Subject: [PATCH] Support exchange shortened URLs (#133) * feat: support shortened exchange urls * feat: add shortener processing for exchange /a/:id conventions --- main.go | 26 +++++++++++++++++--------- src/routes/shortened.go | 17 +++++++++++++++-- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 625b161..f423cd1 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,23 @@ func main() { r.GET("/q/:id", routes.RedirectShortenedOverflowURL) r.GET("/q/:id/:answerId", routes.RedirectShortenedOverflowURL) + exchangeRouter := r.Group("/exchange/:sub") + { + exchangeRouter.GET("/questions/:id/:title", routes.ViewQuestion) + exchangeRouter.GET("/questions/:id", func(c *gin.Context) { + // redirect user to the question with the title + c.Redirect(302, fmt.Sprintf("/exchange/%s/questions/%s/placeholder", c.Param("sub"), c.Param("id"))) + }) + exchangeRouter.GET("/questions/:id/:title/:answerId", func(c *gin.Context) { + // redirect user to the answer with the title + c.Redirect(302, fmt.Sprintf("/exchange/%s/questions/%s/%s#%s", c.Param("sub"), c.Param("id"), c.Param("title"), c.Param("answerId"))) + }) + exchangeRouter.GET("/q/:id/:answerId", routes.RedirectShortenedOverflowURL) + exchangeRouter.GET("/q/:id", routes.RedirectShortenedOverflowURL) + exchangeRouter.GET("/a/:id/:answerId", routes.RedirectShortenedOverflowURL) + exchangeRouter.GET("/a/:id", routes.RedirectShortenedOverflowURL) + } + r.GET("/questions/:id", func(c *gin.Context) { // redirect user to the question with the title c.Redirect(302, fmt.Sprintf("/questions/%s/placeholder", c.Param("id"))) @@ -64,15 +81,6 @@ func main() { // redirect user to the answer with the title c.Redirect(302, fmt.Sprintf("/questions/%s/%s#%s", c.Param("id"), c.Param("title"), c.Param("answerId"))) }) - r.GET("/exchange/:sub/questions/:id/:title", routes.ViewQuestion) - r.GET("/exchange/:sub/questions/:id", func(c *gin.Context) { - // redirect user to the question with the title - c.Redirect(302, fmt.Sprintf("/exchange/%s/questions/%s/placeholder", c.Param("sub"), c.Param("id"))) - }) - r.GET("/exchange/:sub/questions/:id/:title/:answerId", func(c *gin.Context) { - // redirect user to the answer with the title - c.Redirect(302, fmt.Sprintf("/exchange/%s/questions/%s/%s#%s", c.Param("sub"), c.Param("id"), c.Param("title"), c.Param("answerId"))) - }) r.GET("/proxy", routes.GetImage) diff --git a/src/routes/shortened.go b/src/routes/shortened.go index 9f608f8..07b5445 100644 --- a/src/routes/shortened.go +++ b/src/routes/shortened.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "os" + "strings" "github.com/gin-gonic/gin" "github.com/go-resty/resty/v2" @@ -12,6 +13,7 @@ import ( func RedirectShortenedOverflowURL(c *gin.Context) { id := c.Param("id") answerId := c.Param("answerId") + sub := c.Param("sub") // fetch the stack overflow URL client := resty.New() @@ -21,7 +23,13 @@ func RedirectShortenedOverflowURL(c *gin.Context) { }), ) - resp, err := client.R().Get(fmt.Sprintf("https://www.stackoverflow.com/a/%s/%s", id, answerId)) + domain := "www.stackoverflow.com" + if strings.Contains(sub, ".") { + domain = sub + } else if sub != "" { + domain = fmt.Sprintf("%s.stackexchange.com", sub) + } + resp, err := client.R().Get(fmt.Sprintf("https://%s/a/%s/%s", domain, id, answerId)) if err != nil { c.HTML(400, "home.html", gin.H{ "errorMessage": "Unable to fetch stack overflow URL", @@ -41,5 +49,10 @@ func RedirectShortenedOverflowURL(c *gin.Context) { // get the redirect URL location := resp.Header().Get("Location") - c.Redirect(302, fmt.Sprintf("%s%s", os.Getenv("APP_URL"), location)) + redirectPrefix := os.Getenv("APP_URL") + if sub != "" { + redirectPrefix += fmt.Sprintf("/exchange/%s", sub) + } + + c.Redirect(302, fmt.Sprintf("%s%s", redirectPrefix, location)) }