forked from jsdevkr/gitCodeShare.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
128 lines (112 loc) · 3.58 KB
/
server.ts
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
import passport from 'passport';
import express from 'express';
import session from 'express-session';
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import puppeteer from 'puppeteer';
import morgan from 'morgan';
import uuid from 'uuid/v4';
import path from 'path';
import { useStaticRendering } from 'mobx-react';
import { AuthHandler, GithubHandler, GistHandler, ImageHandler } from './handlers';
useStaticRendering(true);
process.on('SIGINT', () => process.exit());
const dev = process.env.NODE_ENV !== 'production';
const port = dev ? parseInt(process.env.BACKEND_PORT, 10) || 3030 : 3000;
const proxyContext = process.env.BACKEND_PROXY_CONTEXT || '/api';
const MemoryStore = require('memorystore')(session);
if (!dev) {
const LOGS_ID = `${process.env.LOGS_SECRET_PREFIX}:${process.env.NOW_URL}`;
require('now-logs')(LOGS_ID);
}
const puppeteerParams = dev
? {}
: {
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--disable-dev-shm-usage'],
};
const browser: { instance: puppeteer.Browser } = { instance: null };
const puppeteerLaunch = async () => {
browser.instance = await puppeteer.launch(puppeteerParams);
browser.instance.on('disconnected', puppeteerLaunch);
};
(async () => {
await puppeteerLaunch();
// set up
const server = express();
const imageHandler = ImageHandler(browser);
if (dev) {
server.use(morgan('tiny'));
}
server.use(cookieParser(process.env.COOKIE_SECRET || 'gitCodeShare'));
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
server.use(
session({
genid: function() {
return uuid();
},
store: new MemoryStore({
checkPeriod: 86400000, // prune expired entries every 24h
}),
secret: process.env.COOKIE_SECRET || 'gitCodeShare',
saveUninitialized: false, // don't create session until something stored,
resave: false, // don't save session if unmodified
cookie: {
signed: true,
maxAge: 1000 * 60 * 60 * 4,
httpOnly: true,
secure: false,
},
}),
);
server.use(passport.initialize());
server.use(passport.session());
server.use(`${proxyContext}/auth`, AuthHandler);
server.use(`${proxyContext}/github`, GithubHandler);
server.use(`${proxyContext}/gists`, GistHandler);
server.use(`${proxyContext}/image`, imageHandler);
// logout
server.get(`${proxyContext}/logout`, (req, res, next) => {
req.session.destroy(err => {
if (err) {
console.log(err);
return next(err);
}
});
req.logout();
res.redirect('/');
});
// production with next
if (!dev) {
const next = require('next');
const app = next({ dev });
await app.prepare();
const handle = app.getRequestHandler();
const filePath = path.join(__dirname, '.next', 'service-worker.js');
server.get('/service-worker.js', (req, res) => app.serveStatic(req, res, filePath));
// to next.js
server.get('*', handle as any);
} else {
// redirect to home
server.get('/', (req, res) => {
res.redirect(`${req.protocol}://${req.hostname}`);
});
}
// error handler
server.use((err, req, res, next) => {
if (!res.headersSent) {
if (typeof res.writeHead === 'function') {
res.writeHead(500, { 'content-type': 'application/json' });
}
}
const json = { error: 'api_error', reason: err.message };
return res.end(JSON.stringify(json));
});
server.listen(port, '0.0.0.0', err => {
if (err) {
throw err;
}
console.log(`> Ready on http://localhost:${port}`);
});
})();