Skip to content

Commit ba0e4df

Browse files
authored
Merge pull request #1228 from hackmdio/refactor-realtime
Refactor realtime
2 parents 7f60206 + 79af236 commit ba0e4df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+4415
-899
lines changed

.dockerignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.git/
2+
node_modules/
3+
docs/
4+
test/
5+
.sequelizerc.example
6+
config.json.example
7+
public/build
8+

.eslintignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.eslintrc.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ public/views/build
2727

2828
public/uploads/*
2929
!public/uploads/.gitkeep
30+
/.nyc_output
31+
/coverage/

.travis.yml

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,34 @@
11
language: node_js
2-
dist: trusty
2+
3+
node_js:
4+
- "lts/carbon"
5+
- "lts/dubnium"
6+
- "11"
7+
- "12"
8+
9+
dist: xenial
310
cache: yarn
4-
env:
5-
global:
6-
- CXX=g++-4.8
7-
- YARN_VERSION=1.3.2
11+
12+
matrix:
13+
fast_finish: true
14+
include:
15+
- node_js: lts/carbon
16+
- node_js: lts/dubnium
17+
allow_failures:
18+
- node_js: "11"
19+
- node_js: "12"
20+
21+
script:
22+
- yarn test:ci
23+
- yarn build
824

925
jobs:
1026
include:
11-
- env: task=npm-test
12-
node_js:
13-
- 8
14-
before_install:
15-
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version "$YARN_VERSION"
16-
- export PATH="$HOME/.yarn/bin:$PATH"
17-
- env: task=npm-test
18-
node_js:
19-
- 10
20-
before_install:
21-
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version "$YARN_VERSION"
22-
- export PATH="$HOME/.yarn/bin:$PATH"
23-
- env: task=ShellCheck
24-
script:
25-
- shellcheck bin/heroku bin/setup
26-
language: generic
27-
- env: task=doctoc
28-
install: npm install doctoc
27+
- stage: doctoc-check
28+
install: npm install -g doctoc
29+
if: type = pull_request OR branch = master
2930
script:
3031
- cp README.md README.md.orig
3132
- npm run doctoc
3233
- diff -q README.md README.md.orig
33-
language: generic
34-
- env: task=json-lint
35-
addons:
36-
apt:
37-
packages:
38-
- jq
39-
script:
40-
- npm run jsonlint
41-
language: generic
34+
node_js: lts/carbon

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CodiMD
22
===
33

4+
[![CodiMD on Gitter][gitter-image]][gitter-url]
45
[![build status][travis-image]][travis-url]
56
[![version][github-version-badge]][github-release-page]
67
[![Gitter][gitter-image]][gitter-url]

app.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var ejs = require('ejs')
77
var passport = require('passport')
88
var methodOverride = require('method-override')
99
var cookieParser = require('cookie-parser')
10-
var compression = require('compression')
1110
var session = require('express-session')
1211
var SequelizeStore = require('connect-session-sequelize')(session.Store)
1312
var fs = require('fs')
@@ -26,31 +25,34 @@ var response = require('./lib/response')
2625
var models = require('./lib/models')
2726
var csp = require('./lib/csp')
2827

29-
// server setup
30-
var app = express()
31-
var server = null
32-
if (config.useSSL) {
33-
var ca = (function () {
34-
var i, len, results
35-
results = []
36-
for (i = 0, len = config.sslCAPath.length; i < len; i++) {
37-
results.push(fs.readFileSync(config.sslCAPath[i], 'utf8'))
28+
function createHttpServer () {
29+
if (config.useSSL) {
30+
const ca = (function () {
31+
let i, len, results
32+
results = []
33+
for (i = 0, len = config.sslCAPath.length; i < len; i++) {
34+
results.push(fs.readFileSync(config.sslCAPath[i], 'utf8'))
35+
}
36+
return results
37+
})()
38+
const options = {
39+
key: fs.readFileSync(config.sslKeyPath, 'utf8'),
40+
cert: fs.readFileSync(config.sslCertPath, 'utf8'),
41+
ca: ca,
42+
dhparam: fs.readFileSync(config.dhParamPath, 'utf8'),
43+
requestCert: false,
44+
rejectUnauthorized: false
3845
}
39-
return results
40-
})()
41-
var options = {
42-
key: fs.readFileSync(config.sslKeyPath, 'utf8'),
43-
cert: fs.readFileSync(config.sslCertPath, 'utf8'),
44-
ca: ca,
45-
dhparam: fs.readFileSync(config.dhParamPath, 'utf8'),
46-
requestCert: false,
47-
rejectUnauthorized: false
46+
return require('https').createServer(options, app)
47+
} else {
48+
return require('http').createServer(app)
4849
}
49-
server = require('https').createServer(options, app)
50-
} else {
51-
server = require('http').createServer(app)
5250
}
5351

52+
// server setup
53+
var app = express()
54+
var server = createHttpServer()
55+
5456
// logger
5557
app.use(morgan('combined', {
5658
'stream': logger.stream
@@ -77,9 +79,6 @@ var sessionStore = new SequelizeStore({
7779
db: models.sequelize
7880
})
7981

80-
// compression
81-
app.use(compression())
82-
8382
// use hsts to tell https users stick to this
8483
if (config.hsts.enable) {
8584
app.use(helmet.hsts({
@@ -279,6 +278,7 @@ process.on('uncaughtException', function (err) {
279278
function handleTermSignals () {
280279
logger.info('CodiMD has been killed by signal, try to exit gracefully...')
281280
realtime.maintenance = true
281+
realtime.terminate()
282282
// disconnect all socket.io clients
283283
Object.keys(io.sockets.sockets).forEach(function (key) {
284284
var socket = io.sockets.sockets[key]
@@ -299,6 +299,9 @@ function handleTermSignals () {
299299
})
300300
}
301301
}, 100)
302+
setTimeout(() => {
303+
process.exit(1)
304+
}, 5000)
302305
}
303306
process.on('SIGINT', handleTermSignals)
304307
process.on('SIGTERM', handleTermSignals)

deployments/Dockerfile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
FROM node:8.15.1-jessie AS BUILD
2+
# use multi-stage build to build frontend javascript
3+
WORKDIR /codimd
4+
5+
COPY . ./
6+
7+
RUN yarn install --non-interactive --pure-lockfile && \
8+
yarn build
9+
10+
# ----------------------------------------------------
11+
# Runtime Stage
12+
FROM node:8.15.1 AS RUNTIME
13+
14+
# build for production
15+
ENV NODE_ENV production
16+
ENV PATH="/home/codimd/.npm-global/bin:${PATH}"
17+
18+
# setup isolated user for more security
19+
ARG USER_NAME=codimd
20+
ARG UID=1500
21+
ARG GID=1500
22+
23+
RUN set +x -ue && \
24+
wget https://github.com/hackmdio/portchecker/releases/download/v1.0.1/portchecker-linux-amd64.tar.gz && \
25+
tar xvf portchecker-linux-amd64.tar.gz -C /usr/local/bin && \
26+
mv /usr/local/bin/portchecker-linux-amd64 /usr/local/bin/pcheck && \
27+
# Add user and groupd
28+
groupadd --gid $GID $USER_NAME && \
29+
useradd --uid $UID --gid $USER_NAME --no-log-init --create-home $USER_NAME && \
30+
# setup local npm global directory
31+
mkdir /home/codimd/.npm-global && \
32+
echo "prefix=/home/codimd/.npm-global/" > /home/codimd/.npmrc && \
33+
# setup app dir
34+
mkdir /codimd && \
35+
# adjust permission
36+
chown -R $USER_NAME:$USER_NAME /home/codimd
37+
38+
# Copy build stage file to runtime
39+
COPY --from=BUILD /codimd /codimd
40+
RUN chown -R $USER_NAME:$USER_NAME /codimd
41+
42+
# change running user name
43+
USER $USER_NAME
44+
# build project
45+
WORKDIR /codimd
46+
47+
RUN set +x -ue && \
48+
cliVer=$(cat package.json | grep sequelize-cli | awk '{print substr($1, 2, length($1) - 3)"@"substr($2, 2, length($2) - 3)}') && \
49+
npm -g install "$cliVer" && \
50+
yarn install --production --non-interactive --pure-lockfile && \
51+
yarn cache clean
52+
53+
VOLUME /codimd/public/uploads
54+
EXPOSE 3000
55+
56+
ENTRYPOINT ["/codimd/docker-entrypoint.sh"]

deployments/dev-Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM node:8.15.1-jessie
2+
3+
WORKDIR /codimd
4+
5+
EXPOSE 3000
6+
7+
VOLUME ['/codimd/node_modules']

deployments/dev-docker-compose.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
version: '3'
2+
services:
3+
dev-database:
4+
image: postgres:11.2
5+
environment:
6+
POSTGRES_USER: codimd
7+
POSTGRES_PASSWORD: password
8+
POSTGRES_DB: codimd
9+
dev-codimd:
10+
build:
11+
dockerfile: ./deployments/dev-Dockerfile
12+
context: ../
13+
environment:
14+
CMD_DB_URL: postgres://codimd:password@dev-database/codimd
15+
volumes:
16+
- ../:/codimd
17+
- node_modules:/codimd/node_modules
18+
- public_build:/codimd/public/build
19+
- public_view_build:/codimd/public/views/build
20+
ports:
21+
- 3000:3000
22+
volumes:
23+
node_modules:
24+
public_build:
25+
public_view_build:

0 commit comments

Comments
 (0)