-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.go
64 lines (48 loc) · 1.19 KB
/
util.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
package api
import (
"encoding/json"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/assetto-corsa-web/acweb/resp"
"github.com/assetto-corsa-web/acweb/session"
"github.com/assetto-corsa-web/acweb/util"
)
func decode(w http.ResponseWriter, r *http.Request, req interface{}) bool {
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error decoding request")
resp.Error(w, 100, err.Error(), nil)
return true
}
return false
}
func success(w http.ResponseWriter) {
resp.Success(w, 0, "", nil)
}
func iserror(w http.ResponseWriter, err error) bool {
if err != nil {
operr, _ := err.(util.OpError)
resp.Error(w, operr.Code, operr.Msg, nil)
return true
}
return false
}
func isadmin(r *http.Request) bool {
s, _ := session.GetCurrentSession(r)
var admin bool
s.Get("admin", &admin)
return admin
}
func ismoderator(r *http.Request) bool {
s, _ := session.GetCurrentSession(r)
var admin, moderator bool
s.Get("admin", &admin)
if admin {
return true
}
s.Get("moderator", &moderator)
return moderator
}
func denyAccess(w http.ResponseWriter) {
resp.Error(w, 200, "Access denied", nil)
}