aboutsummaryrefslogtreecommitdiff
path: root/template.go
blob: 3d5687bcacc609028c06a7b38d02cf798970ed04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

package main

import (
	"io"
	"text/template"

	"github.com/fuwn/space/pkg/database"
	"github.com/pitr/gig"
	"github.com/spf13/viper"
)

type Template struct {
	Templates *template.Template
}

type IndexTemplate struct {
	Content string
	Quote   string
	Hits    int
	// SystemInfo string
	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.
	hits := database.Get(c.Path())
	if hits == 0 {
		database.Create(c.Path())
	}
	database.Increment(c.Path())

	return t.Templates.ExecuteTemplate(w, name, data)
}