Skip to content

Commit

Permalink
Daemonized Modcolle with PM2 (#7)
Browse files Browse the repository at this point in the history
* remove express session

Unused dependencies

* read environment variable without node foreman

* default port for app in dev (devPort=3000, appPort=5000)

* add pm2 configuration file

* configure docker to start modcolle with pm2

* fix #8 'request error' sent from OSAPI

Environment variables loaded in KEY=VALUE format is the cause of this
problem.
Fixed by loading environment variables in JSON format by using dot-env
library.

* automatically prepare environment variables file using gulp

.env.json will not be overwritten if the file already exists

* update README.md
  • Loading branch information
makemek authored Feb 28, 2017
1 parent 8352973 commit fb825f6
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 47 deletions.
7 changes: 4 additions & 3 deletions .env.template → .env.template.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"SESSION_SECRET": "DEFAULT",
"NODE_ENV":"development",

"LOGGER_SILENT": "false",
"LOGGER_LEVEL": "info",
"PORT": 5000,
"PORT_DEV": 3000,

"KANCOLLE_SERVER_MASTER": "http://203.104.209.7",
"KANCOLLE_SERVER_1" : "http://203.104.209.71",
"KANCOLLE_SERVER_2" : "http://203.104.209.87",
Expand All @@ -26,4 +27,4 @@
"KANCOLLE_SERVER_18" : "http://203.104.209.39",
"KANCOLLE_SERVER_19" : "http://203.104.209.55",
"KANCOLLE_SERVER_20" : "http://203.104.209.102"
}
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Project gitignore
public
src/config/settings.json
.env
.env.json
*.min.*

# Nodejs gitignore template
Expand Down Expand Up @@ -43,4 +43,4 @@ jspm_packages
.npm

# Optional REPL history
.node_repl_history
.node_repl_history
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ node_js:
- "5"

before_install:
- cp .env.template .env
- travis_retry npm install

script:
- npm run eslint
- npm run coverage

after_script:
- "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
- "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM node:6.9.1

MAINTAINER Apipol Niyomsak

RUN npm install -g foreman
RUN npm install -g pm2

RUN mkdir -p /var/www/modcolle

Expand All @@ -11,10 +11,12 @@ WORKDIR /var/www/modcolle

ADD . /var/www/modcolle/

RUN npm install
VOLUME ["/var/www/modcolle/log"]

RUN npm install && npm run build

# Expose port
EXPOSE 5000

