/
app.js
193 lines (179 loc) · 5.55 KB
/
app.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
186
187
188
189
190
191
192
193
require('dotenv').config();
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var logger = require('morgan');
var exec = require('child_process').exec;
var Queue = require('bull');
var tmp = require('tmp');
const fs = require('fs');
var favicon = require('serve-favicon');
const redis = require('redis')
const session = require('express-session')
let RedisStore = require('connect-redis')(session)
let redisClient = redis.createClient(6379, "redis")
// Set the configuration settings for oauth
const credentials = {
client: {
id: process.env.ORCID_ID,
secret: process.env.ORCID_SECRET,
},
auth: {
tokenHost: 'https://orcid.org',
tokenPath: '/oauth/token',
authorizePath: '/oauth/authorize'
}
};
// Initialize the OAuth2 Library
const oauth2 = require('simple-oauth2').create(credentials);
var indexRouter = require('./routes/index');
var authRouter = require('./routes/auth');
var usersRouter = require('./routes/users');
var app = express();
app.set('trust proxy', 1);
app.use(
session({
store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { secure: true }
})
)
app.use(favicon(__dirname + '/public/img/favicon.ico'));
//define process queue
var metadbQueue = new Queue('execute pipeline', {
redis: {
host: 'redis',
port: 6379
}
});
// make oauth available in routers
app.locals.oauth2 = oauth2;
// make queue available in routers
app.locals.metadbQueue = metadbQueue;
metadbQueue.process(function(job, done){
let parameters = [];
let tmpdir = tmp.dirSync().name;
let userParameters = job.data.data.parameters;
let taxaFile = job.data.data.taxaFile;
let user = job.data.user;
if(!('outdir' in userParameters)){
done(null, {error: 'Error! No outdir string provided'});
return;
}
if(userParameters['outdir'].indexOf('/') !== -1){
done(null, {error: 'Error! Illegal character in output name: "/"'});
return;
}
if(userParameters['warn-failed-tax-names']){
parameters.push('--warn-failed-tax-names');
}
parameters.push('--outdir', userParameters['outdir']);
if(!('marker-search-string' in userParameters)){
done(null, {error: 'Error! No marker search string provided'});
return;
}
parameters.push('--marker-search-string', userParameters['marker-search-string']);
if('taxonomic-range' in userParameters){
parameters.push('--taxonomic-range', userParameters['taxonomic-range']);
}
parameters.push(
'--zip',
'--check-tax-names',
'--zenodo-token',
'/bcdatabaser/bcdatabaser/.zenodo_token',
'--zenodo-author-name',
user.name,
'--zenodo-author-orcid',
user.orcid,
'--sequence-length-filter',
'100:2000',
'--sequences-per-taxon',
'9'
);
if(taxaFile){
fs.writeFile(tmpdir+'/species_list.txt', taxaFile.data, (err) => {
if (err){
done(null, {error: 'Error! There was a problem with the taxa file you uploaded.'+err.message});
return;
}
parameters.push('--taxa-list', 'species_list.txt')
spawn_process(parameters, tmpdir, done);
});
} else {
spawn_process(parameters, tmpdir, done);
}
});
function spawn_process(parameters, tmpdir, done){
var spawn = require('child_process').spawn;
var process = spawn('/bcdatabaser/bcdatabaser/bin/bcdatabaser.pl', parameters, {'cwd': tmpdir});
var result = [];
var error = [];
let zenodo_info = {};
let stat_info = {};
process.stdin.end();
process.stdout.setEncoding('utf-8');
process.stdout.on('data', function (data) {
result.push(data)
});
process.stderr.setEncoding('utf-8');
process.stderr.on('data', function (data) {
let zenodo_pos = data.indexOf('zenodo_');
if(zenodo_pos !== -1){
zenodo_lines = data.split("\n");
for(zline of zenodo_lines){
if(zline.indexOf("zenodo_") !== 0){
continue;
}
let parts = zline.split(": ");
zenodo_info[parts[0]] = parts[1];
}
}
let stat_pos = data.indexOf('STAT\t');
if(stat_pos !== -1){
stat_lines = data.split("\n");
for(sline of stat_lines){
if(sline.indexOf("STAT\t") === -1){
continue;
}
let parts = sline.split("\t");
stat_info[parts[1]] = parts[2];
}
}
error.push(data)
})
process.on('close', function(code){
done(null,{data: result.join("\n"), error: error.join("\n"), zenodo_info: zenodo_info, stat_info: stat_info})
})
}
function logError(error){console.error(error)}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'twig');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'node_modules')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/auth', authRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;