As developers, we often encounter the need to track views on content across various platforms. Whether it's tracking views for articles, videos, or products, most applications rely on a simple view_count
column in their database tables. The challenge arises when you don’t want to go through the hassle of writing a separate API for each table that needs view tracking, while also ensuring that your system is secure from abuse like DoS (Denial of Service) attacks. This is the problem I encountered and the reason I created the Viewscount library.
In most applications, views are tracked in some form—whether it’s for a blog post, a product listing, or even user profile pages. However, implementing a solution that is both scalable and secure can become cumbersome. You could go the route of writing custom APIs for each of these tables, but that’s time-consuming and doesn’t scale well. What if there was a way to track these views without rewriting APIs for every new table? What if there was a more efficient way to integrate this functionality into any app? That’s when I started working on Viewscount.
The Need for a Unified Solution
When building any system with views tracking, one of the critical concerns is ensuring that the view count remains authentic. In some cases, malicious users may attempt to artificially inflate the view count through repeated requests—this is where preventing DOS (Denial of Service) type increments comes into play. Without a solid solution, your application becomes vulnerable to these types of attacks, undermining the integrity of your view count.
Thus, the idea for Viewscount was born. I wanted a way to seamlessly count organic views across any tables, regardless of the framework, while providing an easy-to-integrate middleware solution that prevents abusive increments. This library is designed to integrate with any Golang web framework, like Gin, Chi, or even basic net/http, and is database-agnostic, supporting PostgreSQL seamlessly.
How Viewscount Works
When you add Viewscount to your application, you can begin tracking views instantly—without having to write separate APIs for each table. Let’s say you have tables like tbl_driver
or tbl_vehicle
, each with a view_count
column. Instead of building an API specifically to increment the view_count
each time a user views a page, you can use Viewscount to track this automatically.
Here’s how it works in practice:
In your database, you simply add the view_count
column to your tables like so:
CREATE TABLE tbl_driver
(
id SERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL DEFAULT '',
view_count INT NOT NULL DEFAULT 0,
deleted INT NOT NULL DEFAULT 0
);
The same goes for your other tables. No need for complicated queries or APIs—just a simple column to track views.
Integrating Viewscount into Your Application
The best part of Viewscount is that it’s designed to be framework-agnostic. Whether you're using Gin, Chi, or even net/http, you can easily integrate it into your application. Here’s how you can add it as middleware in Gin:
First, you’ll need to initialize the view tracker in your app:
package middlewares
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/mikebionic/viewscount"
"time"
)
var viewTracker *viewscount.ViewTracker
func InitializeViewTracker(db interface{}, minutesGap time.Duration) {
viewTracker = viewscount.NewViewTracker(db, minutesGap)
}
func ViewCounterMiddleware(tableName string) gin.HandlerFunc {
return func(c *gin.Context) {
id := c.Param("id")
if id == "" {
c.Next()
return
}
idInt := 0
fmt.Sscanf(id, "%d", &idInt)
if err := viewTracker.HandleView(c.Request, tableName, idInt); err != nil {
fmt.Printf("Error tracking view: %v\n", err)
}
c.Next()
}
}
Once you’ve set up the middleware, you can add it to your routes. Here’s how you do it:
func main() {
// Initialize the database and middleware
middlewares.InitializeViewTracker(database.DB, 10)
// Define routes
router.GET("/:id", middlewares.ViewCounterMiddleware("tbl_driver"), services.GetDriver)
//...
}
This simple setup integrates the Viewscount library into your existing app. The middleware will automatically track views on the specified table and increment the view_count
column as needed, while also providing protection against abuse.
The Benefits of Viewscount
- Seamless Integration: With just a few lines of code, you can integrate view counting into any of your existing tables.
- Framework-Agnostic: The library is designed to work with any Golang web framework, making it easy to use in your project regardless of the stack.
- Prevention of DOS Attacks: By limiting rapid requests that could artificially inflate view counts, Viewscount ensures that the data remains reliable and accurate.
- Efficient and Scalable: No need for custom APIs or complex queries—just use the library and track views automatically in real time.
Conclusion
Viewscount solves a common problem in application development: how to track views across tables securely and efficiently without having to build a separate API for each table. Its easy integration, ability to prevent DOS-style attacks, and framework-agnostic design make it a powerful tool for developers building modern Golang applications.
Feel free to contribute to the project and raise issues or suggestions via Viewscount GitHub. I’d love to see how this library can help others in their development journey!
Happy coding!