Merge branch 'main' into fix/replace-relative-urls-with-absolute-ones

This commit is contained in:
httpjamesm 2024-06-13 02:19:06 -04:00
commit 23802fcf5b
No known key found for this signature in database
2 changed files with 32 additions and 11 deletions

26
main.go
View file

@ -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)

View file

@ -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))
}