forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·106 lines (80 loc) · 2.98 KB
/
index.js
File metadata and controls
executable file
·106 lines (80 loc) · 2.98 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
let path = require('path');
let fs = require('fs-extra');
let yaml = require('js-yaml');
let env = process.env;
// NODE_ENV = development || test || production
env.NODE_ENV = env.NODE_ENV || 'development';
let secret = require('./secret');
let lang = env.NODE_LANG || 'en';
require('util').inspect.defaultOptions.depth = 3;
if (env.DEV_TRACE) {
Error.stackTraceLimit = 100000;
require('trace');
require('clarify');
}
let config = module.exports = {
urlBase: {
// node may be behind nginx, use this in documents
main: new URL(env.URL_BASE_MAIN || env.URL_BASE || 'http://localhost:3000'),
static: new URL(env.URL_BASE_STATIC || env.URL_BASE || 'http://localhost:3000'),
},
urlBaseProduction: {
// when even in dev mode we must reference prod, use this (maybe remove it?)
main: new URL(env.URL_BASE_PRODUCTION_MAIN || env.URL_BASE || 'http://localhost:3000'),
static: new URL(env.URL_BASE_PRODUCTION_STATIC || env.URL_BASE || 'http://localhost:3000')
},
server: {
port: env.PORT || 3000,
host: env.HOST || 'localhost'
},
appKeys: [secret.sessionKey],
adminKey: secret.adminKey,
certDir: path.join(secret.dir, 'cert'),
lang: lang,
plnkrAuthId: secret.plnkrAuthId,
assetVersioning: env.ASSET_VERSIONING === 'file' ? 'file' :
env.ASSET_VERSIONING === 'query' ? 'query' : null,
pug: {
basedir: path.join(process.cwd(), 'templates'),
cache: env.NODE_ENV !== 'development'
},
supportEmail: 'iliakan@javascript.info',
projectRoot: process.cwd(),
// public files, served by nginx
publicRoot: path.join(process.cwd(), 'public', lang),
// private files, for expiring links, not directly accessible
tutorialRoot: env.TUTORIAL_ROOT || path.join(process.cwd(), 'repo', `${env.TUTORIAL_LANG || lang}.javascript.info`),
tmpRoot: path.join(process.cwd(), 'tmp', lang),
// js/css build versions
cacheRoot: path.join(process.cwd(), 'cache', lang),
assetsRoot: path.join(process.cwd(), 'assets'),
handlers: require('./handlers')
};
let repo = `javascript-tutorial/${config.lang}.javascript.info`;
config.tutorialRepo = {
github: repo,
url: new URL('https://github.com/' + repo),
tree: new URL('https://github.com/' + repo + '/tree/master'),
blob: new URL('https://github.com/' + repo + '/blob/master')
};
require.extensions['.yml'] = function(module, filename) {
module.exports = yaml.load(fs.readFileSync(filename, 'utf-8'));
};
// after module.exports for circle dep w/ config
const t = require('engine/i18n');
t.requireHandlerLocales();
// webpack config uses general config
// we have a loop dep here
config.webpack = require('./webpack');
createRoot(config.publicRoot);
createRoot(config.cacheRoot);
createRoot(config.tmpRoot);
function createRoot(root) {
// may be existing symlink
if (fs.existsSync(root) && fs.statSync(root).isFile()) {
fs.unlinkSync(root);
}
if (!fs.existsSync(root)) {
fs.ensureDirSync(root);
}
}