Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizeta committed Jan 14, 2016
1 parent 6f5280a commit f5f47f8
Show file tree
Hide file tree
Showing 12 changed files with 490 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compact": false,
"comments": false,
"presets": ["es2015"],
"plugins": [
"syntax-async-functions",
"syntax-object-rest-spread",
"transform-object-rest-spread",
"transform-regenerator",
"transform-runtime"
]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
cache
log
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src
node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# kcsp

[![NPM](https://nodei.co/npm/kcsp.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/kcsp/)

A simple proxy server for KanColle to avoid network problems.
39 changes: 39 additions & 0 deletions bin/kcsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/node

var getopt = require('node-getopt');
var env = require('../lib/env');

var cmd = getopt.create([
['c', 'cache-path=ARG', 'set cache database path'],
['l', 'log-path=ARG', 'set log file path'],
['p', 'path=ARG', 'set base path'],
['v', 'version', 'show version']
]).bindHelp().parseSystem();

if (cmd.options['version']) {
console.log(env.APP_VERSION);
process.exit();
}

require('daemon')();

var args = [];
if (cmd.options['path']) {
args.push('--path=' + cmd.options['path']);
}
if (cmd.options['cache-path']) {
args.push('--cache-path=' + cmd.options['cache-path']);
}
if (cmd.options['log-path']) {
args.push('--log-path=' + cmd.options['log-path']);
}

process.title = 'kcsp monitor';

var forever = require('forever-monitor');

var child = new (forever.Monitor)(__dirname + '/../lib/cli.js', {
args: args
});

child.start();
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "kcsp",
"version": "0.4.0",
"description": "A simple proxy server for KanColle to avoid network problems.",
"bin": {
"kcsp": "./bin/kcsp"
},
"scripts": {
"compile": "babel -d lib/ src/",
"prepublish": "npm run compile"
},
"author": "Gizeta",
"license": "GPL-2.0",
"files": [
"bin",
"lib"
],
"repository": {
"type": "git",
"url": "https://github.com/Gizeta/kcsp.git"
},
"keywords": [
"kancolle",
"proxy"
],
"bugs": {
"url": "https://github.com/Gizeta/kcsp/issues"
},
"homepage": "https://github.com/Gizeta/kcsp",
"dependencies": {
"babel-polyfill": "^6.3.14",
"daemon": "^1.1.0",
"forever-monitor": "^1.7.0",
"level": "^1.4.0",
"level-ttl": "^3.1.0",
"node-getopt": "^0.2.3",
"request": "^2.67.0",
"tracer": "^0.8.2"
},
"devDependencies": {
"babel-cli": "^6.4.0",
"babel-plugin-transform-object-rest-spread": "^6.3.13",
"babel-plugin-transform-regenerator": "^6.3.26",
"babel-preset-es2015": "^6.3.13"
}
}
7 changes: 7 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import server from './server';
import config from './config';
import logger from './logger';
import 'babel-polyfill';

server.listen(config.port);
logger.info('kcsp server started.');
20 changes: 20 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import getopt from 'node-getopt';

let cmd = getopt.create([
['b', 'path=ARG', 'set base path'],
['c', 'cache-path=ARG', 'set cache database path'],
['l', 'log-path=ARG', 'set log file path'],
['p', 'port=ARG', 'set web port']
]).bindHelp().parseSystem();

let basePath = cmd.options['path'] || __dirname + '/..';
let logPath = cmd.options['log-path'] || (basePath + '/log');
let cachePath = cmd.options['cache-path'] || (basePath + '/cache');
let port = cmd.options['cache-path'] ? parseInt(cmd.options['cache-path']) : 8099;

module.exports = {
basePath: basePath,
cachePath: cachePath,
logPath: logPath,
port: port
};
43 changes: 43 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import levelup from 'level';
import ttl from 'level-ttl';
import config from './config';
import logger from './logger';

let db = levelup(config.cachePath);
db = ttl(db, {
checkFrequency: 2 * 60 * 60 * 1000,
defaultTTL: 30 * 60 * 1000
});

export async function get(key) {
return new Promise((resolve, reject) => {
db.get(key, function(err, value) {
if (err) {
if (err.notFound) {
resolve();
return;
}
logger.error('database error, %s', err);
reject(err);
return;
}
resolve(value);
});
});
}

export function put(key, value) {
db.put(key, value, function(err) {
if (err) {
logger.error('database error, %s', err);
}
});
}

export function del(key) {
db.del(key, function(err) {
if (err) {
logger.error('database error, %s', err);
}
});
}
3 changes: 3 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
APP_VERSION: '0.4.0'
}
11 changes: 11 additions & 0 deletions src/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import tracer from 'tracer';
import fs from 'fs';
import config from './config';

(function(){
if (!fs.existsSync(config.logPath)) {
fs.mkdirSync(config.logPath);
}
})();

module.exports = tracer.dailyfile({root: config.logPath});
Loading

0 comments on commit f5f47f8

Please sign in to comment.