-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
137 lines (114 loc) · 2.42 KB
/
data.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"fmt"
"os"
"strconv"
"time"
"github.com/go-redis/redis/v8"
)
type storeipdata struct {
e bool // exists
ip string
ulat uint16
ulon uint16
ts int64
}
var rdb *redis.Client
var ctx = context.Background()
func initdb() {
// addr := os.Getenv("RedisAddr")
// if addr == "" {
// addr = "localhost:6379" // Use default Redis address if not provided
// }
rdb = redis.NewClient(&redis.Options{
Addr: os.Getenv("RedisAddr"),
Password: os.Getenv("RedisPass"),
DB: 0, // use default DB
})
fmt.Println("Database client connected")
}
func pulldata(ip string) *storeipdata {
valc := rdb.HGetAll(ctx, ip)
val, err := valc.Result()
if err != nil {
return &storeipdata{e: false}
}
ulatp, err := strconv.ParseUint(val["ulat"], 36, 16)
if err != nil {
return &storeipdata{e: false}
}
ulat := uint16(ulatp)
ulonp, err := strconv.ParseUint(val["ulon"], 36, 16)
if err != nil {
return &storeipdata{e: false}
}
ulon := uint16(ulonp)
ts, err := strconv.ParseInt(val["ts"], 36, 64)
if err != nil {
return &storeipdata{e: false}
}
return &storeipdata{
e: true,
ip: ip,
ulat: ulat,
ulon: ulon,
ts: ts,
}
}
func pullall() *[]ipdata {
kc := rdb.Keys(ctx, "*")
k, err := kc.Result()
if err != nil {
return &[]ipdata{}
}
ret := []ipdata{}
for i := 0; i < len(k); i++ {
data := pulldata(k[i])
ret = append(ret, *storeDataToIPData(data))
}
return &ret
}
func storedata(data *storeipdata) bool {
if !data.e {
return false
}
// convert to strings
ulat := strconv.FormatUint(uint64(data.ulat), 36)
ulon := strconv.FormatUint(uint64(data.ulon), 36)
ts := strconv.FormatInt(data.ts, 36)
ret := rdb.HSet(ctx, data.ip, map[string]interface{}{"ulat": ulat, "ulon": ulon, "ts": ts})
_, err := ret.Result()
return err == nil // return true if no error
}
func removeip(ip string) bool {
res, err := rdb.Del(ctx, ip).Result()
if err != nil || res != 1 {
return false
}
return true
}
func storeDataToIPData(storedata *storeipdata) *ipdata {
return &ipdata{
OK: storedata.e,
IP: storedata.ip,
Ulat: storedata.ulat,
Ulon: storedata.ulon,
}
}
func IPDatatoStoreData(data *ipdata, timestamp int64) *storeipdata {
return &storeipdata{
e: data.OK,
ip: data.IP,
ulat: data.Ulat,
ulon: data.Ulon,
ts: timestamp,
}
}
func countVisits() {
rdb.Incr(ctx, "visits")
}
// utility
func getTS() int64 {
return time.Now().UnixMilli()
}