-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_keys.go
85 lines (70 loc) · 1.91 KB
/
access_keys.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
package controllers
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"github.com/willyrgf/rerest/api/middleware"
"github.com/willyrgf/rerest/api/responses"
"github.com/willyrgf/rerest/config"
"github.com/willyrgf/rerest/pkg/account"
"github.com/willyrgf/rerest/pkg/repository"
)
func getKeyPartsFromURL(ctx *fasthttp.RequestCtx) (kp, ks, k string, ok bool) {
var v interface{}
v = ctx.UserValue("key_prefix")
kp, ok = v.(string)
if !ok {
return
}
v = ctx.UserValue("key_suffix")
ks, ok = v.(string)
if !ok {
return
}
k = fmt.Sprintf("%s:%s", kp, ks)
if k != "" || kp != "" || ks != "" {
ok = true
}
return
}
// GetKey is a controller for get any key
func GetKey(ctx *fasthttp.RequestCtx) {
keyPrefix, keySuffix, key, ok := getKeyPartsFromURL(ctx)
if !ok {
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
return
}
keyConf, ok := config.RerestConf.Access[keyPrefix]
if !ok {
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
return
}
log.Debugf("controllers.GetKey(): key_prefix=%s; key_suffix=%s; key=%s keyconf=%+v\n", keyPrefix, keySuffix, key, keyConf)
switch keyConf.Type {
case "set":
members, err := repository.GetFromSet(key)
if err != nil {
ctx.Error("error on get key", fasthttp.StatusInternalServerError)
}
log.Debugf("controllers.GetKey(): set; members=%+v", members)
answered := responses.Strings(ctx, members)
if answered {
go account.Response(middleware.GetKeyOfCtx(ctx))
}
return
case "list":
members, err := repository.GetFromList(key)
if err != nil {
ctx.Error("error on get key", fasthttp.StatusInternalServerError)
}
log.Debugf("controllers.GetKey(): list; members=%+v", members)
answered := responses.Strings(ctx, members)
if answered {
go account.Response(middleware.GetKeyOfCtx(ctx))
}
return
default:
ctx.Error("Type of key unsupported", fasthttp.StatusInternalServerError)
return
}
}