-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.go
156 lines (100 loc) · 2.8 KB
/
modules.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package graph
import (
"fmt"
"strings"
//
"github.com/golangdaddy/tarantula/web"
//"github.com/golangdaddy/tarantula/router/common"
)
func (db *Database) MOD_authenticate(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
auths := strings.Split(req.GetHeader("Authorization"), " ")
username := auths[0]
token := auths[1]
ok, session := db.FindSession(token)
if !ok {
return req.Respond(403, "NO SESSION FOUND FOR: "+username)
}
q := fmt.Sprintf("SELECT * FROM %v WHERE uid = ?;", TABLE_USERS)
ok, user := db.Client.QueryUser(q, session.User)
if !ok {
return req.Fail()
}
if username != user.Name {
return req.Respond(403, "UNAUTHORIZED API CALL")
}
req.SetParam("$user", user)
return nil
}
func (db *Database) MOD_return(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
key := arg.(string)
return req.Respond(req.Param(key))
}
func (db *Database) MOD_lookupVertex(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
outputKey := arg.(string)
uid := req.Param("uid").(int64)
ok, vertex := db.Client.QueryVertex(uid)
if !ok {
return req.Fail()
}
req.SetParam(outputKey, vertex)
return nil
}
type NewVertex struct {
Class *Class
ParamKey string
}
func (db *Database) MOD_createVertex(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
args := arg.(*NewVertex)
var X string
if req.BodyParam("_") != nil {
X = req.BodyParam("_").(string)
if len(X) == 0 {
return req.Respond(400, "VERTEX _ (X) REFERENCE IS ZERO LENGTH")
}
} else {
X = random()
}
data := map[string]interface{}{}
for _, property := range args.Class.Properties {
param := req.BodyParam(property.Key)
if param == nil {
req.Log().NewError("FAILED TO FIND BODY PARAM " + args.Class.Name + ":" + property.Key)
continue
}
data[property.Key] = param
}
vertex := args.Class.NewVertex(X, data)
if !vertex.Insert() {
return req.Fail()
}
req.SetParam(args.ParamKey, vertex)
req.Log().Debug(fmt.Sprintf("CREATED VERTEX: %v", vertex.Uid))
return nil
}
type Edge struct {
Phase bool
InKey string
Predicate string
OutKey string
State bool
}
func (db *Database) MOD_edgeState(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
args := arg.(*Edge)
predicate := db.GetPredicate(args.Predicate)
in := req.Param(args.InKey).(*Vertex)
out := req.Param(args.OutKey).(*Vertex)
if args.Phase {
in.SetEdgeState(out, predicate.Name, args.State)
} else {
out.SetEdgeState(in, predicate.Name, args.State)
}
return nil
}
func (db *Database) MOD_getProperty(req web.RequestInterface, arg interface{}) *web.ResponseStatus {
key := arg.(string)
subject := req.Param("$subject").(*Vertex)
if !subject.LoadProperties(key) {
return req.Fail()
}
return req.Respond(subject.GetProperty(key))
}