mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-05-16 06:53:03 +00:00
feat: home page with URL conversion endpoint
style: mobile optimization
This commit is contained in:
parent
85dad1cbe0
commit
2ae093b43d
6 changed files with 166 additions and 11 deletions
29
main.go
29
main.go
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue