-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.js
174 lines (142 loc) · 4.79 KB
/
backend.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
const express = require("express");
const expressJWT = require("express-jwt");
const Sentry = require("@sentry/node");
const { graphqlHTTP } = require("express-graphql");
const { graphqlUploadExpress } = require("graphql-upload");
const helmet = require("helmet");
const multer = require("multer");
const expressPlayground = require("graphql-playground-middleware-express").default; // for testing auth
const cors = require("cors");
const mongoose = require("mongoose");
const graphqlSchema = require("./graphql/schema");
const graphqlResolvers = require("./graphql/resolvers");
const { uploadFile, getFile } = require("./swift");
const uuid = require("uuid");
const { makeExecutableSchema } = require("@graphql-tools/schema");
require("dotenv").config();
const { DB, JWT_SECRET, NODE_ENV, SENTRY_URL } = process.env;
Sentry.init({ dsn: SENTRY_URL });
const app = express();
app.use(Sentry.Handlers.requestHandler());
app.use(cors());
const html = `
<!DOCTYPE html>
<head><title>nsf open data</title></head>
<body>
Hello World! This is a GraphQL API. Check out /graphql or /playground<br>
<form action="/upload" enctype="multipart/form-data" method="post">
<div>
<input type="text" placeholder="Object ID" name="id">
<input type="text" placeholder="Type: vehicle or animal" name="type">
<input type="file" name="images" multiple>
<input type="submit" value="upload">
</div>
</form>
</body>
</html>
`;
const schema = makeExecutableSchema({
typeDefs: graphqlSchema,
resolvers: graphqlResolvers,
});
app.get("/", (req, res) => {
res.send(html);
});
app.get("/playground", expressPlayground({ endpoint: "/graphql" }));
app.get("/debug-sentry", function mainHandler() {
throw new Error("My first Sentry error!");
});
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
/**
* validate if item with given id exists, used in uploads and downloads of files
* @param {String} id the id of item to check
* @param {String} item animal / vehicle to fetch the object from
* @returns the object if found else null
*/
// const checkValidID = async (id, item) => {
// item = item.toLowerCase();
// // map to the database model
// const object = {
// vehicle: Vehicle,
// animal: Animal,
// };
// // check if it is in the object
// if (!Object.prototype.hasOwnProperty.call(object, item)) return false;
// const found = await object[item].findById(id);
// return found;
// };
// endpoint to get images from the server
app.get("/file/:type/:id/:filename", async (req, res) => {
const { type, id, filename } = req.params;
console.log(`${filename} requested to be served`);
// verify uuid
if (!uuid.validate(id)) res.status(404).send(`no ${type} with that id found, type or id might be incorrect`);
try {
const prefix = `${type}/${id}`;
const file = await getFile(prefix, filename);
res.send(await file.buffer());
} catch (error) {
console.log(error);
res.sendStatus(error);
}
});
app.post("/upload", upload.array("images"), async (req, res) => {
//setFileName(`vehicle/${imagesID}/${fileChangeEvent.target.files[0].name}`);
const id = uuid.v4();
const { type } = req.body;
if (!type) res.status(400).send("Id field required");
// do the uploads
let uploads = [];
try {
for (let file of req.files) {
console.log(file);
var { originalname: name, buffer } = file;
// console.log("original buffer: ", buffer);
const prefix = `${type}/${id}`;
console.log(prefix);
// remove all spaces from name
name = name.replaceAll(" ", "");
const status = await uploadFile(prefix, name, buffer).catch(error => {
console.log(error);
throw error;
});
if (status === 201) {
console.log(status);
uploads.push(`${prefix}/${name}`);
} else {
console.log(status);
}
}
} catch (error) {
console.log(error);
throw error;
}
console.log("uploads: ", uploads);
res.status(200).send(uploads);
});
app.use(Sentry.Handlers.errorHandler());
app.use(helmet());
app.use(
expressJWT({
secret: JWT_SECRET,
algorithms: ["HS256"],
credentialsRequired: false,
})
);
app.use(
"/graphql",
graphqlUploadExpress({ maxFileSize: 10000000, maxFiles: 10 }),
graphqlHTTP({
schema: schema,
graphiql: true,
})
);
const uri = DB;
const options = {};
mongoose
.connect(uri, options)
.then(() => app.listen(3000, console.log(`Server is running, env: ${NODE_ENV || "development"}`)))
.catch(error => {
throw error;
});