Skip to content

Commit

Permalink
Support exchange shortened URLs (#133)
Browse files Browse the repository at this point in the history
* feat: support shortened exchange urls

* feat: add shortener processing for exchange /a/:id conventions
  • Loading branch information
httpjamesm authored Jun 13, 2024
1 parent bcc932b commit 80b45bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
26 changes: 17 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -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)

Expand Down
17 changes: 15 additions & 2 deletions src/routes/shortened.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"strings"

"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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))
}

0 comments on commit 80b45bf

Please sign in to comment.