-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreens.go
65 lines (56 loc) · 1.68 KB
/
screens.go
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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"path/filepath"
"strings"
"github.com/nikolalohinski/gonja/v2"
"github.com/nikolalohinski/gonja/v2/exec"
)
var screenDir = flag.String("screendir", "./screens", "directory where screen .html files are located")
func api_view(w http.ResponseWriter, req *http.Request) {
screen_name := req.PathValue("screen")
full_screen_name := filepath.Join(*screenDir, fmt.Sprintf("%s.html", screen_name))
if screen_name == "" {
// TODO: go to a home page or something
w.WriteHeader(http.StatusNotFound)
return
}
subtemplate, err := gonja.FromFile(full_screen_name)
if err != nil || subtemplate == nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "could not get screen html file\n")
io.WriteString(w, err.Error())
return
}
page_data, err := subtemplate.ExecuteToString(nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "could not get execute page\n")
io.WriteString(w, err.Error())
return
}
template, err := gonja.FromFile(filepath.Join(*screenDir, "main.html"))
if err != nil || subtemplate == nil {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "could not get main screen html file\n")
io.WriteString(w, err.Error())
return
}
dat := exec.NewContext(map[string]any{
"Title": screen_name,
"Body": page_data,
})
final_page_data := strings.Builder{}
err = template.Execute(&final_page_data, dat)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "could not get execute main page with content\n")
io.WriteString(w, err.Error())
return
}
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, final_page_data.String())
}