-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (93 loc) · 3.01 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const { Observable, zip, fromEvent, merge, concat, of, bindNodeCallback } = require('rxjs');
const { pluck, map, concatMap, tap } = require('rxjs/operators');
const { spawn } = require('child_process');
const authRouter = require('./routes/authentication');
const passportSetup = require('./passport');
const passport = require('passport');
const port = process.env.PORT || 5000;
const sockets = require('socket.io');
const config = require('config');
const appDb = require('./db/db.apps');
const path = require('path');
var expressWinston = require('express-winston');
var winston = require('winston');
app.use(express.static(path.join(__dirname, './client/build')));
app.use(expressWinston.logger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
}),
new winston.transports.File({
filename: 'access.log',
level: 'info'
})
]
}));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
app.use('/auth', authRouter);
const server = app.listen(port, () => {
console.log("listening on port 5000 for app" + config.get('rxjs.app'));
})
const io = sockets(server);
app.post("/deploy", (req, res) => {
const requestResponseObservable = Observable.create((o) => {
o.next({ req, res });
});
const request = requestResponseObservable.pipe(pluck("req"));
const response = requestResponseObservable.pipe(pluck("res"));
const url = request.pipe(pluck("body"), pluck("url"),
tap((url) => console.log("url entered is ", url)));
const repoName = url.pipe(map((url) => url.split("/").pop().toLowerCase()),
tap((repo) => console.log("repo name is ", repo)));
const zipped = zip(url, repoName);
const emission = zipped.pipe(concatMap((data) => {
const command = spawn(`./script.sh`, [data[0], data[1]]);
storeData(data[0],data[1]);
const stdout = fromEvent(command.stdout, 'data');
const stderr = fromEvent(command.stderr, 'data');
return merge(stdout, stderr).pipe(map((data) => data.toString('utf-8')));
}));
emission.subscribe(
x => {
console.log('data', x);
io.emit('chat', x);
},
e => {
console.error("error object", e);
},
() => console.log("completed")
)
})
app.get("/apps", (req, res) => {
appDb.getUserApps().then((data) => {
res.json(data);
});
})
app.use(expressWinston.errorLogger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
}),
new winston.transports.File({
filename: 'access.log',
level: 'error'
})
]
}));
function storeData(url,appName){
appDb.addApp({
appId: Math.floor(Math.random()*100),
userId:"testUser",
app_name:appName,
timestamp:new Date(),
status:"ok",
app_URL:url
});
}