feat: home page with URL conversion endpoint

style: mobile optimization
This commit is contained in:
httpjamesm 2022-12-27 23:39:34 -05:00
parent 85dad1cbe0
commit 2ae093b43d
6 changed files with 166 additions and 11 deletions

29
main.go
View file

@ -24,6 +24,35 @@ func main() {
c.String(200, "User-agent: *\nDisallow: /")
})
r.GET("/", func(c *gin.Context) {
c.HTML(200, "home.html", gin.H{})
})
type urlConversionRequest struct {
URL string `form:"url" binding:"required"`
}
r.POST("/", func(c *gin.Context) {
body := urlConversionRequest{}
if err := c.ShouldBind(&body); err != nil {
c.JSON(400, gin.H{"success": false, "message": "Invalid request body"})
return
}
soLink := body.URL
// validate URL
if !strings.HasPrefix(soLink, "https://stackoverflow.com/questions/") {
c.JSON(400, gin.H{"success": false, "message": "Invalid URL"})
return
}
// redirect to the proxied thread
c.Redirect(302, fmt.Sprintf("/questions/%s", strings.TrimPrefix(soLink, "https://stackoverflow.com/questions/")))
})
r.GET("/questions/:id/:title", func(c *gin.Context) {
questionId := c.Param("id")
questionTitle := c.Param("title")