97 lines
3.0 KiB
Go
Raw Permalink Normal View History

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"
"github.com/gin-gonic/gin"
healthcheck "github.com/tavsec/gin-healthcheck"
"github.com/tavsec/gin-healthcheck/checks"
"github.com/tavsec/gin-healthcheck/config"
)
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)
}
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)
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
r.GET("/a/:id", routes.RedirectShortenedOverflowURL)
r.GET("/a/:id/:answerId", routes.RedirectShortenedOverflowURL)
2023-09-28 19:26:34 +02:00
r.GET("/q/:id", 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)
exchangeRouter.GET("/a/:id/:answerId", routes.RedirectShortenedOverflowURL)
exchangeRouter.GET("/a/:id", routes.RedirectShortenedOverflowURL)
}
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)
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)
2024-07-25 10:51:05 -07:00
r.GET("/version", routes.GetVersion)
soPingCheck := checks.NewPingCheck("https://stackoverflow.com", "GET", 5000, nil, nil)
sePingCheck := checks.NewPingCheck("https://stackexchange.com", "GET", 5000, nil, nil)
healthcheck.New(r, config.DefaultConfig(), []checks.Check{soPingCheck, sePingCheck})
2022-12-28 00:23:53 -05:00
r.Run(fmt.Sprintf("%s:%s", host, port))
}