diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | .space/database/Hit | 6 | ||||
| -rw-r--r-- | go.mod | 1 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | pkg/database/database.go | 59 | ||||
| -rw-r--r-- | route.go | 5 | ||||
| -rw-r--r-- | template.go | 10 |
7 files changed, 78 insertions, 7 deletions
@@ -3,5 +3,7 @@ # Development /.space/.certificates/ +/.space/database/ /.space-data/.certificates/ +/.space-data/database/ /space diff --git a/.space/database/Hit b/.space/database/Hit new file mode 100644 index 0000000..6c51f46 --- /dev/null +++ b/.space/database/Hit @@ -0,0 +1,6 @@ +[ + { + "path": "/", + "count": 2 + } +]
\ No newline at end of file @@ -4,5 +4,6 @@ go 1.16 require ( github.com/pitr/gig v0.9.8 + github.com/sonyarouje/simdb v0.1.0 github.com/spf13/viper v1.8.1 ) @@ -212,6 +212,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/sonyarouje/simdb v0.1.0 h1:oCxF05HdVmG3vdZZf5oLINIS0UYLoZBVx/IOJzpMA2A= +github.com/sonyarouje/simdb v0.1.0/go.mod h1:sBxWOZxv78yOmCzIyXbUWzHua9+QpXwwnFdlLK/UiUU= github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= diff --git a/pkg/database/database.go b/pkg/database/database.go new file mode 100644 index 0000000..9d14fa9 --- /dev/null +++ b/pkg/database/database.go @@ -0,0 +1,59 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +package database + +import ( + "github.com/sonyarouje/simdb" +) + +var driver *simdb.Driver + +func init() { + var err error + driver, err = simdb.New(".space/database") + if err != nil { + panic(err) + } +} + +type Hit struct { + Path string `json:"path"` + Count int `json:"count"` +} + +func (c Hit) ID() (jsonField string, value interface{}) { + value = c.Path + jsonField = "path" + return +} + +func Get(path string) int { + var hit Hit + + err := driver.Open(Hit{}).Where("path", "=", path).First().AsEntity(&hit) + if err != nil { + return 0 + } + + return hit.Count +} + +func Create(path string) { + driver.Insert(Hit{ + Path: path, + Count: 0, + }) +} + +func Increment(path string) { + var hit Hit + + err := driver.Open(Hit{}).Where("path", "=", path).First().AsEntity(&hit) + if err != nil { + return + } + + hit.Count = hit.Count + 1 + driver.Update(hit) +} @@ -4,6 +4,7 @@ package main import ( + "github.com/fuwn/space/pkg/database" "github.com/fuwn/space/pkg/utilities" "github.com/pitr/gig" ) @@ -13,7 +14,7 @@ func createRoute(route string, template string, content string) { return c.Render(template, IndexTemplate{ Content: GetContent(content), Quote: utilities.GetRandomQuote(), - Hits: hitsTracker[route] + 1, + Hits: database.Get(route) + 1, Copyright: utilities.GetCopyright(), }) }) @@ -25,7 +26,7 @@ func createErrorRoute(route string, template string, content string, err string) Error: err, Content: GetContent(content), Quote: utilities.GetRandomQuote(), - Hits: hitsTracker[route] + 1, + Hits: database.Get(route) + 1, Copyright: utilities.GetCopyright(), }) }) diff --git a/template.go b/template.go index 69a7716..2e992d7 100644 --- a/template.go +++ b/template.go @@ -7,6 +7,7 @@ import ( "io" "text/template" + "github.com/fuwn/space/pkg/database" "github.com/pitr/gig" "github.com/spf13/viper" ) @@ -43,12 +44,11 @@ func (_ ErrorTemplate) HitsEnabled() bool { func (t *Template) Render(w io.Writer, name string, data interface{}, c gig.Context) error { // Check if the route is present in the hits tracker, if it isn't present, add // it, but either way: increment it. - // - // https://stackoverflow.com/a/2050570 - if _, ok := hitsTracker[c.Path()]; !ok { - hitsTracker[c.Path()] = 0 + hits := database.Get(c.Path()) + if hits == 0 { + database.Create(c.Path()) } - hitsTracker[c.Path()]++ + database.Increment(c.Path()) return t.Templates.ExecuteTemplate(w, name, data) } |