func dbFunc(c *gin.Context) { if _, err := db.Exec("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("Error creating database table: %q", err)) return } if _, err := db.Exec("INSERT INTO ticks VALUES (now())"); err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("Error incrementing tick: %q", err)) return } rows, err := db.Query("SELECT tick FROM ticks") if err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("Error reading ticks: %q", err)) return } defer rows.Close() for rows.Next() { var tick time.Time if err := rows.Scan(&tick); err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("Error scanning ticks: %q", err)) return } c.String(http.StatusOK, fmt.Sprintf("Read from DB: %s\n", tick.String())) } }
func repeatHandler(c *gin.Context) { var buffer bytes.Buffer for i := 0; i < repeat; i++ { buffer.WriteString("Hello from Go!\n") } c.String(http.StatusOK, buffer.String()) }
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 :)") } }