forked from cube-js/cube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (76 loc) · 2.81 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
const CubejsServerCore = require('@cubejs-backend/server-core');
const MySQLDriver = require('@cubejs-backend/mysql-driver');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const serveStatic = require('serve-static');
const session = require('express-session');
const passport = require('passport');
require('dotenv').config();
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(require('cors')());
const cubejsServer = CubejsServerCore.create({
externalDbType: 'mysql',
externalDriverFactory: () => new MySQLDriver({
host: process.env.CUBEJS_EXT_DB_HOST,
database: process.env.CUBEJS_EXT_DB_NAME,
port: process.env.CUBEJS_EXT_DB_PORT,
user: process.env.CUBEJS_EXT_DB_USER,
password: process.env.CUBEJS_EXT_DB_PASS,
}),
preAggregationsSchema: 'wa_pre_aggregations',
scheduledRefreshTimer: true
});
app.get('/healthy', (req, res) => {
res.json({ status: 'ok' });
});
app.use(session({ secret: process.env.CUBEJS_API_SECRET }));
app.use(passport.initialize());
app.use(passport.session());
if (process.env.GOOGLE_AUTH_DOMAIN) {
const { Strategy: GoogleStrategy } = require('passport-google-oauth20');
const authURL = process.env.NODE_ENV === 'production' ? `${process.env.GOOGLE_AUTH_REDIRECT}` : 'http://localhost:4000';
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_AUTH_CLIENT_ID,
clientSecret: process.env.GOOGLE_AUTH_CLIENT_SECRET,
callbackURL: `${authURL}/auth/google/callback`
},
(accessToken, refreshToken, profile, cb) => {
if (profile.emails && profile.emails.find(e => e.value.match(new RegExp(`@${process.env.GOOGLE_AUTH_DOMAIN}$`)) && e.verified)) {
return cb(null, profile);
}
return cb(`${profile.emails && profile.emails[0] && profile.emails[0].value} not within @${process.env.GOOGLE_AUTH_DOMAIN}`);
}));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/auth/google' }),
(req, res) => {
// Successful authentication, redirect home.
res.redirect('/');
});
app.use((req, res, next) => {
if (!req.user) {
res.redirect('/auth/google');
return;
}
next();
});
}
if (process.env.NODE_ENV === 'production') {
app.use(serveStatic(path.join(__dirname, 'dashboard-app/build')));
}
cubejsServer.initApp(app);
const port = process.env.PORT || 4000;
const server = http.createServer({}, app);
server.listen(port, () => {
console.log(`🚀 Cube.js server (${CubejsServerCore.version()}) is listening on ${port}`);
});