/
ogma-prime.go
316 lines (257 loc) · 7.26 KB
/
ogma-prime.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/codegangsta/cli"
cayleyConfig "github.com/google/cayley/config"
cayleyDb "github.com/google/cayley/db"
cayleyGraph "github.com/google/cayley/graph"
cayleyGremlin "github.com/google/cayley/query/gremlin"
cayleyQuery "github.com/google/cayley/query"
_ "github.com/google/cayley/graph/mongo"
_ "github.com/google/cayley/writer"
"github.com/gorilla/mux"
log "github.com/Sirupsen/logrus"
"gopkg.in/mgo.v2"
// "gopkg.in/mgo.v2/bson"
)
// Filled in by `go build -ldflags="-X main.Version `ver`"`.
var (
BuildDate string
Version string
)
type duration time.Duration
type ogmaPrimeConfig struct {
DatabaseType string `json:"database_type"`
DatabasePath string `json:"database_string"`
ListenHost string `json:"listen_host"`
ListenPort string `json:"listen_port"`
Timeout duration `json:"timeout"`
}
func (config *ogmaPrimeConfig) CayleyConfig() (cayley *cayleyConfig.Config) {
cayley = &cayleyConfig.Config{
DatabaseType: config.DatabaseType,
DatabasePath: config.DatabasePath,
ReplicationType: "single",
Timeout: time.Duration(20 * time.Second),
}
return
}
func loadConfigOn(c *cli.Context) (config *ogmaPrimeConfig) {
config, err := loadConfigFrom(c.GlobalString("config"))
if err != nil {
log.Fatalf("Cannot load configuration file: %v", err)
}
return
}
func loadConfigFrom(file string) (*ogmaPrimeConfig, error) {
config := &ogmaPrimeConfig{}
if file == "" {
return config, nil
}
if _, err := os.Stat(file); os.IsNotExist(err) {
log.Fatalf("Cannot find specified configuration file %q, aborting", file)
}
hnd, err := os.Open(file)
if err != nil {
return nil, fmt.Errorf("could not open config file %q: %v", file, err)
}
defer hnd.Close()
decoder := json.NewDecoder(hnd)
err = decoder.Decode(config)
if err != nil {
return nil, fmt.Errorf("could not parse config file %q: %v", file, err)
}
setConfigDefaults(config)
return config, nil
}
func setConfigDefaults(config *ogmaPrimeConfig) {
if config.DatabaseType == "" {
config.DatabaseType = "mongo"
}
if config.DatabasePath == "" {
config.DatabasePath = "localhost:27017"
}
if config.ListenHost == "" {
config.ListenHost = "0.0.0.0"
}
if config.ListenPort == "" {
config.ListenPort = "22327"
}
}
func main() {
ogma := cli.NewApp()
ogma.Name = "ogma"
ogma.Version = Version
ogma.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "./data/config.json",
Usage: "Path to a configuration file",
EnvVar: "OGMA_PRIME_CONFIG",
},
cli.BoolFlag{
Name: "dump-profile",
Usage: "Dump profiling information to a file",
},
}
ogma.Commands = []cli.Command{
{
Name: "dump",
Usage: "Dump Cayley contents",
Action: dumpAction,
},
{
Name: "init",
Usage: "Bootstrap and initialize",
Action: initAction,
},
{
Name: "show-config",
Usage: "Show configuration settings and exit",
Action: showConfigAction,
},
{
Name: "serve",
Aliases: []string{"s", "srv"},
Usage: "Serve HTTP",
Action: serveAction,
},
}
ogma.Run(os.Args)
}
func initAction(c *cli.Context) {
config := loadConfigOn(c)
err := cayleyDb.Init(config.CayleyConfig())
if err != nil {
log.Fatalf("Could not bootstrap database: %v", err)
}
}
func mongoSession(config *ogmaPrimeConfig) (session *mgo.Session, err error) {
session, err = mgo.Dial(config.DatabasePath)
return
}
func mongoShow(config *ogmaPrimeConfig) {
session, err := mongoSession(config)
if err != nil {
log.Fatalf("Cannot connect to MongoDB: %v", err)
}
// dbs, err := session.DatabaseNames()
// if err != nil {
// log.Fatalf("Cannot retrieve database names: %v\n", err)
// }
// fmt.Printf("Available databases are: %v\n", dbs)
db := session.DB("cayley")
collection := db.C("quads")
query := collection.Find(nil)
count, err := query.Count()
if err != nil {
log.Errorf("Cannot count result set: %v", err)
}
fmt.Printf("Found %d results:\n", count)
cursor := query.Iter()
var result *interface{}
for cursor.Next(&result) {
fmt.Printf("%v\n", result)
}
if err = cursor.Close(); err != nil {
log.Fatalf("Cannot close cursor: %v", err)
}
}
func dumpAction(c *cli.Context) {
config := loadConfigOn(c)
mongoShow(config)
}
func showConfigAction(c *cli.Context) {
config := loadConfigOn(c)
dump, err := json.Marshal(config)
if err != nil {
log.Fatalf("Cannot dump configuration data: %v", err)
}
var buf bytes.Buffer
json.Indent(&buf, dump, "", " ")
buf.WriteString("\n")
buf.WriteTo(os.Stdout)
}
func serveAction(c *cli.Context) {
config := loadConfigOn(c)
cayConfig := config.CayleyConfig()
graph, err := cayleyDb.Open(cayConfig)
if err != nil {
log.Fatalf("Cannot open database: %v", err)
}
http.Handle("/", serveInstallRoutes(graph, cayConfig))
log.Infof("Listening on %s:%s", config.ListenHost, config.ListenPort)
err = http.ListenAndServe(fmt.Sprintf("%s:%s", config.ListenHost, config.ListenPort), nil)
if err != nil {
log.Fatalf("Cannot listen and serve on %s:%s: %v", config.ListenHost, config.ListenPort, err)
}
}
func serveInstallApiV1(router *mux.Router, graph *cayleyGraph.Handle) {
router.HandleFunc("/properties/{id}", apiLogger(graph, findProperty)).Methods("GET")
}
func serveInstallRoutes(graph *cayleyGraph.Handle, config *cayleyConfig.Config) *mux.Router {
root := mux.NewRouter()
serveInstallApiV1(root.PathPrefix("/api/v1").Subrouter(), graph)
return root
}
type apiHandler func(http.ResponseWriter, *http.Request)
type graphHandler func(graph *cayleyGraph.Handle, rsp http.ResponseWriter, req *http.Request) int
func apiLogger(graph *cayleyGraph.Handle, handler graphHandler) apiHandler {
return func(rsp http.ResponseWriter, req *http.Request) {
start := time.Now()
addr := req.Header.Get("X-Real-IP")
if addr == "" {
addr = req.Header.Get("X-Forwarded-For")
if addr == "" {
addr = req.RemoteAddr
}
}
retcode := handler(graph, rsp, req)
log.Infof("%s %s %d %v %s", req.Method, addr, retcode, time.Since(start), req.URL.Path)
}
}
func findProperty(graph *cayleyGraph.Handle, rsp http.ResponseWriter, req *http.Request) int {
result := make(map[string]interface{})
retcode := 200
pathVars := mux.Vars(req)
result["request"] = pathVars
session := cayleyGremlin.NewSession(graph.QuadStore, time.Duration(10 * time.Second), false)
gremlinQuery := fmt.Sprintf(`g.V("/properties/%s").All()`, pathVars["id"])
queryResult, err := session.Parse(gremlinQuery)
switch queryResult {
case cayleyQuery.Parsed:
output, err := runGremlinQuery(gremlinQuery, session)
if err != nil {
result["success"] = false
result["error"] = err
retcode = 400
break
}
result["success"] = true
result["output"] = output
case cayleyQuery.ParseFail:
result["success"] = false
result["error"] = fmt.Sprintf("Failed to parse query: %s", err)
retcode = 400
default:
result["success"] = false
result["error"] = "Possibly incomplete data or query?"
retcode = 500
}
bytes, _ := json.MarshalIndent(result, "", " ")
fmt.Fprintln(rsp, string(bytes))
return retcode
}
func runGremlinQuery(q string, session cayleyQuery.HTTP) (interface{}, error) {
c := make(chan interface{}, 5)
go session.Execute(q, c, 100)
for result := range c {
session.Collate(result)
}
return session.Results()
}