-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abandon global var and use import yapi method
- Loading branch information
sean
committed
Jul 4, 2017
1 parent
60b86c4
commit 42edf67
Showing
17 changed files
with
470 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"watch": ["lib/"] | ||
"watch": ["server_dist/"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,38 @@ | ||
import yapi from './yapi.js'; | ||
|
||
import path from 'path' | ||
import init from'./init.js' | ||
import fs from 'fs-extra' | ||
import commons from './utils/commons'; | ||
yapi.commons = commons; | ||
|
||
|
||
import dbModule from './utils/db.js'; | ||
|
||
import Koa from 'koa' | ||
import convert from 'koa-convert' | ||
import koaStatic from 'koa-static' | ||
import bodyParser from 'koa-bodyparser' | ||
import router from './router.js' | ||
|
||
|
||
|
||
const app = new Koa() | ||
app.use(bodyParser()) | ||
app.use(router.routes()) | ||
app.use(router.allowedMethods()) | ||
app.use(koaStatic( | ||
yapi.path.join(yapi.WEBROOT, 'static') | ||
)) | ||
app.listen(yapi.WEBCONFIG.port) | ||
commons.log(`the server is start at port ${yapi.WEBCONFIG.port}`) | ||
|
||
|
||
|
||
yapi.fs.ensureDirSync(yapi.WEBROOT_RUNTIME); | ||
yapi.fs.ensureDirSync(yapi.WEBROOT_LOG); | ||
dbModule.connect() | ||
|
||
import prdConfig from './config.json' | ||
import devConfig from './config.dev.json' | ||
|
||
let args = process.argv.splice(2); | ||
let isDev = args[0] === 'dev' ? true : false; | ||
const config = isDev ? devConfig : prdConfig; | ||
|
||
|
||
global.WEBROOT = path.resolve(__dirname, '..'); | ||
global.WEBROOT_SERVER = __dirname; | ||
global.WEBROOT_RUNTIME = path.join(WEBROOT, 'runtime'); | ||
global.WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log'); | ||
global.WEBCONFIG = config; | ||
|
||
init(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import groupModel from '../models/group.js' | ||
import yapi from '../yapi.js' | ||
|
||
module.exports = { | ||
async add(ctx) { | ||
let params = ctx.request.body; | ||
if(!params.group_name){ | ||
return ctx.body = yapi.commons.resReturn(null, 400, 'Group不能为空'); | ||
} | ||
var checkRepeat = await groupModel.checkRepeat(params.group_name); | ||
// if(checkRepeat > 0){ | ||
// return ctx.body = yapi.commons.resReturn(null, 401, 'group已存在'); | ||
// } | ||
let data = { | ||
group_name: params.group_name, | ||
group_desc: params.group_desc, | ||
uid: '0', | ||
add_time: yapi.commons.time(), | ||
up_time: yapi.commons.time() | ||
} | ||
try{ | ||
let result = await groupModel.save(data); | ||
result = yapi.commons.fieldSelect(result, ['_id', 'group_name', 'group_desc', 'uid']) | ||
ctx.body = yapi.commons.resReturn(result) | ||
}catch(e){ | ||
ctx.body = yapi.commons.resReturn(null, 402, e.message) | ||
} | ||
|
||
}, | ||
|
||
async list(ctx) { | ||
console.log(yapi) | ||
ctx.body = 1; | ||
}, | ||
|
||
async del(ctx){ | ||
|
||
}, | ||
|
||
async up(ctx){ | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import yapi from '../yapi.js' | ||
const groupSchema = ({ | ||
uid: String, | ||
group_name: String, | ||
group_desc: String, | ||
add_time: Number, | ||
up_time: Number | ||
}) | ||
|
||
|
||
var groupModel = yapi.db('group', groupSchema); | ||
|
||
|
||
function save(data){ | ||
let m = new groupModel(data); | ||
return m.save(); | ||
} | ||
|
||
function checkRepeat(name){ | ||
return groupModel.count({ | ||
group_name: name | ||
}) | ||
} | ||
|
||
function list(){ | ||
return groupModel.list().exec() | ||
} | ||
|
||
module.exports = { | ||
save: save, | ||
checkRepeat: checkRepeat, | ||
list: list | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import koaRouter from 'koa-router' | ||
import interfaceController from './controllers/interface' | ||
import koaRouter from 'koa-router' | ||
import interfaceController from './controllers/interface.js' | ||
import groupController from './controllers/group.js' | ||
|
||
let router = koaRouter(); | ||
const router = koaRouter(); | ||
|
||
const interface_PREFIX = { | ||
const INTERFACE_PREFIX = { | ||
interface: '/interface/', | ||
user: '/user/' | ||
user: '/user/', | ||
group: '/group/' | ||
}; | ||
|
||
router.get ( interface_PREFIX.interface + 'add', interfaceController.add) | ||
.get ( interface_PREFIX.interface + 'list', interfaceController.list) | ||
|
||
|
||
|
||
router.post ( INTERFACE_PREFIX.interface + 'add', interfaceController.add) | ||
.get ( INTERFACE_PREFIX.interface + 'list', interfaceController.list) | ||
.get ( INTERFACE_PREFIX.group + 'list', groupController.list) | ||
.post ( INTERFACE_PREFIX.group + 'add', groupController.add) | ||
.post ( INTERFACE_PREFIX.group + 'up', groupController.up) | ||
.post ( INTERFACE_PREFIX.group + 'del', groupController.del) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,42 @@ | ||
import path from 'path' | ||
import mongoose from 'mongoose' | ||
import {fileExist, log} from './commons.js' | ||
|
||
import yapi from '../yapi.js' | ||
|
||
function model(model, schema){ | ||
return mongoose.model(model, schema, model) | ||
} | ||
|
||
function init(){ | ||
function connect(){ | ||
mongoose.Promise = global.Promise; | ||
let config = WEBCONFIG; | ||
let config = yapi.WEBCONFIG; | ||
|
||
let db = mongoose.connect(`mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`); | ||
|
||
db.then(function (res) { | ||
log('mongodb load success...') | ||
yapi.commons.log('mongodb load success...') | ||
}, function (err) { | ||
log(err, 'Mongo connect error'); | ||
yapi.commons.log(err, 'Mongo connect error'); | ||
}) | ||
|
||
|
||
checkDatabase(); | ||
return db; | ||
} | ||
|
||
function checkDatabase(){ | ||
let exist = fileExist(path.join(WEBROOT_RUNTIME, 'init.lock')) | ||
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock')) | ||
if(!exist){ | ||
log('lock is not exist') | ||
yapi.commons.log('lock is not exist') | ||
} | ||
} | ||
|
||
export default init; | ||
yapi.db = model; | ||
|
||
|
||
module.exports = { | ||
model: model, | ||
connect: connect | ||
}; | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import prdConfig from './config.json' | ||
import devConfig from './config.dev.json' | ||
let args = process.argv.splice(2); | ||
let isDev = args[0] === 'dev' ? true : false; | ||
const config = isDev ? devConfig : prdConfig; | ||
|
||
const WEBROOT = path.resolve(__dirname, '..'); | ||
const WEBROOT_SERVER = __dirname; | ||
const WEBROOT_RUNTIME = path.join(WEBROOT, 'runtime'); | ||
const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log'); | ||
const WEBCONFIG = config; | ||
|
||
module.exports = { | ||
fs: fs, | ||
path: path, | ||
WEBROOT: WEBROOT, | ||
WEBROOT_SERVER: WEBROOT_SERVER, | ||
WEBROOT_RUNTIME: WEBROOT_RUNTIME, | ||
WEBROOT_LOG: WEBROOT_LOG, | ||
WEBCONFIG: WEBCONFIG, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,49 @@ | ||
'use strict'; | ||
|
||
var _path = require('path'); | ||
var _yapi = require('./yapi.js'); | ||
|
||
var _path2 = _interopRequireDefault(_path); | ||
var _yapi2 = _interopRequireDefault(_yapi); | ||
|
||
var _init = require('./init.js'); | ||
var _commons = require('./utils/commons'); | ||
|
||
var _init2 = _interopRequireDefault(_init); | ||
var _commons2 = _interopRequireDefault(_commons); | ||
|
||
var _fsExtra = require('fs-extra'); | ||
var _db = require('./utils/db.js'); | ||
|
||
var _fsExtra2 = _interopRequireDefault(_fsExtra); | ||
var _db2 = _interopRequireDefault(_db); | ||
|
||
var _config = require('./config.json'); | ||
var _koa = require('koa'); | ||
|
||
var _config2 = _interopRequireDefault(_config); | ||
var _koa2 = _interopRequireDefault(_koa); | ||
|
||
var _configDev = require('./config.dev.json'); | ||
var _koaConvert = require('koa-convert'); | ||
|
||
var _configDev2 = _interopRequireDefault(_configDev); | ||
var _koaConvert2 = _interopRequireDefault(_koaConvert); | ||
|
||
var _koaStatic = require('koa-static'); | ||
|
||
var _koaStatic2 = _interopRequireDefault(_koaStatic); | ||
|
||
var _koaBodyparser = require('koa-bodyparser'); | ||
|
||
var _koaBodyparser2 = _interopRequireDefault(_koaBodyparser); | ||
|
||
var _router = require('./router.js'); | ||
|
||
var _router2 = _interopRequireDefault(_router); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var args = process.argv.splice(2); | ||
var isDev = args[0] === 'dev' ? true : false; | ||
var config = isDev ? _configDev2.default : _config2.default; | ||
_yapi2.default.commons = _commons2.default; | ||
|
||
global.WEBROOT = _path2.default.resolve(__dirname, '..'); | ||
global.WEBROOT_SERVER = __dirname; | ||
global.WEBROOT_RUNTIME = _path2.default.join(WEBROOT, 'runtime'); | ||
global.WEBROOT_LOG = _path2.default.join(WEBROOT_RUNTIME, 'log'); | ||
global.WEBCONFIG = config; | ||
var app = new _koa2.default(); | ||
app.use((0, _koaBodyparser2.default)()); | ||
app.use(_router2.default.routes()); | ||
app.use(_router2.default.allowedMethods()); | ||
app.use((0, _koaStatic2.default)(_yapi2.default.path.join(_yapi2.default.WEBROOT, 'static'))); | ||
app.listen(_yapi2.default.WEBCONFIG.port); | ||
_commons2.default.log('the server is start at port ' + _yapi2.default.WEBCONFIG.port); | ||
|
||
(0, _init2.default)(); | ||
_yapi2.default.fs.ensureDirSync(_yapi2.default.WEBROOT_RUNTIME); | ||
_yapi2.default.fs.ensureDirSync(_yapi2.default.WEBROOT_LOG); | ||
_db2.default.connect(); |
Oops, something went wrong.