aboutsummaryrefslogtreecommitdiff
path: root/template.go
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-07-14 21:46:15 +0000
committerFuwn <[email protected]>2021-07-14 21:46:15 +0000
commit80c553eb058fab6083d575559267fa4d8472727a (patch)
tree569d92a85ea0b608bb6634ac1c31fc1c3c5b85a7 /template.go
downloadspace-80c553eb058fab6083d575559267fa4d8472727a.tar.xz
space-80c553eb058fab6083d575559267fa4d8472727a.zip
feat(space): :gemini:
Diffstat (limited to 'template.go')
-rw-r--r--template.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/template.go b/template.go
new file mode 100644
index 0000000..69a7716
--- /dev/null
+++ b/template.go
@@ -0,0 +1,54 @@
+// Copyright (C) 2021-2021 Fuwn
+// SPDX-License-Identifier: GPL-3.0-only
+
+package main
+
+import (
+ "io"
+ "text/template"
+
+ "github.com/pitr/gig"
+ "github.com/spf13/viper"
+)
+
+type Template struct {
+ Templates *template.Template
+}
+
+type IndexTemplate struct {
+ Content string
+ Quote string
+ Hits int
+ Copyright string
+}
+type ErrorTemplate struct {
+ Error string
+ Content string
+ Quote string
+ Hits int
+ Copyright string
+}
+
+// Lazy...
+func isHitsEnabled() bool {
+ return viper.GetBool("space.hits")
+}
+func (_ IndexTemplate) HitsEnabled() bool {
+ return isHitsEnabled()
+}
+func (_ ErrorTemplate) HitsEnabled() bool {
+ return isHitsEnabled()
+}
+
+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
+ }
+ hitsTracker[c.Path()]++
+
+ return t.Templates.ExecuteTemplate(w, name, data)
+}