aboutsummaryrefslogtreecommitdiff
path: root/page/render.go
blob: 0ae5334e6f7f3b0bbba2d7cab32758e5803272d8 (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
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
114
115
package page

import (
	"bytes"
	"errors"
	"io/fs"
	"net/http"
)

// Render5xx is automatically called if any render fails,
// additionally you can call it for your own uses. It will
// try to use the 5xx template but will fall back to a plain
// page if that fails.
func Render5xx(w http.ResponseWriter, r *http.Request,
	vars map[string]interface{}, statusCode int) {
	p := NewPage(TemplateDirectory + "/5xx")

	buf := &bytes.Buffer{}

	err := p.Render(buf)
	if err != nil {
		logErr(r, "while trying 5xx", err)
		http.Error(w, "Internal server error", statusCode)

		return
	}

	w.WriteHeader(statusCode)

	_, _ = w.Write(buf.Bytes())

	logReq(r, statusCode)
}

// Render4xx is mostly used to render a 404 page, pulls from 404 template.
// automatically called by Render if a page is missing.
func Render4xx(w http.ResponseWriter, r *http.Request,
	vars map[string]interface{}, statusCode int) {
	p := NewPage(TemplateDirectory + "/4xx")

	buf := &bytes.Buffer{}

	err := p.Render(buf)
	if err != nil {
		logErr(r, "while trying 404", err)
		Render5xx(w, r, vars, http.StatusInternalServerError)

		return
	}

	w.WriteHeader(statusCode)

	_, err = w.Write(buf.Bytes())
	if err != nil {
		Render5xx(w, r, nil, http.StatusInternalServerError)

		return
	}

	logReq(r, statusCode)
}

// Render is a lower level option, allowing you to specify local
// variables and the status code in which to return.
func Render(w http.ResponseWriter, r *http.Request,
	path string, vars map[string]interface{}, statusCode int) {
	// Sepcifically use the specified path for the page
	p := NewPage(path)

	if vars != nil {
		p.Vars = vars
	}

	buf := &bytes.Buffer{}

	err := p.Render(buf)
	if err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			Render4xx(w, r, vars, http.StatusNotFound)

			return
		}

		logErr(r, "rendering encountered", err)
		Render5xx(w, r, vars, http.StatusInternalServerError)

		return
	}

	w.WriteHeader(statusCode)

	_, err = w.Write(buf.Bytes())
	if err != nil {
		logErr(r, "while writing to buf", err)
		Render5xx(w, r, nil, http.StatusInternalServerError)

		return
	}

	logReq(r, statusCode)
}

// RenderWithVars allows you to specify a specific page and whether or not
// you wish to override vars. If left nil they will not be overridden.
// Also see RenderForPath if you don't need to override them.
func RenderWithVars(w http.ResponseWriter, r *http.Request,
	path string, vars map[string]interface{}) {
	Render(w, r, path, vars, http.StatusOK)
}

// RenderForPath takes the path to a page and finish up the rendering
// Allowing you to place logic on what page is rendered by your handlers.
func RenderForPath(w http.ResponseWriter, r *http.Request, path string) {
	RenderWithVars(w, r, path, nil)
}