-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (168 loc) · 4.77 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
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
require('dotenv').config()
const IPFS = require('ipfs')
const OrbitDB = require('orbit-db')
const NetworkIdentity = require('@nosplatform/orbitdb-nos-identity-provider').default;
const auth = require('./helpers/auth.js');
const schemaHelper = require("./helpers/schema");
const cors = require('cors')
const wrtc = require('wrtc');
const WebRTCStar = require('libp2p-webrtc-star');
const express = require('express');
const bodyParser = require('body-parser');
const Websockets = require("libp2p-websockets");
const app = express()
app.use(cors());
app.use(bodyParser.json({ extended: true }));
const schemaKey = process.env.SCHEMA;
const initIPFSInstance = async () => {
return await IPFS.create({
repo: './.ipfs',
relay: { enabled: true, hop: { enabled: true, active: true } },
EXPERIMENTAL: { pubsub: true },
config: {
Addresses: {
Swarm: [
"/dns4/hidden-sierra-49375.herokuapp.com/tcp/443/wss/p2p-webrtc-star/",
`/ip4/0.0.0.0/tcp/${process.env.IPFS_TCP_PORT}`,
`/ip4/0.0.0.0/tcp/${process.env.IPFS_WS_PORT}/ws`
]
}
},
libp2p: {
modules: {
transport: [WebRTCStar, Websockets]
},
config: {
peerDiscovery: {
webRTCStar: { // <- note the lower-case w - see https://github.com/libp2p/js-libp2p/issues/576
enabled: true
}
},
transport: {
WebRTCStar: { // <- note the upper-case w- see https://github.com/libp2p/js-libp2p/issues/576
wrtc
}
}
}
}
});
};
initIPFSInstance().then(async ipfs => {
// Get schema
const schema = await schemaHelper.resolveSchema(schemaKey);
const validate = schemaHelper.ajvSchema(schema);
const primaryKey = schemaHelper.getPrimaryKey(schema);
// Set up OrbitDB Identity Provider
const passphrase = process.env.PASSPHRASE;
OrbitDB.Identities.addIdentityProvider(NetworkIdentity);
const identity = await OrbitDB.Identities.createIdentity({ type: `CoreIdentityType`, passphrase })
const orbitdb = await OrbitDB.createInstance(ipfs, { identity });
// Create / Open a database
const options = {
indexBy: primaryKey
}
const db = await orbitdb.docs(process.env.SCHEMA, options);
await db.load();
console.log("Database hash: " + String(db.address).replace("/orbitdb/", "").replace(`/${schemaKey}`, ""));
app.use('/', auth);
app.post('/', async function (req, res) {
try {
const valid = validate(req.body);
if (valid) {
const primaryVal = req.body[primaryKey];
const entry = db.get(primaryVal);
// Replace previous
if (entry && Object.values(entry).length) {
await db.del(primaryVal);
}
const hash = await db.put(req.body);
if (hash) {
res.json({ hash });
} else {
res.status(422).json({
error: {
message: 'Data could not be added to database.'
}
});
}
} else {
res.status(422).json({
error: {
message: 'data does not pass schema validation.',
data: req.body
}
});
}
} catch (error) {
console.log(error);
res.status(500).json({
error: {
message: error.message,
data: req.body
}
});
}
})
app.get('/', async function (req, res) {
try {
if (req.query.id && String(req.query.id).length > 1) {
const entry = await db.get(req.query.id);
if (entry && entry.length) {
res.json(entry[0]);
} else {
res.status(404).json({
error: {
message: 'Entry not found.'
}
});
}
} else {
res.status(404).json({
error: {
message: 'Entry not found.'
}
});
}
} catch (error) {
console.log(error);
res.status(500).json({
error: {
message: error.message,
data: req.body
}
});
}
})
app.delete('/', async function (req, res) {
try {
if (Object.values(req.body).length === 1 && req.body.id) {
const hash = await db.del(req.body.id);
if (hash) {
res.status(200).json({ hash });
} else {
res.status(422).json({
error: {
message: 'Data could not be added to database.'
}
});
}
} else {
res.status(422).json({
error: {
message: 'Data does not pass schema validation.',
data: req.body
}
});
}
} catch (error) {
console.log(error);
res.status(500).json({
error: {
message: error.message,
data: req.body
}
});
}
})
app.listen(process.env.PORT);
});