Skip to content

Commit 32af96a

Browse files
authored
Merge pull request #940 from WilliButz/fix-configurable-paths
enhance configurabiltiy of paths & make execution path-independent
2 parents f186f73 + 61e2401 commit 32af96a

File tree

8 files changed

+39
-45
lines changed

8 files changed

+39
-45
lines changed

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ There are some config settings you need to change in the files below.
174174
| --------- | ------ | ----------- |
175175
| `NODE_ENV` | `production` or `development` | set current environment (will apply corresponding settings in the `config.json`) |
176176
| `DEBUG` | `true` or `false` | set debug mode; show more logs |
177+
| `CMD_CONFIG_FILE` | `/path/to/config.json` | optional override for the path to CodiMD's config file |
177178
| `CMD_DOMAIN` | `codimd.org` | domain name |
178179
| `CMD_URL_PATH` | `codimd` | sub URL path, like `www.example.com/<URL_PATH>` |
179180
| `CMD_HOST` | `localhost` | host to listen on |
@@ -285,19 +286,15 @@ There are some config settings you need to change in the files below.
285286
| `defaultPermission` | `freely`, `editable`, `limited`, `locked`, `protected` or `private` | set notes default permission (only applied on signed users) |
286287
| `dbURL` | `mysql://localhost:3306/database` | set the db URL; if set, then db config (below) won't be applied |
287288
| `db` | `{ "dialect": "sqlite", "storage": "./db.codimd.sqlite" }` | set the db configs, [see more here](http://sequelize.readthedocs.org/en/latest/api/sequelize/) |
288-
| `sslKeyPath` | `./cert/client.key` | SSL key path (only need when you set `useSSL`) |
289-
| `sslCertPath` | `./cert/codimd_io.crt` | SSL cert path (only need when you set `useSSL`) |
290-
| `sslCAPath` | `['./cert/COMODORSAAddTrustCA.crt']` | SSL ca chain (only need when you set `useSSL`) |
291-
| `dhParamPath` | `./cert/dhparam.pem` | SSL dhparam path (only need when you set `useSSL`) |
292-
| `tmpPath` | `./tmp/` | temp directory path |
293-
| `defaultNotePath` | `./public/default.md` | default note file path |
294-
| `docsPath` | `./public/docs` | docs directory path |
295-
| `indexPath` | `./public/views/index.ejs` | index template file path |
296-
| `hackmdPath` | `./public/views/hackmd.ejs` | hackmd template file path |
297-
| `errorPath` | `./public/views/error.ejs` | error template file path |
298-
| `prettyPath` | `./public/views/pretty.ejs` | pretty template file path |
299-
| `slidePath` | `./public/views/slide.hbs` | slide template file path |
300-
| `uploadsPath` | `./public/uploads` | uploads directory - needs to be persistent when you use imageUploadType `filesystem` |
289+
| `sslKeyPath` | `./cert/client.key` | SSL key path<sup>1</sup> (only need when you set `useSSL`) |
290+
| `sslCertPath` | `./cert/codimd_io.crt` | SSL cert path<sup>1</sup> (only need when you set `useSSL`) |
291+
| `sslCAPath` | `['./cert/COMODORSAAddTrustCA.crt']` | SSL ca chain<sup>1</sup> (only need when you set `useSSL`) |
292+
| `dhParamPath` | `./cert/dhparam.pem` | SSL dhparam path<sup>1</sup> (only need when you set `useSSL`) |
293+
| `tmpPath` | `./tmp/` | temp directory path<sup>1</sup> |
294+
| `defaultNotePath` | `./public/default.md` | default note file path<sup>1</sup> |
295+
| `docsPath` | `./public/docs` | docs directory path<sup>1</sup> |
296+
| `viewPath` | `./public/views` | template directory path<sup>1</sup> |
297+
| `uploadsPath` | `./public/uploads` | uploads directory<sup>1</sup> - needs to be persistent when you use imageUploadType `filesystem` |
301298
| `sessionName` | `connect.sid` | cookie session name |
302299
| `sessionSecret` | `secret` | cookie session secret |
303300
| `sessionLife` | `14 * 24 * 60 * 60 * 1000` | cookie session life |
@@ -314,6 +311,8 @@ There are some config settings you need to change in the files below.
314311
| `s3` | `{ "accessKeyId": "YOUR_S3_ACCESS_KEY_ID", "secretAccessKey": "YOUR_S3_ACCESS_KEY", "region": "YOUR_S3_REGION" }` | When `imageuploadtype` be set to `s3`, you would also need to setup this key, check our [S3 Image Upload Guide](docs/guides/s3-image-upload.md) |
315312
| `s3bucket` | `YOUR_S3_BUCKET_NAME` | bucket name when `imageUploadType` is set to `s3` or `minio` |
316313

314+
<sup>1</sup>: relative paths are based on CodiMD's base directory
315+
317316
## Third-party integration API key settings
318317

319318
| service | settings location | description |

app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ app.use(i18n.init)
126126
// routes without sessions
127127
// static files
128128
app.use('/', express.static(path.join(__dirname, '/public'), { maxAge: config.staticCacheTime }))
129+
app.use('/docs', express.static(path.resolve(__dirname, config.docsPath), { maxAge: config.staticCacheTime }))
130+
app.use('/uploads', express.static(path.resolve(__dirname, config.uploadsPath), { maxAge: config.staticCacheTime }))
131+
app.use('/default.md', express.static(path.resolve(__dirname, config.defaultNotePath), { maxAge: config.staticCacheTime }))
129132

130133
// session
131134
app.use(session({
@@ -167,7 +170,7 @@ app.use(require('./lib/web/middleware/codiMDVersion'))
167170

168171
// routes need sessions
169172
// template files
170-
app.set('views', path.join(__dirname, '/public/views'))
173+
app.set('views', config.viewPath)
171174
// set render engine
172175
app.engine('ejs', ejs.renderFile)
173176
// set view engine

lib/config/default.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,10 @@ module.exports = {
3838
sslCAPath: '',
3939
dhParamPath: '',
4040
// other path
41+
viewPath: './public/views',
4142
tmpPath: './tmp',
4243
defaultNotePath: './public/default.md',
4344
docsPath: './public/docs',
44-
indexPath: './public/views/index.ejs',
45-
codimdPath: './public/views/codimd.ejs',
46-
errorPath: './public/views/error.ejs',
47-
prettyPath: './public/views/pretty.ejs',
48-
slidePath: './public/views/slide.ejs',
49-
constantsPath: './public/js/lib/common/constant.ejs',
5045
uploadsPath: './public/uploads',
5146
// session
5247
sessionName: 'connect.sid',

lib/config/index.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const deepFreeze = require('deep-freeze')
99
const {Environment, Permission} = require('./enum')
1010
const logger = require('../logger')
1111

12-
const appRootPath = path.join(__dirname, '../../')
12+
const appRootPath = path.resolve(__dirname, '../../')
1313
const env = process.env.NODE_ENV || Environment.development
1414
const debugConfig = {
1515
debug: (env === Environment.development)
@@ -23,7 +23,8 @@ const packageConfig = {
2323
minimumCompatibleVersion: '0.5.0'
2424
}
2525

26-
const configFilePath = path.join(appRootPath, 'config.json')
26+
const configFilePath = path.resolve(appRootPath, process.env.CMD_CONFIG_FILE ||
27+
'config.json')
2728
const fileConfig = fs.existsSync(configFilePath) ? require(configFilePath)[env] : undefined
2829

2930
let config = require('./default')
@@ -173,20 +174,14 @@ config.sslCAPath.forEach(function (capath, i, array) {
173174
array[i] = path.resolve(appRootPath, capath)
174175
})
175176

176-
config.sslCertPath = path.join(appRootPath, config.sslCertPath)
177-
config.sslKeyPath = path.join(appRootPath, config.sslKeyPath)
178-
config.dhParamPath = path.join(appRootPath, config.dhParamPath)
179-
180-
config.tmpPath = path.join(appRootPath, config.tmpPath)
181-
config.defaultNotePath = path.join(appRootPath, config.defaultNotePath)
182-
config.docsPath = path.join(appRootPath, config.docsPath)
183-
config.indexPath = path.join(appRootPath, config.indexPath)
184-
config.codimdPath = path.join(appRootPath, config.codimdPath)
185-
config.errorPath = path.join(appRootPath, config.errorPath)
186-
config.prettyPath = path.join(appRootPath, config.prettyPath)
187-
config.slidePath = path.join(appRootPath, config.slidePath)
188-
config.constantsPath = path.join(appRootPath, config.constantsPath)
189-
config.uploadsPath = path.join(appRootPath, config.uploadsPath)
177+
config.sslCertPath = path.resolve(appRootPath, config.sslCertPath)
178+
config.sslKeyPath = path.resolve(appRootPath, config.sslKeyPath)
179+
config.dhParamPath = path.resolve(appRootPath, config.dhParamPath)
180+
config.viewPath = path.resolve(appRootPath, config.viewPath)
181+
config.tmpPath = path.resolve(appRootPath, config.tmpPath)
182+
config.defaultNotePath = path.resolve(appRootPath, config.defaultNotePath)
183+
config.docsPath = path.resolve(appRootPath, config.docsPath)
184+
config.uploadsPath = path.resolve(appRootPath, config.uploadsPath)
190185

191186
// make config readonly
192187
config = deepFreeze(config)

lib/models/revision.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var async = require('async')
55
var moment = require('moment')
66
var childProcess = require('child_process')
77
var shortId = require('shortid')
8+
var path = require('path')
89

910
// core
1011
var config = require('../config')
@@ -14,7 +15,7 @@ var dmpWorker = createDmpWorker()
1415
var dmpCallbackCache = {}
1516

1617
function createDmpWorker () {
17-
var worker = childProcess.fork('./lib/workers/dmpWorker.js', {
18+
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), {
1819
stdio: 'ignore'
1920
})
2021
if (config.debug) logger.info('dmp worker process started')

lib/response.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var response = {
5454
}
5555

5656
function responseError (res, code, detail, msg) {
57-
res.status(code).render(config.errorPath, {
57+
res.status(code).render('error.ejs', {
5858
url: config.serverURL,
5959
title: code + ' ' + detail + ' ' + msg,
6060
code: code,
@@ -104,11 +104,11 @@ function showIndex (req, res, next) {
104104
}).then(function (user) {
105105
if (user) {
106106
data.deleteToken = user.deleteToken
107-
res.render(config.indexPath, data)
107+
res.render('index.ejs', data)
108108
}
109109
})
110110
} else {
111-
res.render(config.indexPath, data)
111+
res.render('index.ejs', data)
112112
}
113113
}
114114

@@ -122,7 +122,7 @@ function responseCodiMD (res, note) {
122122
'Cache-Control': 'private', // only cache by client
123123
'X-Robots-Tag': 'noindex, nofollow' // prevent crawling
124124
})
125-
res.render(config.codimdPath, {
125+
res.render('codimd.ejs', {
126126
url: config.serverURL,
127127
title: title,
128128
useCDN: config.useCDN,
@@ -283,7 +283,7 @@ function renderPublish (data, res) {
283283
res.set({
284284
'Cache-Control': 'private' // only cache by client
285285
})
286-
res.render(config.prettyPath, data)
286+
res.render('pretty.ejs', data)
287287
}
288288

289289
function actionPublish (req, res, note) {
@@ -665,7 +665,7 @@ function renderPublishSlide (data, res) {
665665
res.set({
666666
'Cache-Control': 'private' // only cache by client
667667
})
668-
res.render(config.slidePath, data)
668+
res.render('slide.ejs', data)
669669
}
670670

671671
module.exports = response

lib/web/imageRouter/filesystem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22
const url = require('url')
3+
const path = require('path')
34

45
const config = require('../../config')
56
const logger = require('../../logger')
@@ -15,5 +16,5 @@ exports.uploadImage = function (imagePath, callback) {
1516
return
1617
}
1718

18-
callback(null, url.resolve(config.serverURL + '/', imagePath.match(/public\/(.+)$/)[1]))
19+
callback(null, url.resolve(config.serverURL + '/uploads/', path.basename(imagePath)))
1920
}

lib/web/statusRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ statusRouter.get('/config', function (req, res) {
105105
'X-Robots-Tag': 'noindex, nofollow', // prevent crawling
106106
'Content-Type': 'application/javascript'
107107
})
108-
res.render(config.constantsPath, data)
108+
res.render('../js/lib/common/constant.ejs', data)
109109
})

0 commit comments

Comments
 (0)