# Run app
CMD npm start
CMD pm2-docker start process.json --auto-exit
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Coverage Status](https://coveralls.io/repos/github/makemek/Modcolle/badge.svg?branch=dev)](https://coveralls.io/github/makemek/Modcolle?branch=dev)

Modcolle is a proxy server for playing [Kantai Collection (艦隊これくしょん ~艦これ~)](http://www.dmm.com/netgame_s/kancolle/).
The main goal of Modcolle is to provide in-game translation displaying in the flash client directly.
The main goal of Modcolle is to provide in-game translation displaying in the flash client directly.
Unlike [Kancolle Viewer](https://github.com/Grabacr07/KanColleViewer) that wraps IE browser and listens to outgoing traffic, Modcolle, acting as a web server, makes Kancolle send all HTTP requests to Modcolle instead.
Thus, it has the opportunity to process requests before forwarding them to destinated Kancolle servers or response back to clients.
In this case, Modcolle is a middleman between client and Kancolle servers.
Expand Down Expand Up @@ -32,28 +32,31 @@ Please follows steps below to run Modcolle
### On Mac and Linux

1. Install [Docker](https://docs.docker.com/engine/installation/) and [Docker Compose](https://docs.docker.com/compose/install/)
2. Clone this repository `git clone git@github.com:makemek/Modcolle.git`
3. Copy `.env.template` to `.env` and configure
- SESSION_SECRET: a key for computing hash to generate a session. Can be any string
- LOGGER_SILENT: if `false` enables log, `true` disables log
- LOGGER_LEVEL: select a [logging level](https://github.com/winstonjs/winston#logging-levels)
4. Open terminal and run `docker-compose up`. You can add `-d` flag to detach the process and run as a daemon.
5. Open a web browser and type `localhost` in the url
2. Clone this repository `git clone git@github.com:makemek/Modcolle.git` and run `npm install`
3. Open terminal and run `docker-compose up`. You can add `-d` flag to detach the process and run as a daemon.
4. Open a web browser and type `localhost` in the url

### On windows
Unfortunately, Docker is not natively supported on Windows.
Also there is a shell script that be executed only in linux environment.
You have to install Nginx and Modcolle manually.

1. Install Nginx and Node.js (v. 6.9.1)
2. Clone this repository `git clone git@github.com:makemek/Modcolle.git`
3. Copy `.env.template` to `.env` and configure
- SESSION_SECRET: a key for computing hash to generate a session. Can be any string
- LOGGER_SILENT: if `false` enables log, `true` disables log
- LOGGER_LEVEL: select a [logging level](https://github.com/winstonjs/winston#logging-levels)
4. Copy `/deployment/nginx/nginx.conf` to `\path\to\nginx\conf`. Make sure to backup nginx.conf first before overwrite.
5. Inside nginx.conf replace `app1` and `DOMAIN_NAME` with `localhost`
6. Start nginx
7. `cd` to modcolle directory and run `npm install`
8. Type `npm start`
9. Open a browser and type `localhost` in the url
2. Clone this repository `git clone git@github.com:makemek/Modcolle.git` and run `npm install`
3. Copy `/deployment/nginx/nginx.conf` to `\path\to\nginx\conf`. Make sure to backup nginx.conf first before overwrite.
4. Inside nginx.conf replace `app1` and `DOMAIN_NAME` with `localhost`
5. Start nginx
6. Type `npm install pm2 -g` and run `pm2 start process.json`
7. Open a browser and type `localhost` in the url

## Configuration
`.env.json` is the common configuration for Modcolle which will be created on first-time start or test by copying `.env.json.template` (no overwrite if file already exists).
At `env` section in `process.json` are environment variables that will override `.env.json` for running in production environment.

### Environment Variables
- LOGGER_SILENT: if `false` enables log, `true` disables log
- LOGGER_LEVEL: select a [logging level](https://github.com/winstonjs/winston#logging-levels)
- PORT: application port
- PORT_DEV: **FOR DEVELOPMENT ONLY** will take effect when run `npm run dev`
- KANCOLLE_SERVER_MASTER: A host name for main Kancolle server that have interface`/kcsapi/api_world/get_id` for requesting players world id
- KANCOLLE_SERVER_#: A host name for other Kancolle servers where `#` is `world id`
1 change: 1 addition & 0 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

'use strict'

require('dot-env')
const winston = require('winston')
const app = require('../src/')
const http = require('http')
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ nginx:
- DOMAIN_NAME=localhost
app1:
build: .
volumes:
- /var/www/modcolle/log:/var/www/modcolle/log
ports:
- "5000"
14 changes: 11 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

const appPort = process.env.PORT
const devPort = process.env.PORT_DEV
require('dot-env')

const appPort = process.env.PORT || '5000'
const devPort = process.env.PORT_DEV || '3000'
const css = 'src/views/*.css'
const js = 'src/views/js/*.js'
const htmlTemplate = 'src/views/**/*.hbs'
Expand Down Expand Up @@ -49,7 +51,13 @@ gulp.task('build:js', () => {
.pipe(bs.stream())
})

gulp.task('import', ['font-awesome'])
gulp.task('import', ['environment-variables', 'font-awesome'])

gulp.task('environment-variables', () => {
gulp.src('.env.template.json')
.pipe(rename('.env.json'))
.pipe(gulp.dest('.', {overwrite: false}))
})

gulp.task('font-awesome', () => {
gulp.src('node_modules/font-awesome/css/font-awesome.min.css')
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
"build": "gulp build && gulp import",
"prestart": "npm run build",
"preinstall": "npm prune",
"start": "nf start",
"dev": "npm run build && nf run gulp browser-sync",
"coverage": "nf run istanbul cover ./node_modules/mocha/bin/_mocha",
"start": "node bin/www",
"dev": "npm run build && gulp browser-sync",
"coverage": "npm run pretest && istanbul cover ./node_modules/mocha/bin/_mocha",
"eslint": "eslint src/**/*.js test/**/*.js bin/**",
"utest": "nf run mocha",
"utest": "mocha",
"pretest": "npm run build",
"test": "npm run eslint && npm run utest"
},
"dependencies": {
"async": "2.1.2",
"body-parser": "1.15.2",
"dot-env": "0.0.1",
"express": "4.14.0",
"express-handlebars": "3.0.0",
"express-session": "1.14.2",
"fallbackjs": "1.1.8",
"font-awesome": "4.7.0",
"foreman": "2.0.0",
"inherit": "2.2.6",
"morgan": "1.7.0",
"passport": "0.3.2",
Expand Down
17 changes: 17 additions & 0 deletions process.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"apps": [{
"name": "Modcolle",
"script": "bin/www",
"watch": false,
"instances": 0,
"merge_logs": true,
"exec_mode": "cluster_mode",
"error_file": "./log/error.log",
"out_file": "./log/out.log",
"pid_file": "./log/modcolle.pid",
"env": {
"NODE_ENV": "production",
"PORT": 5000
}
}]
}
9 changes: 0 additions & 9 deletions src/modcolle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const express = require('express')
const bodyParser = require('body-parser')
const expressHandlebars = require('express-handlebars')
const passport = require('passport')
const session = require('express-session')
const LocalStrategy = require('passport-local').Strategy
const loginStrategy = require('./login-strategy')
const morgan = require('morgan')
Expand All @@ -13,8 +12,6 @@ const log = require('./logger')('app:router')
const router = require('./routing/')
const path = require('path')

const SESSION_SECRET = process.env.SESSION_SECRET

log.info('=== Welcome to Modcolle ===')
setupMiddleware()
setupTemplateEngine()
Expand All @@ -41,12 +38,6 @@ function setupMiddleware() {
log.verbose('setup POST body parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
log.verbose(`setup session using session secret ${SESSION_SECRET}`)
app.use(session({
secret: SESSION_SECRET,
resave: true,
saveUninitialized: false
}))
log.verbose('initialize passport')
app.use(passport.initialize())
passport.use('dmm-account', new LocalStrategy(loginStrategy.dmmAccount))
Expand Down
1 change: 1 addition & 0 deletions test/helper/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

require('dot-env')
require('../../src/')
require('./approot')
require('./sinon')

0 comments on commit fb825f6

Please sign in to comment.