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-28 14:14:30 -05:00
|
|
|
r.Use(middleware.NoCacheMiddleware())
|
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-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")))
|
|
|
|
})
|
2023-02-16 10:25:24 -05:00
|
|
|
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")))
|
|
|
|
})
|
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
|
|
|
}
|