-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
132 lines (109 loc) · 3.24 KB
/
server.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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const path = require("path");
const axios = require("axios");
var util = require("util");
const molecules = require("./routes/api/molecules/molecule");
const users = require("./routes/api/users/user");
const app = express();
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// DB Config
const keys = require("./config/keys_dev");
const db = keys.mongoURI;
// Connect to MongoDB
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB Connected"))
.catch(err => console.log(err));
// Passport middleware
app.use(passport.initialize());
// Passport config
require("./config/passport")(passport);
// API endpoints
app.use("/api/molecules/molecule", molecules);
app.use("/api/users/user", users);
/*
SERVER SIDE REQ
*/
// Generate molecule - AZURE
var r;
app.post("/gen", (req, res) => {
axios
.post(`${keys.azure}`, req.body, {
headers: {
Authorization: "Bearer " + `${keys.token}`,
"Content-Type": "application/json"
}
})
.then(re => {
r = JSON.parse(JSON.stringify(util.inspect(re.data)));
var a_smiles = [];
for (var i = 0; i < re.data.smiles.length; i++) {
a_smiles.push(re.data.smiles[i]);
}
var a_pic50 = [];
for (var i = 0; i < re.data.pic50.length; i++) {
a_pic50.push(re.data.pic50[i]);
}
var a_qed = [];
for (var i = 0; i < re.data.qed.length; i++) {
a_qed.push(re.data.qed[i]);
}
var a_pic_dist = [];
for (var i = 0; i < re.data.pic50_dist.length; i++) {
a_pic_dist.push(re.data.pic50_dist[i]);
}
const result = {
smiles: a_smiles,
pic50: a_pic50,
qed: a_qed,
pic_dist: a_pic_dist,
n_generated: parseInt(util.inspect(re.data.n_generated))
};
//console.log(result);
return res.json(result);
})
.catch(err => console.log(err));
});
// Predictions - IBM
app.post("/pred_retro", (req, res) => {
//console.log(typeof req.body); -> object
//console.log(req.body);
axios
.post(`${keys.ibm}`, req.body, {
headers: {
Authorization: `${keys.ibm_token}`,
"Content-Type": "application/json",
Accept: "application/json"
}
})
.then(re => {
var s = JSON.parse(JSON.stringify(util.inspect(re.data.payload.id)));
//console.log(s);
return res.json(s);
})
.catch(err => console.log(err));
});
// Retrosynthesis - IBM
app.get("/get_retro", (req, res) => {
axios
.get(`https://rxn.res.ibm.com/rxn/api/api/v1/retrosynthesis/${req.body.id}`)
.then(re => {
return res.json(util.inspect(re));
})
.catch(err => console.log(err));
});
// Server static assets if in production
if (process.env.NODE_ENV === "production") {
// Set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}`));