minor refactoring
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@@ -19,10 +18,7 @@ import (
|
|||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
const fraudThreshold = 7
|
||||||
stats = &sync.Map{}
|
|
||||||
ads []byte
|
|
||||||
)
|
|
||||||
|
|
||||||
type Template struct{ templates *template.Template }
|
type Template struct{ templates *template.Template }
|
||||||
|
|
||||||
@@ -40,6 +36,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
var ads []byte
|
||||||
adsFName := os.Getenv("ADS")
|
adsFName := os.Getenv("ADS")
|
||||||
if adsFName != "" {
|
if adsFName != "" {
|
||||||
var err error
|
var err error
|
||||||
@@ -49,7 +46,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
go persistStats(e.Logger, db, stats)
|
go persistStats(e.Logger, db)
|
||||||
go cleanAccessRegistry(e.Logger)
|
go cleanAccessRegistry(e.Logger)
|
||||||
|
|
||||||
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
|
e.Renderer = &Template{templates: template.Must(template.ParseGlob("assets/templates/*.html"))}
|
||||||
@@ -67,15 +64,8 @@ func main() {
|
|||||||
|
|
||||||
e.GET("/:id", func(c echo.Context) error {
|
e.GET("/:id", func(c echo.Context) error {
|
||||||
n, code := load(c, db)
|
n, code := load(c, db)
|
||||||
|
defer incViews(n)
|
||||||
n.prepare()
|
n.prepare()
|
||||||
views := n.Views
|
|
||||||
if val, ok := stats.Load(n.ID); ok {
|
|
||||||
intVal, ok := val.(int)
|
|
||||||
if ok {
|
|
||||||
views = intVal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer stats.Store(n.ID, views+1)
|
|
||||||
if fraudelent(n) {
|
if fraudelent(n) {
|
||||||
n.Ads = mdTmplHTML(ads)
|
n.Ads = mdTmplHTML(ads)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
|
|
||||||
const statsSavingInterval = 1 * time.Minute
|
const statsSavingInterval = 1 * time.Minute
|
||||||
|
|
||||||
func persistStats(logger echo.Logger, db *sql.DB, stats *sync.Map) {
|
var stats = &sync.Map{}
|
||||||
|
|
||||||
|
func persistStats(logger echo.Logger, db *sql.DB) {
|
||||||
for {
|
for {
|
||||||
time.Sleep(statsSavingInterval)
|
time.Sleep(statsSavingInterval)
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
@@ -35,3 +37,14 @@ func persistStats(logger echo.Logger, db *sql.DB, stats *sync.Map) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func incViews(n *Note) {
|
||||||
|
views := n.Views
|
||||||
|
if val, ok := stats.Load(n.ID); ok {
|
||||||
|
intVal, ok := val.(int)
|
||||||
|
if ok {
|
||||||
|
views = intVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer stats.Store(n.ID, views+1)
|
||||||
|
}
|
||||||
|
|||||||
+1
-4
@@ -19,10 +19,7 @@ func init() {
|
|||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const idLength = 5
|
||||||
idLength = 5
|
|
||||||
fraudThreshold = 7
|
|
||||||
)
|
|
||||||
|
|
||||||
type Note struct {
|
type Note struct {
|
||||||
ID, Title, Text, Password, DeprecatedPassword string
|
ID, Title, Text, Password, DeprecatedPassword string
|
||||||
|
|||||||
Reference in New Issue
Block a user