diff --git a/.env.template b/.env.template.json similarity index 96% rename from .env.template rename to .env.template.json index 42b8226..0103fbd 100644 --- a/.env.template +++ b/.env.template.json @@ -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", @@ -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" -} \ No newline at end of file +} diff --git a/.gitignore b/.gitignore index e91ed01..f9430ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Project gitignore public src/config/settings.json -.env +.env.json *.min.* # Nodejs gitignore template @@ -43,4 +43,4 @@ jspm_packages .npm # Optional REPL history -.node_repl_history \ No newline at end of file +.node_repl_history diff --git a/.travis.yml b/.travis.yml index 0d09c21..17d9f42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ node_js: - "5" before_install: - - cp .env.template .env - travis_retry npm install script: @@ -12,4 +11,4 @@ script: - npm run coverage after_script: - - "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" \ No newline at end of file + - "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js" diff --git a/Dockerfile b/Dockerfile index 7489800..0305cbe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 @@ -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 diff --git a/README.md b/README.md index 54e733c..27e727e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -32,13 +32,9 @@ 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. @@ -46,14 +42,21 @@ 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` diff --git a/bin/www b/bin/www index 6fedec2..4e4aa8a 100644 --- a/bin/www +++ b/bin/www @@ -2,6 +2,7 @@ 'use strict' +require('dot-env') const winston = require('winston') const app = require('../src/') const http = require('http') diff --git a/docker-compose.yml b/docker-compose.yml index 8e04f94..6e0fb67 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,5 +12,7 @@ nginx: - DOMAIN_NAME=localhost app1: build: . + volumes: + - /var/www/modcolle/log:/var/www/modcolle/log ports: - "5000" diff --git a/gulpfile.js b/gulpfile.js index 9725616..844a68f 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' @@ -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') diff --git a/package.json b/package.json index c322b88..a1d3247 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/process.json b/process.json new file mode 100644 index 0000000..20206b2 --- /dev/null +++ b/process.json @@ -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 + } + }] +} diff --git a/src/modcolle.js b/src/modcolle.js index 4c2b78c..c28ecf2 100644 --- a/src/modcolle.js +++ b/src/modcolle.js @@ -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') @@ -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() @@ -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)) diff --git a/test/helper/index.js b/test/helper/index.js index c6bc23f..47cef4b 100644 --- a/test/helper/index.js +++ b/test/helper/index.js @@ -1,5 +1,6 @@ 'use strict' +require('dot-env') require('../../src/') require('./approot') require('./sinon')