forked from osuripple/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
91 lines (81 loc) · 1.92 KB
/
simple.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"errors"
"fmt"
"net/url"
"github.com/gin-gonic/gin"
)
func loadSimplePages(r *gin.Engine) {
for _, el := range simplePages {
if el.Handler == "" {
continue
}
r.GET(el.Handler, simplePageFunc(el))
}
}
func simplePageFunc(p templateConfig) gin.HandlerFunc {
return func(c *gin.Context) {
s := getContext(c)
if s.User.Privileges&p.mp() != p.mp() {
resp403(c)
return
}
simple(c, p, nil, nil)
}
}
func resp403(c *gin.Context) {
if getContext(c).User.ID == 0 {
ru := c.Request.URL
addMessage(c, warningMessage{T(c, "You need to login first.")})
getSession(c).Save()
c.Redirect(302, "/login?redir="+url.QueryEscape(ru.Path+"?"+ru.RawQuery))
return
}
respEmpty(c, "Forbidden", warningMessage{T(c, "You should not be 'round here.")})
}
func simpleReply(c *gin.Context, errs ...message) error {
t := getSimple(c.Request.URL.Path)
if t.Handler == "" {
return errors.New("simpleReply: simplepage not found")
}
simple(c, t, errs, nil)
return nil
}
func getSimple(h string) templateConfig {
for _, s := range simplePages {
if s.Handler == h {
return s
}
}
fmt.Println("oh handler shit not found", h)
return templateConfig{}
}
func getSimpleByFilename(f string) templateConfig {
for _, s := range simplePages {
if s.Template == f {
return s
}
}
fmt.Println("oh shit not found", f)
return templateConfig{}
}
func simple(c *gin.Context, p templateConfig, errs []message, requestInfo map[string]interface{}) {
resp(c, 200, p.Template, &baseTemplateData{
TitleBar: p.TitleBar,
KyutGrill: p.KyutGrill,
Scripts: p.additionalJS(),
HeadingOnRight: p.HugeHeadingRight,
FormData: normaliseURLValues(c.Request.PostForm),
RequestInfo: requestInfo,
Messages: errs,
})
}
func normaliseURLValues(uv url.Values) map[string]string {
m := make(map[string]string, len(uv))
for k, v := range uv {
if len(v) > 0 {
m[k] = v[0]
}
}
return m
}