This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (52 loc) · 2.21 KB
/
index.js
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
'use strict';
const { aql, query, db } = require("@arangodb");
const createRouter = require('@arangodb/foxx/router');
const users = require("@arangodb/users");
const router = createRouter();
const joi = require('joi');
const aisisInstances = "aisisInstances"
module.context.use(router);
router.post('/createDB', function (req,res) {
const data = req.body;
const dbName = data.dbName ? data.dbName : randomStringGenerator();
const username = data.username ? data.username : randomStringGenerator();
const password = data.password ? data.password : randomStringGenerator();
const email = data.email ? data.email : '[email protected]'
// If user doesn't exist, create the user
try {
users.document(username);
// If user does exist send error response.
res.send("User already exists, please supply new username.").status(409);
} catch(err) {
// Handle duplicate database name.
try {
db._createDatabase(dbName);
} catch (err) {
res.send("Database already exists or invalid name supplied, please supply new dbName.").status(400);
}
users.save(username, password, true);
// Grants user access only to newly created database
users.grantDatabase(username, dbName, 'rw');
// @TODO: obtain hostname and port of external deployment if necessary.
// Returns hostname and port
let hostname = req.hostname
let port = req.port
let insertDoc = query`INSERT {
"dbName": ${dbName},
"username": ${username},
"hostname": ${hostname},
"port": ${port},
"email": ${email},
"timestamp": DATE_NOW()
} INTO aisisInstances`
res.send({dbName, username, password, hostname, port});
}
})
.body(joi.object().required(), 'Creates a new database, optionally provide dbName, username, or password ')
.response(joi.object().required(), 'Returns database name, username, and password.')
.summary('Creates a database and returns name and login credentials.')
.description('Creates a database with potentially randomly generated dbName, username, and password. ');
function randomStringGenerator() {
// Database name must start with letter.
return "ML" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}