Merge branch '8-statistics' into 'main'

Resolve "Statistics per Instance level"

Closes #8

See merge request Phil/goshorly!10
This commit is contained in:
Phil 2022-01-08 17:21:53 +01:00
commit 4abc9b8762
12 changed files with 148 additions and 7 deletions

23
db/AddLinks.go Normal file
View file

@ -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
}

23
db/AddViews.go Normal file
View file

@ -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
}

30
db/GetStats.go Normal file
View file

@ -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
}

View file

@ -1,14 +1,15 @@
version: "3" version: "3"
services: services:
web: 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 image: registry.ucode.space/phil/goshorly:latest
environment: environment:
- HOST=example.org # Domain or IP-Adress - HOST=127.0.0.1 # Domain or IP-Adress
- PORT=3000 # The Port you want to use - PORT=3000 # The Port you want to use
- HTTPS=true # If you want to use HTTPS - HTTPS=true # If you want to use HTTPS
- PROXY=true # If you want to use a Reverse Proxy - PROXY=true # If you want to use a Reverse Proxy
- REDIS_URI=redis # The Redis-URI (name of redis container OOTB) - REDIS_URI=redis # The Redis-URI (name of redis container OOTB)
- ENABLE_STATS=true # Enables the /stats public frontend
depends_on: depends_on:
- redis - redis
restart: always restart: always

View file

@ -4,11 +4,12 @@ services:
# 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 image: registry.ucode.space/phil/goshorly:latest
environment: 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 - PORT=3000 # The Port you want to use
- HTTPS=false # If you want to use HTTPS - HTTPS=false # If you want to use HTTPS
- PROXY=false # If you want to use a Reverse Proxy - PROXY=false # If you want to use a Reverse Proxy
- REDIS_URI=redis # The Redis-URI (name of redis container OOTB) - REDIS_URI=redis # The Redis-URI (name of redis container OOTB)
- ENABLE_STATS=true # Enables the /stats public frontend (Default: true)
ports: ports:
- "3000:3000" - "3000:3000"
depends_on: depends_on:

View file

@ -32,6 +32,10 @@ func main() {
app.Get("/", routes.Gethome) app.Get("/", routes.Gethome)
if utils.ESTATS == "true" {
app.Get("/stats", routes.GetStats)
}
app.Get("/:id", routes.ID) app.Get("/:id", routes.ID)
app.Use(limiter.New(utils.ConfigLimiter)) app.Use(limiter.New(utils.ConfigLimiter))

View file

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"git.ucode.space/Phil/goshorly/db"
"git.ucode.space/Phil/goshorly/utils" "git.ucode.space/Phil/goshorly/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -10,5 +11,7 @@ func Gethome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }

View file

@ -1,6 +1,8 @@
package routes package routes
import ( import (
"log"
"git.ucode.space/Phil/goshorly/db" "git.ucode.space/Phil/goshorly/db"
"git.ucode.space/Phil/goshorly/utils" "git.ucode.space/Phil/goshorly/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -16,6 +18,10 @@ func ID(c *fiber.Ctx) error {
"url": "URL not found", "url": "URL not found",
}) })
} else { } else {
_, err = db.StatsIncreaseViewsLinks()
if err != nil {
log.Fatal(err.Error())
}
return c.Status(301).JSON(&fiber.Map{ return c.Status(301).JSON(&fiber.Map{
"error": false, "error": false,
"url": val, "url": val,
@ -28,5 +34,10 @@ func ID(c *fiber.Ctx) error {
"BASEURL": utils.URL, "BASEURL": utils.URL,
}) })
} }
_, err = db.StatsIncreaseViewsLinks()
if err != nil {
log.Fatal(err.Error())
}
return c.Redirect(val) return c.Redirect(val)
} }

View file

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"log"
"regexp" "regexp"
"time" "time"
@ -31,6 +32,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }
@ -48,6 +51,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }
@ -67,6 +72,8 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }
@ -85,11 +92,20 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }
fURL := utils.URL + id fURL := utils.URL + id
// Increase Total Links into the DB
_, err = db.StatsIncreaseTotalLinks()
if err != nil {
log.Fatalln(err.Error())
}
if u.CLI { if u.CLI {
return c.Status(201).JSON(&fiber.Map{ return c.Status(201).JSON(&fiber.Map{
"success": true, "success": true,
@ -102,5 +118,7 @@ func Posthome(c *fiber.Ctx) error {
"GitCommitShort": utils.GitCommitShort, "GitCommitShort": utils.GitCommitShort,
"GitBranch": utils.GitBranch, "GitBranch": utils.GitBranch,
"GitBuild": utils.GitBuild, "GitBuild": utils.GitBuild,
"TotalLinks": db.GetTotalLinks(),
"TotalViews": db.GetTotalViews(),
}) })
} }

16
routes/stats.go Normal file
View file

@ -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(),
})
}

View file

@ -12,6 +12,7 @@ var (
PROXY bool PROXY bool
URL string URL string
REDIS_URI string REDIS_URI string
ESTATS string
) )
func Init_env_vars() { func Init_env_vars() {
@ -48,6 +49,13 @@ func Init_env_vars() {
REDIS_URI = "redis" REDIS_URI = "redis"
} }
UESTATS, _ := os.LookupEnv("ENABLE_STATS")
if UESTATS != "false" {
ESTATS = "true"
} else {
ESTATS = "false"
}
create_string() create_string()
} }

View file

@ -35,6 +35,9 @@
<p>An dead simple & fast URL shortener. <br> <p>An dead simple & fast URL shortener. <br>
All shorten URLs will be available for 30days! All shorten URLs will be available for 30days!
</p> </p>
{{ if and .TotalLinks .TotalViews }}
<p>This Instance has served over {{ .TotalLinks }} URLs and {{ .TotalViews }} total Views!</p>
{{ end }}
<hr> <hr>
<form method="post" action="#"> <form method="post" action="#">
<fieldset> <fieldset>