From 151e9b9b7fb16879f18574b3ca99bdc0df69cca2 Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 8 Jan 2022 17:21:53 +0100 Subject: [PATCH] Resolve "Statistics per Instance level" --- db/AddLinks.go | 23 +++++++++++++++++++++++ db/AddViews.go | 23 +++++++++++++++++++++++ db/GetStats.go | 30 ++++++++++++++++++++++++++++++ docker-compose-proxy.yml | 13 +++++++------ docker-compose.yml | 3 ++- main.go | 4 ++++ routes/gethome.go | 3 +++ routes/id.go | 11 +++++++++++ routes/posthome.go | 18 ++++++++++++++++++ routes/stats.go | 16 ++++++++++++++++ utils/env.go | 8 ++++++++ views/home.html | 3 +++ 12 files changed, 148 insertions(+), 7 deletions(-) create mode 100644 db/AddLinks.go create mode 100644 db/AddViews.go create mode 100644 db/GetStats.go create mode 100644 routes/stats.go diff --git a/db/AddLinks.go b/db/AddLinks.go new file mode 100644 index 0000000..6b91688 --- /dev/null +++ b/db/AddLinks.go @@ -0,0 +1,23 @@ +package db + +import ( + "strconv" +) + +func StatsIncreaseTotalLinks() (int, error) { + val, err := Client.Get("total-links").Result() + + if err != nil { + err := Client.Set("total-links", "0", 0).Err() + if err != nil { + return 0, err + } + } + i, _ := strconv.Atoi(val) + i++ + err = Client.Set("total-links", i, 0).Err() + if err != nil { + return 0, err + } + return i, nil +} diff --git a/db/AddViews.go b/db/AddViews.go new file mode 100644 index 0000000..842517e --- /dev/null +++ b/db/AddViews.go @@ -0,0 +1,23 @@ +package db + +import ( + "strconv" +) + +func StatsIncreaseViewsLinks() (int, error) { + val, err := Client.Get("total-views").Result() + + if err != nil { + err := Client.Set("total-views", "0", 0).Err() + if err != nil { + return 0, err + } + } + i, _ := strconv.Atoi(val) + i++ + err = Client.Set("total-views", i, 0).Err() + if err != nil { + return 0, err + } + return i, nil +} diff --git a/db/GetStats.go b/db/GetStats.go new file mode 100644 index 0000000..88afdf9 --- /dev/null +++ b/db/GetStats.go @@ -0,0 +1,30 @@ +package db + +import ( + "log" + "strconv" +) + +func GetTotalLinks() int { + val1, err1 := Client.Get("total-links").Result() + if err1 != nil { + log.Fatal(err1) + } + val2, err2 := strconv.Atoi(val1) + if err2 != nil { + log.Fatal(err1) + } + return val2 +} + +func GetTotalViews() int { + val1, err1 := Client.Get("total-views").Result() + if err1 != nil { + log.Fatal(err1) + } + val2, err2 := strconv.Atoi(val1) + if err2 != nil { + log.Fatal(err1) + } + return val2 +} diff --git a/docker-compose-proxy.yml b/docker-compose-proxy.yml index 9c20d07..5d40e7e 100644 --- a/docker-compose-proxy.yml +++ b/docker-compose-proxy.yml @@ -1,14 +1,15 @@ version: "3" services: web: - # build: . # Only if you want to Build the image on your own Server! + # build: . # Only if you want to Build the image on your own Server! image: registry.ucode.space/phil/goshorly:latest environment: - - HOST=example.org # Domain or IP-Adress - - PORT=3000 # The Port you want to use - - HTTPS=true # If you want to use HTTPS - - PROXY=true # If you want to use a Reverse Proxy - - REDIS_URI=redis # The Redis-URI (name of redis container OOTB) + - HOST=127.0.0.1 # Domain or IP-Adress + - PORT=3000 # The Port you want to use + - HTTPS=true # If you want to use HTTPS + - PROXY=true # If you want to use a Reverse Proxy + - REDIS_URI=redis # The Redis-URI (name of redis container OOTB) + - ENABLE_STATS=true # Enables the /stats public frontend depends_on: - redis restart: always diff --git a/docker-compose.yml b/docker-compose.yml index e9b008a..43e1757 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,11 +4,12 @@ services: # build: . # Only if you want to Build the image on your own Server! image: registry.ucode.space/phil/goshorly:latest environment: - - HOST=your_domain_or_ip # Domain or IP-Adress + - HOST=127.0.0.1 # Domain or IP-Adress - PORT=3000 # The Port you want to use - HTTPS=false # If you want to use HTTPS - PROXY=false # If you want to use a Reverse Proxy - REDIS_URI=redis # The Redis-URI (name of redis container OOTB) + - ENABLE_STATS=true # Enables the /stats public frontend (Default: true) ports: - "3000:3000" depends_on: diff --git a/main.go b/main.go index 30b8be8..238d210 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,10 @@ func main() { app.Get("/", routes.Gethome) + if utils.ESTATS == "true" { + app.Get("/stats", routes.GetStats) + } + app.Get("/:id", routes.ID) app.Use(limiter.New(utils.ConfigLimiter)) diff --git a/routes/gethome.go b/routes/gethome.go index e35997d..cdb3fe7 100644 --- a/routes/gethome.go +++ b/routes/gethome.go @@ -1,6 +1,7 @@ package routes import ( + "git.ucode.space/Phil/goshorly/db" "git.ucode.space/Phil/goshorly/utils" "github.com/gofiber/fiber/v2" ) @@ -10,5 +11,7 @@ func Gethome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } diff --git a/routes/id.go b/routes/id.go index 9193a18..738d812 100644 --- a/routes/id.go +++ b/routes/id.go @@ -1,6 +1,8 @@ package routes import ( + "log" + "git.ucode.space/Phil/goshorly/db" "git.ucode.space/Phil/goshorly/utils" "github.com/gofiber/fiber/v2" @@ -16,6 +18,10 @@ func ID(c *fiber.Ctx) error { "url": "URL not found", }) } else { + _, err = db.StatsIncreaseViewsLinks() + if err != nil { + log.Fatal(err.Error()) + } return c.Status(301).JSON(&fiber.Map{ "error": false, "url": val, @@ -28,5 +34,10 @@ func ID(c *fiber.Ctx) error { "BASEURL": utils.URL, }) } + + _, err = db.StatsIncreaseViewsLinks() + if err != nil { + log.Fatal(err.Error()) + } return c.Redirect(val) } diff --git a/routes/posthome.go b/routes/posthome.go index 711b6e2..312f4e2 100644 --- a/routes/posthome.go +++ b/routes/posthome.go @@ -1,6 +1,7 @@ package routes import ( + "log" "regexp" "time" @@ -31,6 +32,8 @@ func Posthome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } @@ -48,6 +51,8 @@ func Posthome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } @@ -67,6 +72,8 @@ func Posthome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } @@ -85,11 +92,20 @@ func Posthome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } fURL := utils.URL + id + // Increase Total Links into the DB + _, err = db.StatsIncreaseTotalLinks() + + if err != nil { + log.Fatalln(err.Error()) + } + if u.CLI { return c.Status(201).JSON(&fiber.Map{ "success": true, @@ -102,5 +118,7 @@ func Posthome(c *fiber.Ctx) error { "GitCommitShort": utils.GitCommitShort, "GitBranch": utils.GitBranch, "GitBuild": utils.GitBuild, + "TotalLinks": db.GetTotalLinks(), + "TotalViews": db.GetTotalViews(), }) } diff --git a/routes/stats.go b/routes/stats.go new file mode 100644 index 0000000..55d62ef --- /dev/null +++ b/routes/stats.go @@ -0,0 +1,16 @@ +package routes + +import ( + "time" + + "git.ucode.space/Phil/goshorly/db" + "github.com/gofiber/fiber/v2" +) + +func GetStats(c *fiber.Ctx) error { + return c.JSON(&fiber.Map{ + "Timestamp": time.Now(), + "Totallinks": db.GetTotalLinks(), + "Totalviews": db.GetTotalViews(), + }) +} diff --git a/utils/env.go b/utils/env.go index 09e955e..4db8c5c 100644 --- a/utils/env.go +++ b/utils/env.go @@ -12,6 +12,7 @@ var ( PROXY bool URL string REDIS_URI string + ESTATS string ) func Init_env_vars() { @@ -48,6 +49,13 @@ func Init_env_vars() { REDIS_URI = "redis" } + UESTATS, _ := os.LookupEnv("ENABLE_STATS") + if UESTATS != "false" { + ESTATS = "true" + } else { + ESTATS = "false" + } + create_string() } diff --git a/views/home.html b/views/home.html index b73f4fb..a1307f2 100644 --- a/views/home.html +++ b/views/home.html @@ -35,6 +35,9 @@

An dead simple & fast URL shortener.
All shorten URLs will be available for 30days!

+ {{ if and .TotalLinks .TotalViews }} +

This Instance has served over {{ .TotalLinks }} URLs and {{ .TotalViews }} total Views!

+ {{ end }}