-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
58 lines (48 loc) · 1.23 KB
/
handlers.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
package server
import (
"net/http"
"time"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
)
// Time0 represents initial time when we started the server
var Time0 time.Time
// init function
func init() {
Time0 = time.Now()
}
//
// GET handlers
//
// CaptchaHandler provides access to captcha server
func CaptchaHandler() gin.HandlerFunc {
hdlr := captcha.Server(captcha.StdWidth, captcha.StdHeight)
return func(c *gin.Context) {
hdlr.ServeHTTP(c.Writer, c.Request)
}
}
// GinRoute represents git route info
type GinRoute struct {
Method string `json:"method"`
Path string `json:"path"`
}
// ApisHandler provides JSON output for server routes
func ApisHandler(c *gin.Context) {
var ginRoutes []GinRoute
for _, r := range _routes {
route := GinRoute{Method: r.Method, Path: r.Path}
ginRoutes = append(ginRoutes, route)
}
c.JSON(http.StatusOK, ginRoutes)
}
// MetricsHandler provides metrics JSON for monitoring purposes (Prometheus)
func MetricsHandler(c *gin.Context) {
c.Writer.Write([]byte(promMetrics(metricsPrefix)))
}
// GinHandlerFunc converts given http.Handler to gin.HandlerFunc
func GinHandlerFunc(hdlr http.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
hdlr(c.Writer, c.Request)
c.Next()
}
}