-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
83 lines (70 loc) · 1.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"net"
"github.com/kataras/iris"
"github.com/tidwall/buntdb"
)
const (
keyUserID = "user_id"
)
var getCurrentUser func(ctx *iris.Context) *User
func makeGetCurrentUser(db *buntdb.DB) func(ctx *iris.Context) *User {
return func(ctx *iris.Context) *User {
id := getUserID(ctx)
user, err := findUserByID(db, id)
if err != nil {
return nil
}
return user
}
}
func getUserID(ctx *iris.Context) string {
val := ctx.Session().Get(keyUserID)
if val != nil {
fmt.Printf("user_id from session: %v\n", val)
s, ok := val.(string)
if ok && len(s) > 0 {
return s
}
}
val = ctx.Get(keyUserID)
if val != nil {
fmt.Printf("user_id from request context: %v\n", val)
s, ok := val.(string)
if ok && len(s) > 0 {
return s
}
}
s := ctx.GetCookie(keyUserID)
if len(s) > 0 {
fmt.Printf("user_id from cookie: %v\n", val)
return s
}
return "robot"
}
func setUser(ctx *iris.Context, user *User) {
// TODO generate token instead of user id
ctx.Set(keyUserID, user.ID)
ctx.Session().Set(keyUserID, user.ID)
ctx.SetCookieKV(keyUserID, user.ID)
}
func logError(ctx *iris.Context, message string) {
payload := string(ctx.Request.Body())
fmt.Printf("%s: %s", message, payload)
}
func sendError(ctx *iris.Context, err error) {
fmt.Printf("error: %v", err)
// TODO classify errors
ctx.Error(err.Error(), 404)
}
func realIP(ctx *iris.Context) net.IP {
ip := ctx.RemoteIP()
fmt.Printf("RemoteIP: %s\n", ip.String())
b := ctx.Request.Header.Peek("X-Real-IP")
if b != nil && len(b) > 0 {
fmt.Printf("X-Real-IP: %s\n", string(b))
return net.ParseIP(string(b))
}
return ip
}