コード例 #1
0
func (l *Logger) End(c *yarf.Context) error {
	// If nobody sets the status code, it's a 200
	var code int
	if _, ok := c.Response.(*LoggerWriter); ok {
		code = c.Response.(*LoggerWriter).StatusCode
	}

	if code == 0 {
		code = 200
	}

	log.Printf(
		"| %s | %s | %d | %s",
		c.GetClientIP(),
		c.Request.Method,
		code,
		c.Request.URL.String(),
	)

	return nil
}
コード例 #2
0
// PreDispatch performs the requests counting and handle blocks/
func (m *RateLimiter) PreDispatch(c *yarf.Context) error {
	// IP as key
	key := c.GetClientIP()

	// Count
	err := m.rl.Count(key)
	if err != nil {
		if _, ok := err.(RateLimitError); ok {
			return &YarfError{}
		}

		return err
	}

	// Set rate limit info on headers
	rate := m.rl.Get(key)
	c.Response.Header().Set("X-RateLimit-Limit", strconv.Itoa(int(rate.Limit)))
	c.Response.Header().Set("X-RateLimit-Remaining", strconv.Itoa(int(rate.Limit-rate.EventCount)))
	c.Response.Header().Set("X-RateLimit-Reset", strconv.Itoa(int(rate.Start.Add(time.Second*time.Duration(rate.Window)).Unix())))

	return nil
}