示例#1
0
func rateLimit(c *gin.Context) {

	ip := c.ClientIP()
	value := int(ips.Add(ip, 1))
	if value%50 == 0 {
		fmt.Printf("ip: %s, count: %d\n", ip, value)
	}
	if value >= 200 {
		if value%200 == 0 {
			fmt.Println("ip blocked")
		}
		c.Abort()
		c.String(503, "you were automatically banned :)")
	}
}
示例#2
0
func mainHandler(c *gin.Context) {
	filename := c.Param("file")
	fmt.Println("opening file: ", filename, filename[1:])
	file, err := os.Open("web" + filename)
	if err != nil {
		fmt.Println(err)
		c.String(404, "cound not find page")
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		fmt.Println(err)
	}
	pathnames := strings.Split(filename, ".")
	ext := pathnames[len(pathnames)-1]
	fmt.Println("extention is:", ext)
	if ext == "js" {
		c.Header("Content-Type", "text/javascript")
		c.String(200, string(data))
	} else if ext == "png" {
		fmt.Println("sending image")
		c.Data(200, "image/png", data)
	} else {
		c.Header("Content-type", "text/"+ext)
		c.String(200, string(data))
	}
}