https://github.com/mikebionic/viewscount
A smart and configurable Golang library for PostgreSQL that tracks organic views for any database tables and columns. Easily integrable as middleware in frameworks like Gin, Chi, or net/http.
https://github.com/mikebionic/viewscount
counter go middleware organic sql views
Last synced: about 1 year ago
JSON representation
A smart and configurable Golang library for PostgreSQL that tracks organic views for any database tables and columns. Easily integrable as middleware in frameworks like Gin, Chi, or net/http.
- Host: GitHub
- URL: https://github.com/mikebionic/viewscount
- Owner: mikebionic
- Created: 2024-11-26T08:42:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-19T14:25:06.000Z (over 1 year ago)
- Last Synced: 2025-04-12T20:58:31.247Z (about 1 year ago)
- Topics: counter, go, middleware, organic, sql, views
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Organic Views counter in Go
Imagine you have tables with a **view_count** column, but you don't want to write separate api for them and you want to prevent DOS type increment. This library is a solution for you.
```sql
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
);
CREATE TABLE tbl_vehicle
(
id SERIAL PRIMARY KEY,
numberplate VARCHAR(100) NOT NULL DEFAULT '',
view_count INT NOT NULL DEFAULT 0,
deleted INT NOT NULL DEFAULT 0
);
```
---
An example how you can integrate viewscounter into Gin API. You should create a middleware handler
```go
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()
}
}
```
In your main app you should import created middleware and configure the database (supports sql.DB and pgxscann.DB)
```go
func main() {
//...
middlewares.InitializeViewTracker(database.DB, 10)
// some routers controllers:
router.GET("/:id", middlewares.ViewCounterMiddleware("tbl_driver"), services.GetDriver)
//...
}
```
