summaryrefslogtreecommitdiffstats
path: root/api/handlers/html.go
blob: 57adb1292eaa5f671959dc4feab3d0b3ff6c94dd (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package handlers

import (
	"net/http"

	"git.leonardobishop.net/stash/pkg/entries"
	"git.leonardobishop.net/stash/pkg/html"
	"git.leonardobishop.net/stash/pkg/kinds"
)

const style = `<style>
html, body {
  -moz-text-size-adjust: none;
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
}

body {
  max-width: 800px;
  margin: 0 auto;
}

.entry-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.entry {
  display: grid;
  grid-template:
    'title date'
    'desc desc';
}

.entry-title {
  grid-area: title;
  font-size: medium;
}

.entry-date {
  grid-area: date;
  justify-self: end;
  font-size: medium;
}

.entry-description {
  grid-area: desc;
  font-size: small;
}
</style>`

func GetEntriesHtml(entriesService entries.Service, kindsService kinds.Service, htmlService html.Service) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var entries []entries.EntryRow
		kinds, err := kindsService.GetKinds()

		kind := r.URL.Query().Get("kind")
		if kind != "" {
			entries, err = entriesService.GetEntriesByKind(kind)
		} else {
			entries, err = entriesService.GetEntries()
		}
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		var html string
		page, err := htmlService.GenerateHtml(entries)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		if r.URL.Query().Get("css") == "no" {
			html = page
			goto send
		}

		html = `<!DOCTYPE html>
<html>
<head>
<title>Stash</title>
` + style + `
</head>
<body>`

		// gross
		html += "<p>Show: "
		if kind == "" {
			html += `<b>`
		}
		html += `<a href="/html">all</a> `
		if kind == "" {
			html += `</b>`
		}
		for _, k := range kinds {
			if kind == k.Name {
				html += `<b>`
			}
			html += `<a href="/html?kind=` + k.Name + `">` + k.Emoji + " " + k.Name + "</a> "
			if kind == k.Name {
				html += `</b>`
			}
		}
		html += "</p>" + page + "</body></html>"

	send:
		w.Header().Set("Content-Type", "text/html;charset=UTF-8")
		w.Write([]byte(html))
	}
}