Skip to content

Commit

Permalink
hello world!
Browse files Browse the repository at this point in the history
Hi,从见天起,中国蚁剑正式开源和大家见面啦!
  • Loading branch information
antoor committed Mar 13, 2016
1 parent aa83a57 commit 7905e4a
Show file tree
Hide file tree
Showing 1,170 changed files with 97,990 additions and 0 deletions.
53 changes: 53 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

// 导入模块
const Menubar = require('./modules/menubar');
const Request = require('./modules/request');
const Database = require('./modules/database');
const Cache = require('./modules/cache');

// electron.crashReporter.start();

app
.on('window-all-closed', app.quit)
.on('ready', () => {
let mainWindow = new BrowserWindow({
width: 1040,
height: 699,
minWidth: 1040,
minHeight: 699,
webgl: false,
title: 'AntSword',
// autoHideMenuBar: true,
// transparent: false,
// frame: false
// resizable: false
});
mainWindow.loadURL(`file:\/\/${__dirname}/views/index.html`);

mainWindow
.on('closed', () => { mainWindow = null })
.on('maximize', mainWindow.webContents.reload)
.on('unmaximize', mainWindow.webContents.reload)
.on('enter-full-screen', mainWindow.webContents.reload)
.on('leave-full-screen', mainWindow.webContents.reload)

// 打开调试控制台
// mainWindow.webContents.openDevTools();

new Menubar(electron, app, mainWindow);

// 监听Request请求
new Request(electron);

// 初始化数据库
new Database(electron);

// 初始化缓存模块
new Cache(electron);

});
124 changes: 124 additions & 0 deletions modules/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// 缓存管理模块
//

'use strict';

const fs = require('fs');
const path = require('path');
const log4js = require('log4js');
const Datastore = require('nedb');

const logger = log4js.getLogger('Cache');

class Cache {

constructor(electron) {
// 创建数据库
// 获取用户保存目录(mac&&*unix=/home/path/,win=c:/path/appdata
let dbPath = '';
if (process.env.HOME) {
dbPath = path.join(process.env.HOME, '.antSword');
}else if (process.env.LOCALAPPPATH) {
dbPath = path.join(process.env.LOCALAPPPATH, '.antSword');
}else{
dbPath = 'database';
};
// 创建数据目录
if (!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath);
};
// 创建缓存目录
const cachePath = path.join(dbPath, 'cache');
if (!fs.existsSync(cachePath)) {
fs.mkdirSync(cachePath);
};
this.dbPath = dbPath;
this.cachePath = cachePath;
// 监听数据请求
this.listenHandle(electron.ipcMain);
}

listenHandle(ipcMain) {
logger.info('listenHandle');
ipcMain
// 添加缓存
// arg={id="shellID",tag="存储标识",cache="存储内容"}
.on('cache-add', (event, arg) => {
logger.debug('cache-add', arg);
this.createDB(arg['id']).insert({
tag: arg['tag'],
cache: arg['cache']
}, (err, ret) => {
event.returnValue = err || ret;
});
})
// 更新缓存
// arg = {id, tag, cache}
.on('cache-set', (event, arg) => {
logger.debug('cache-set', arg);
this.createDB(arg['id']).update({
tag: arg['tag']
}, {
$set: {
cache: arg['cache']
}
}, (err, ret) => {
event.returnValue = err || ret;
});
})
// 查询缓存
// arg={id="shellID", tag="存储标识"}
.on('cache-get', (event, arg) => {
logger.debug('cache-get', arg);
this.createDB(arg['id']).findOne({
tag: arg['tag']
}, (err, ret) => {
event.returnValue = err || ret;
})
})
// 删除缓存
// arg = {id: 'SHELL-ID', tag: 'SAVE-TAG'}
.on('cache-del', (event, arg) => {
logger.warn('cache-del', arg);
this.createDB(arg['id']).remove({
tag: arg['tag']
}, (err, ret) => {
event.returnValue = err || ret;
})
})
// 清空缓存
// arg = {id: 'SHELL-ID'}
.on('cache-clear', (event, arg) => {
logger.fatal('cache-clear', arg);
try{
fs.unlinkSync(path.join(this.cachePath, arg['id']));
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
})
// 清空所有缓存
.on('cache-clearAll', (event, arg) => {
logger.fatal('cache-clearAll', arg);
try{
fs.readdirSync(this.cachePath).map((_) => {
fs.unlinkSync(path.join(this.cachePath, _));
});
event.returnValue = true;
}catch(e) {
event.returnValue = e;
}
})
}

createDB(id) {
// 创建数据库
return new Datastore({
filename: path.join(this.cachePath, id),
autoload: true
});
}

}
module.exports = Cache;
Loading

0 comments on commit 7905e4a

Please sign in to comment.