-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
96 lines (75 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"anonymousoverflow/env"
"anonymousoverflow/src/middleware"
"anonymousoverflow/src/routes"
"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() {
env.RunChecks()
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/*")
r.Use(gin.Recovery())
r.Use(middleware.XssPreventionHeaders())
r.Use(middleware.OptionsMiddleware())
r.Use(middleware.Ratelimit())
r.GET("/static/*filepath", routes.StaticContent)
r.GET("/robots.txt", func(c *gin.Context) {
c.String(200, "User-agent: *\nDisallow: /")
})
r.GET("/options/:name", routes.ChangeOptions)
r.GET("/", routes.GetHome)
r.POST("/", routes.PostHome)
r.GET("/a/:id", routes.RedirectShortenedOverflowURL)
r.GET("/a/:id/:answerId", routes.RedirectShortenedOverflowURL)
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")))
})
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")))
})
r.GET("/proxy", routes.GetImage)
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})
r.Run(fmt.Sprintf("%s:%s", host, port))
}