-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_get_home.go
74 lines (64 loc) · 1.72 KB
/
http_get_home.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
66
67
68
69
70
71
72
73
74
package crudui
import (
"bytes"
"embed"
"log"
"net/http"
"text/template"
)
func (c *Controller) tryGetHome(w http.ResponseWriter, r *http.Request, uri string, objFuncs ...func() interface{}) bool {
realURI := c.getRealURI(uri, r.RequestURI)
if realURI == "" {
c.renderMain(w, r, uri, objFuncs...)
return true
}
return false
}
func (c *Controller) renderMain(w http.ResponseWriter, r *http.Request, uri string, objFuncs ...func() interface{}) {
configCss, _ := embed.FS.ReadFile(htmlDir, "html/config.css")
stylesCss, _ := embed.FS.ReadFile(htmlDir, "html/styles.css")
indexTpl, _ := embed.FS.ReadFile(htmlDir, "html/index.html")
structListTpl, err := c.getStructListHTML(uri, r, objFuncs...)
if err != nil {
log.Print(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
contentHomeTpl, _ := embed.FS.ReadFile(htmlDir, "html/content_home.html")
ctxId := r.Context().Value(ContextValue("LoggedUserID"))
ctxName := r.Context().Value(ContextValue("LoggedUserName"))
userId := "0"
if ctxId != nil {
userId = ctxId.(string)
}
userName := ""
if ctxName != nil {
userName = ctxName.(string)
}
tplObj := struct {
URI string
StructList string
Content string
ConfigCss string
StylesCss string
Username string
UserID string
}{
URI: uri,
StructList: structListTpl,
Content: string(contentHomeTpl),
ConfigCss: string(configCss),
StylesCss: string(stylesCss),
Username: userName,
UserID: userId,
}
buf := &bytes.Buffer{}
t := template.Must(template.New("index").Parse(string(indexTpl)))
err = t.Execute(buf, &tplObj)
if err != nil {
log.Print(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(buf.Bytes())
}