{"id":23468212,"url":"https://github.com/mikebionic/viewscount","last_synced_at":"2025-04-12T20:58:34.733Z","repository":{"id":268894937,"uuid":"894372728","full_name":"mikebionic/viewscount","owner":"mikebionic","description":"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.","archived":false,"fork":false,"pushed_at":"2024-12-19T14:25:06.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T20:58:31.247Z","etag":null,"topics":["counter","go","middleware","organic","sql","views"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikebionic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-26T08:42:40.000Z","updated_at":"2025-02-20T11:44:35.000Z","dependencies_parsed_at":"2024-12-19T15:38:46.741Z","dependency_job_id":null,"html_url":"https://github.com/mikebionic/viewscount","commit_stats":null,"previous_names":["mikebionic/viewscount"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebionic%2Fviewscount","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebionic%2Fviewscount/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebionic%2Fviewscount/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikebionic%2Fviewscount/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikebionic","download_url":"https://codeload.github.com/mikebionic/viewscount/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631703,"owners_count":21136561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["counter","go","middleware","organic","sql","views"],"created_at":"2024-12-24T13:39:00.109Z","updated_at":"2025-04-12T20:58:34.714Z","avatar_url":"https://github.com/mikebionic.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Organic Views counter in Go\n\nImagine 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.\n\n```sql\nCREATE TABLE tbl_driver\n(\n    id              SERIAL PRIMARY KEY,\n    first_name      VARCHAR(100) NOT NULL DEFAULT '',\n    view_count      INT          NOT NULL DEFAULT 0,\n    deleted         INT          NOT NULL DEFAULT 0\n);\n\nCREATE TABLE tbl_vehicle\n(\n    id              SERIAL PRIMARY KEY,\n    numberplate      VARCHAR(100) NOT NULL DEFAULT '',\n    view_count      INT          NOT NULL DEFAULT 0,\n    deleted         INT          NOT NULL DEFAULT 0\n);\n\n\n```\n\n---\n\nAn example how you can integrate viewscounter into Gin API. You should create a middleware handler\n```go\n\npackage middlewares\n\nimport (\n\t\"fmt\"\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/mikebionic/viewscount\"\n\t\"time\"\n)\n\nvar viewTracker *viewscount.ViewTracker\n\nfunc InitializeViewTracker(db interface{}, minutesGap time.Duration) {\n\tviewTracker = viewscount.NewViewTracker(db, minutesGap)\n}\n\nfunc ViewCounterMiddleware(tableName string) gin.HandlerFunc {\n\treturn func(c *gin.Context) {\n\t\tid := c.Param(\"id\")\n\t\tif id == \"\" {\n\t\t\tc.Next()\n\t\t\treturn\n\t\t}\n\n\t\tidInt := 0\n\t\tfmt.Sscanf(id, \"%d\", \u0026idInt)\n\n\t\tif err := viewTracker.HandleView(c.Request, tableName, idInt); err != nil {\n\t\t\tfmt.Printf(\"Error tracking view: %v\\n\", err)\n\t\t}\n\t\tc.Next()\n\t}\n}\n\n\n\n```\n\nIn your main app you should import created middleware and configure the database (supports sql.DB and pgxscann.DB)\n\n```go\nfunc main() {\n\t//...\n\tmiddlewares.InitializeViewTracker(database.DB, 10)\t\n\n\t// some routers controllers:\n\trouter.GET(\"/:id\", middlewares.ViewCounterMiddleware(\"tbl_driver\"), services.GetDriver)\n\t//...\n}\n```\n\n![](https://go.dev/blog/gophergala/fancygopher.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikebionic%2Fviewscount","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikebionic%2Fviewscount","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikebionic%2Fviewscount/lists"}