mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-04-21 06:19:14 +00:00
feat: support shortened exchange urls
This commit is contained in:
parent
bcc932bd22
commit
5cae6c460d
2 changed files with 30 additions and 11 deletions
24
main.go
24
main.go
|
@ -55,6 +55,21 @@ 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)
|
||||||
|
}
|
||||||
|
|
||||||
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 +79,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…
Add table
Reference in a new issue