2022-12-27 22:31:07 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-28 11:57:58 -05:00
|
|
|
"anonymousoverflow/env"
|
2022-12-27 23:53:28 -05:00
|
|
|
"anonymousoverflow/src/middleware"
|
|
|
|
"anonymousoverflow/src/routes"
|
2022-12-28 00:23:53 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-12-27 22:31:07 -05:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2022-12-28 11:57:58 -05:00
|
|
|
env.RunChecks()
|
|
|
|
|
2022-12-28 11:35:03 -05:00
|
|
|
host := os.Getenv("HOST")
|
|
|
|
if host == "" {
|
|
|
|
host = "0.0.0.0"
|
|
|
|
}
|
|
|
|
|
|
|
|
port := os.Getenv("PORT")
|
|
|
|
if port == "" {
|
|
|
|
port = "8080"
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("DEV") != "true" {
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
fmt.Printf("Running in production mode. Listening on %s:%s.", host, port)
|
|
|
|
}
|
|
|
|
|
2022-12-27 22:31:07 -05:00
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
r.LoadHTMLGlob("templates/*")
|
|
|
|
|
2022-12-28 00:00:17 -05:00
|
|
|
r.Use(gin.Recovery())
|
2022-12-28 23:56:14 -05:00
|
|
|
r.Use(middleware.XssPreventionHeaders())
|
2022-12-27 23:53:28 -05:00
|
|
|
r.Use(middleware.OptionsMiddleware())
|
2022-12-28 00:00:17 -05:00
|
|
|
r.Use(middleware.Ratelimit())
|
2022-12-27 23:53:28 -05:00
|
|
|
|
2023-02-04 13:14:52 -05:00
|
|
|
r.GET("/static/*filepath", routes.StaticContent)
|
|
|
|
|
2022-12-27 23:23:19 -05:00
|
|
|
r.GET("/robots.txt", func(c *gin.Context) {
|
|
|
|
c.String(200, "User-agent: *\nDisallow: /")
|
|
|
|
})
|
|
|
|
|
2022-12-28 00:08:06 -05:00
|
|
|
r.GET("/options/:name", routes.ChangeOptions)
|
|
|
|
|
2022-12-27 23:53:28 -05:00
|
|
|
r.GET("/", routes.GetHome)
|
|
|
|
r.POST("/", routes.PostHome)
|
2022-12-27 22:46:31 -05:00
|
|
|
|
2023-08-21 02:01:56 -04:00
|
|
|
r.GET("/a/:id", routes.RedirectShortenedOverflowURL)
|
2024-04-06 04:45:06 +00:00
|
|
|
r.GET("/a/:id/:answerId", routes.RedirectShortenedOverflowURL)
|
2023-09-28 19:26:34 +02:00
|
|
|
r.GET("/q/:id", routes.RedirectShortenedOverflowURL)
|
2024-03-09 12:09:17 -05:00
|
|
|
r.GET("/q/:id/:answerId", routes.RedirectShortenedOverflowURL)
|
2023-08-21 02:01:56 -04:00
|
|
|
|
2024-06-13 02:18:51 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-02-04 12:49:28 -05:00
|
|
|
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")))
|
|
|
|
})
|
2022-12-27 23:53:28 -05:00
|
|
|
r.GET("/questions/:id/:title", routes.ViewQuestion)
|
2023-02-14 19:55:17 -05:00
|
|
|
r.GET("/questions/:id/:title/:answerId", func(c *gin.Context) {
|
|
|
|
// 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")))
|
|
|
|
})
|
2022-12-27 22:46:31 -05:00
|
|
|
|
2023-02-02 19:05:08 -05:00
|
|
|
r.GET("/proxy", routes.GetImage)
|
|
|
|
|
2022-12-28 00:23:53 -05:00
|
|
|
r.Run(fmt.Sprintf("%s:%s", host, port))
|
2022-12-27 22:31:07 -05:00
|
|
|
}
|