aboutsummaryrefslogtreecommitdiff
path: root/backend/data.go
blob: 3bc93d11c3916f4ea6194389bdae786b4abe9d03 (plain)
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
package backend

import (
	"fmt"
	"html/template"
	"net/http"
	"strings"
)

type data struct {
	title       string
	Article     bool
	Domain      string
	URL         string
	Image       string
	Description string
}

func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string) {
	if d.Domain == "" {
		cfg := r.Context().Value("config").(*Config)
		d.Domain = cfg.Domain
	}
	if d.URL == "" {
		d.URL = strings.TrimPrefix(r.URL.Path, "/")
	}
	t, err := template.New("").Funcs(template.FuncMap{
		"static": func(path string) string {
			return fmt.Sprintf("/static/%s", path)
		},
		"assets": func(path string) string {
			return fmt.Sprintf("/assets/%s", path)
		},
	}).ParseFS(templates, fmt.Sprintf("templates/%s.html", name), "templates/base.html")
	if err != nil {
		panic(err)
	}
	err = t.ExecuteTemplate(w, "base.html", d)
	if err != nil {
		panic(err)
	}
}

func (d *data) Title() string {
	title := "anhgelus"
	if d.Article {
		title += " - log entry"
	}
	if len(d.title) != 0 {
		title += " - " + d.title
	}
	return title
}