-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
94 lines (72 loc) · 7.75 KB
/
router.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
package main
import (
"encoding/base64"
"net/http"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"git.tcp.direct/tcp.direct/tcp.ac/config"
)
const favicon = "AAABAAEAUFAQAAEABAC0CgAAFgAAAIlQTkcNChoKAAAADUlIRFIAAABQAAAAUAgGAAAAjhHyrQAACntJREFUeJztXF1oG9kV/sbWeKTYkSxbUeufrIxjJxTi+CfdNBAX6gbSQE3thoJhQ8s2y5YNgYRlYSFst+6mLH4ptOsS1uziQlgSGlpCDF5oAo3zYEPWTezECYTUSprZKHaryJIlO5aUiTJ9mJ3xSDOauSONNHKa70lo7s+55557zrnnnnupCtrN88/X8Aq5gQLAW03ERkYZTTFW07Ch8UoC88QrCcwTNsq2CeCSAIDdh52wO8tN7SARS+HG2Rhx+db9dni3O0xr19tmQ2v3ZuL+s2F2LIq1hReK/21yC3zwvddQ5dEn3ghWQ3HcOHuHuPyB4w1o7KzRLReYDRMxcM+hGux7y0fcfzYE/3Ub8wsJxf82jk/m3Xi+2H3YiboWOwDA7rIR1bG7bOgd9GL8o2AhSdOFjaYYWMlEmmLQfqAGrT1bDNXzNDvhaXbi8h+f4FlU3Q4WQ79LOtAqY3LoEw8c1bnr3Z/+rl76ff74YzNIMgRJB1olhR399fqFCOtbwkCRcTTFwD8ZQaVrNWthR3W5pOBXQ3FUeRwIzIYRX05lrfM0ymkSILajBnnbdW1VmgZuNRTX7Efe1/zEE92yAIjUiq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IAogIDg4LCAgIjhhLCAgICxhYSA4OGIsICAgLGE4IiA4ODggODgsICAgICw4OCAiOGEsICAgLGFhICAKICAiWTg4OCBgIlliYmQ4IicgODhgWWJiZFAiJyAgODg4IGAiOGJiZFAiWTggIGAiWWJiZDgiJyAgCiAgICAgICAgICAgICAgICAgIDg4ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICA4OCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKCjwvcHJlPgo8L2Rpdj4KCjxhIGhyZWY9Imh0dHA6Ly9hZG1pbi50Y3AuYWMvIiBzdHlsZT0iZGlzcGxheTpub25lOyI+YWRtaW4gbG9naW48L2E+Cgo8L2JvZHk+Cgo8L2h0bWw+Cg=="
func favIcon(c *gin.Context) {
ico, _ := base64.StdEncoding.DecodeString(favicon)
c.Data(200, "image/ico", ico)
}
func placeHolder(c *gin.Context) {
html, _ := base64.StdEncoding.DecodeString(staticIndex)
c.Data(200, "text/html", html)
}
func httpRouter() *http.Server {
if !config.Trace {
log.Debug().Caller().Msg("running gin in release mode, enable trace to run gin in debug mode")
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.SetTrustedProxies([]string{"127.0.0.1", "10.8.0.1"})
router.Use(logger.SetLogger(
logger.WithLogger(
func(c *gin.Context, l zerolog.Logger) zerolog.Logger {
if zerolog.GlobalLevel() > zerolog.DebugLevel {
// because this spams the logs
if c.Request.URL.String() == "/ip" {
return zerolog.Nop()
}
}
return log.With().
Str("caller", c.ClientIP()).
Str("url", c.Request.URL.String()).
Str("uagent", c.Request.Header.Get("User-Agent")).
Logger()
},
),
))
router.MaxMultipartMemory = 16 << 20 // crude POST limit (fix this)
// use gzip compression unless someone requests something with an explicit extension
router.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{"(?i)\\.[a-z]*"})))
router.GET("/favicon.ico", favIcon)
router.GET("/", placeHolder)
router.GET("/ip", func(c *gin.Context) { c.String(200, c.ClientIP()) })
imgR := router.Group("/i")
imgR.GET("/", placeHolder)
imgR.POST("/put", func(c *gin.Context) { post(c, imageValidator{}, Image, false) })
imgR.GET("/:uid", func(c *gin.Context) { view(c, imageValidator{}, Image) })
txtR := router.Group("/t")
txtR.GET("/", placeHolder)
txtR.GET("/:uid", func(c *gin.Context) { view(c, textValidator{}, Text) })
delR := router.Group("/d")
delR.GET("/i/:key", func(c *gin.Context) { del(c, Image) })
delR.GET("/t/:key", func(c *gin.Context) { del(c, Text) })
log.Info().Str("Host", config.HTTPBind).
Str("Port", config.HTTPPort).
Msg("done; tcp.ac is live.")
router.SetTrustedProxies(config.TrustedProxies)
srv := &http.Server{
Addr: config.HTTPBind + ":" + config.HTTPPort,
Handler: router,
}
go srv.ListenAndServe()
return srv
}