Support exchange shortened URLs (#133)
* feat: support shortened exchange urls * feat: add shortener processing for exchange /a/:id conventions
This commit is contained in:
parent
bcc932bd22
commit
80b45bf034
26
main.go
26
main.go
@ -55,6 +55,23 @@ func main() {
|
|||||||
r.GET("/q/:id", routes.RedirectShortenedOverflowURL)
|
r.GET("/q/:id", routes.RedirectShortenedOverflowURL)
|
||||||
r.GET("/q/:id/:answerId", 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) {
|
r.GET("/questions/:id", func(c *gin.Context) {
|
||||||
// redirect user to the question with the title
|
// redirect user to the question with the title
|
||||||
c.Redirect(302, fmt.Sprintf("/questions/%s/placeholder", c.Param("id")))
|
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
|
// 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")))
|
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)
|
r.GET("/proxy", routes.GetImage)
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
@ -12,6 +13,7 @@ import (
|
|||||||
func RedirectShortenedOverflowURL(c *gin.Context) {
|
func RedirectShortenedOverflowURL(c *gin.Context) {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
answerId := c.Param("answerId")
|
answerId := c.Param("answerId")
|
||||||
|
sub := c.Param("sub")
|
||||||
|
|
||||||
// fetch the stack overflow URL
|
// fetch the stack overflow URL
|
||||||
client := resty.New()
|
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 {
|
if err != nil {
|
||||||
c.HTML(400, "home.html", gin.H{
|
c.HTML(400, "home.html", gin.H{
|
||||||
"errorMessage": "Unable to fetch stack overflow URL",
|
"errorMessage": "Unable to fetch stack overflow URL",
|
||||||
@ -41,5 +49,10 @@ func RedirectShortenedOverflowURL(c *gin.Context) {
|
|||||||
// get the redirect URL
|
// get the redirect URL
|
||||||
location := resp.Header().Get("Location")
|
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))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user