Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathserver.ts
More file actions
Latest commit
128 lines (112 loc) · 3.58 KB
/
Copy pathserver.ts
File metadata and controls
128 lines (112 loc) · 3.58 KB
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
importpassportfrom'passport';
importexpressfrom'express';
importsessionfrom'express-session';
importbodyParserfrom'body-parser';
importcookieParserfrom'cookie-parser';
importpuppeteerfrom'puppeteer';
importmorganfrom'morgan';
importuuidfrom'uuid/v4';
importpathfrom'path';
import{useStaticRendering}from'mobx-react';
import{AuthHandler,GithubHandler,GistHandler,ImageHandler}from'./handlers';
useStaticRendering(true);
process.on('SIGINT',()=>process.exit());
constdev=process.env.NODE_ENV!=='production';
constport=dev ? parseInt(process.env.BACKEND_PORT,10)||3030 : 3000;
constproxyContext=process.env.BACKEND_PROXY_CONTEXT||'/api';
constMemoryStore=require('memorystore')(session);
if(!dev){
constLOGS_ID=`${process.env.LOGS_SECRET_PREFIX}:${process.env.NOW_URL}`;
require('now-logs')(LOGS_ID);
}
constpuppeteerParams=dev
? {}
: {
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox','--disable-dev-shm-usage'],
};
constbrowser: {instance: puppeteer.Browser}={instance: null};
constpuppeteerLaunch=async()=>{
browser.instance=awaitpuppeteer.launch(puppeteerParams);
browser.instance.on('disconnected',puppeteerLaunch);
};
(async()=>{
awaitpuppeteerLaunch();
// set up
constserver=express();
constimageHandler=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(){
returnuuid();
},
store: newMemoryStore({
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);
returnnext(err);
}
});
req.logout();
res.redirect('/');
});
// production with next
if(!dev){
constnext=require('next');
constapp=next({ dev });
awaitapp.prepare();
consthandle=app.getRequestHandler();
constfilePath=path.join(__dirname,'.next','service-worker.js');
server.get('/service-worker.js',(req,res)=>app.serveStatic(req,res,filePath));
// to next.js
server.get('*',handleasany);
}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(typeofres.writeHead==='function'){
res.writeHead(500,{'content-type': 'application/json'});
}
}
constjson={error: 'api_error',reason: err.message};
returnres.end(JSON.stringify(json));
});
server.listen(port,'0.0.0.0',err=>{
if(err){
throwerr;
}
console.log(`> Ready on http://localhost:${port}`);
});
})();