aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/index.go
blob: b90faf8f00653e84cca5d735223996dfa56e037d (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
package main

import (
	"html/template"
	"net/http"
	_ "embed"

	"riedstra.dev/mitch/steam-export/steam"
)

var (
	//go:embed templates/index.html
	indexTemplate string

	Templ = template.Must(template.New("index").Parse(indexTemplate))
)

func index(w http.ResponseWriter, r *http.Request) {
	// During rendering of the template I believe it's
	// mutating during the sort of keys, so Lib no longer
	// is an RWMutex and we're just locking this as if
	// we're writing to it
	Lib.Lock()
	defer Lib.Unlock()
	status.Lock()
	defer status.Unlock()

	err := Templ.ExecuteTemplate(w, "index",
		struct {
			Lib     *steam.Library
			Info    *statusInfo
			Local   bool
			HostIP  string
			Port    string
			Version string
		}{
			&Lib.Library,
			status,
			isLocal(r.RemoteAddr),
			getHostIP(),
			getPort(),
			Version,
		})
	if err != nil {
		Logger.Printf("While Rendering template: %s", err)
	}
	Logger.Printf("Client %s Index page", r.RemoteAddr)
}