-
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.
- Loading branch information
0 parents
commit d421083
Showing
24 changed files
with
343 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/node_modules/ | ||
/.idea/ | ||
/template/node_modules/ |
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,38 @@ | ||
module.exports = { | ||
prompts: { | ||
name: { | ||
type: 'string', | ||
required: true, | ||
message: 'project name' | ||
}, | ||
description: { | ||
type: 'string', | ||
message: 'description', | ||
default: 'a node website backend' | ||
}, | ||
author: { | ||
type: 'string', | ||
message: 'author', | ||
required: true | ||
}, | ||
pm2: { | ||
type: "confirm", | ||
message: "create pm2 deploy config file?" | ||
}, | ||
tair: { | ||
type: "confirm", | ||
message: "need tair?" | ||
}, | ||
mysql: { | ||
type: 'confirm', | ||
message: 'need mysql database?' | ||
} | ||
}, | ||
filters:{ | ||
'deploy/pm2/*':'pm2', | ||
'lib/tair.js':'tair', | ||
'service/db/*':'mysql', | ||
'service/use_db_demo/*':'mysql', | ||
}, | ||
completeMessage: "init project successfully. \n read more in readme.md" | ||
}; |
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,6 @@ | ||
logs/* | ||
*.log | ||
/node_modules/ | ||
node_modules | ||
/.idea/ | ||
.DS_Store |
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 @@ | ||
registry = http://r.npm.sankuai.com |
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 @@ | ||
'use strict'; | ||
var koa = require('koa'); | ||
var compress = require('koa-compress'); | ||
var logger = require('koa-logger'); | ||
var serve = require('koa-static'); | ||
var path = require('path'); | ||
var koaJson = require('koa-json'); | ||
var bodyParser = require('koa-bodyparser'); | ||
var controller = require('./router'); | ||
var env = require('./lib/config').getEnv(); | ||
|
||
var app = koa(); | ||
|
||
app.use(bodyParser()); | ||
app.use(koaJson()); | ||
app.use(serve(path.join(__dirname, 'public'))); | ||
app.use(compress()); | ||
app.use(logger()); | ||
controller.register(app); | ||
|
||
try { | ||
if (env === "development") { | ||
app.listen(9000,()=>{ | ||
console.log('server listening on port 9000'); | ||
}); | ||
} else { | ||
app.listen(8080,()=>{ | ||
console.log('server listening on port 8080'); | ||
}); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} |
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,41 @@ | ||
'use strict'; | ||
module.exports = { | ||
//common config | ||
//... | ||
|
||
//config for different environment | ||
development:{ | ||
{{#mysql}} | ||
db: { | ||
username: '', | ||
database: '', | ||
host: '', | ||
port: 0, | ||
dialect: 'mysql', | ||
pool: { | ||
max: 100, | ||
min: 0, | ||
idle: 10000 | ||
} | ||
}, | ||
{{/mysql}} | ||
{{#tair}} | ||
tair: { | ||
server: [ | ||
{ | ||
'host': '', | ||
'port': 0 | ||
}, | ||
{ | ||
'host': '', | ||
'port': 0 | ||
} | ||
], | ||
namespace: 0, | ||
group: '' | ||
} | ||
{{/tair}} | ||
}, | ||
staging:{}, | ||
production:{} | ||
}; |
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,17 @@ | ||
{ | ||
"apps": [ | ||
{ | ||
"name": "{{ name }}", | ||
"script": "app.js", | ||
"args": [], | ||
"log_date_format": "YYYY-MM-DD HH:mm Z", | ||
"error_file": "{{ name }}-err.log", | ||
"out_file": "{{ name }}-out.log", | ||
"node_args": "--harmony", | ||
"cwd": ".", | ||
"env": { | ||
"NODE_ENV": "dev" | ||
} | ||
} | ||
] | ||
} |
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,18 @@ | ||
'use strict'; | ||
var config = require('../config.js'); | ||
var env = process.env.NODE_ENV || 'development'; | ||
module.exports = { | ||
{{#mysql}} | ||
getDbConfig: function(){ | ||
return config[env].db; | ||
}, | ||
{{/mysql}} | ||
{{#tair}} | ||
getTairConfig: function () { | ||
return config[env].tair; | ||
}, | ||
{{/tair}} | ||
getEnv: function () { | ||
return env; | ||
} | ||
}; |
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 @@ | ||
'use strict'; |
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,7 @@ | ||
'use strict'; | ||
let views = require('co-views'); | ||
let env = process.env.NODE_ENV || 'development'; | ||
|
||
module.exports = views(__dirname + '/../views', { | ||
map: { html: 'swig' }, cache : env !== 'production' ? false : 'memory' | ||
}); |
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,54 @@ | ||
'use strict'; | ||
var tair_client = require('@mtfe/node-tair'); | ||
var config = require('./config'); | ||
var tairConfig = config.getTairConfig(); | ||
var namespace = tairConfig.namespace; | ||
var expire = 0; | ||
var tair = new tair_client(tairConfig.group, tairConfig.server, function (err) { | ||
if (err) { | ||
console.log('创建tair客户端失败:', err); | ||
} | ||
}); | ||
|
||
// 建议在这里统一对缓存key添加前缀, 避免冲突 | ||
// eg: var prefix = 'web_staging_'; | ||
// 然后在下面每个方法中,设置 key = prefix + key; | ||
|
||
module.exports = { | ||
getCache: function (key) { | ||
return new Promise(function (resolve, reject) { | ||
tair.get(key, namespace, function (err, data) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
data = JSON.parse(data); | ||
resolve(data); | ||
} | ||
}); | ||
}); | ||
}, | ||
setCache: function (key, value, options) { | ||
value = JSON.stringify(value); | ||
options = options || {}; | ||
return new Promise(function (resolve, reject) { | ||
tair.set(key, value, options.expire || expire, namespace, function (err, isSuccess) { | ||
if (isSuccess) { | ||
resolve(isSuccess); | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
}, | ||
removeCache: function (key) { | ||
return new Promise(function (resolve, reject) { | ||
tair.remove(key, namespace, function (err, isSuccess) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(isSuccess); | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
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 @@ | ||
'use strict'; |
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,34 @@ | ||
{ | ||
"name": "{{ name }}", | ||
"version": "0.0.1", | ||
"description": "{{ description }}", | ||
"main": "app.js", | ||
"scripts":{ | ||
"start": "node app.js --harmony" | ||
}, | ||
"dependencies": { | ||
"@mtfe/hlb": "^0.2.0", | ||
{{#tair}} | ||
"@mtfe/node-tair": "0.0.3", | ||
{{/tair}} | ||
"@mx/kms": "^0.2.0", | ||
"co": "^4.5.4", | ||
"co-request": "^0.2.1", | ||
"co-views": "^2.1.0", | ||
"koa": "^0.20.0", | ||
"koa-bodyparser": "^2.0.0", | ||
"koa-compress": "^1.0.8", | ||
"koa-json": "^1.1.1", | ||
"koa-logger": "^1.2.2", | ||
"koa-router": "^5.0.0", | ||
"koa-static": "^2.1.0", | ||
{{#mysql}} | ||
"mysql": "^2.6.1", | ||
"sequelize": "^3.0.1", | ||
{{/mysql}} | ||
"swig": "^1.4.2" | ||
}, | ||
"devDependencies": {}, | ||
"author": "{{ author }}", | ||
"license": "ISC" | ||
} |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,18 @@ | ||
#### 启动服务 | ||
* cd 项目目录 | ||
* npm i | ||
* npm run start | ||
* 访问127.0.0.1:9000 | ||
|
||
#### 静态资源服务 | ||
* 静态资源放在public文件夹下, eg: 放在public/image下面的图片demo.jpg, 访问路径是/image/demo.jpg。 使用示例参考/views/home/index.html | ||
|
||
{{#tair}} | ||
#### 使用tair | ||
* 在config.js文件中, 按照模板配置tair集群的地址 | ||
{{/tair}} | ||
|
||
{{#mysql}} | ||
#### 使用Mysql | ||
* 连接Mysql使用sequelize做ORM, 使用示例参考/service/use_db_demo/user.js,更多用法参见http://docs.sequelizejs.com | ||
{{/mysql}} |
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 @@ | ||
'use strict'; |
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 @@ | ||
'use strict'; |
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,14 @@ | ||
'use strict'; | ||
var render = require('../lib/render.js'); | ||
|
||
var HomeIndex = function* () { | ||
return this.body = yield render('./home/index',{ | ||
project: "{{ name }}", | ||
author: "{{ author }}" | ||
}); | ||
}; | ||
|
||
module.exports.register = function (router) { | ||
router.get('/',HomeIndex); | ||
router.get('/home/index',HomeIndex) | ||
}; |
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,9 @@ | ||
'use strict'; | ||
var Router = require('koa-router'); | ||
var router = new Router(); | ||
|
||
require('./home.js').register(router); | ||
|
||
module.exports.register = function (app) { | ||
app.use(router.middleware()); | ||
}; |
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,16 @@ | ||
'use strict'; | ||
var Sequelize = require('sequelize'); | ||
var config = require('../../lib/config'); | ||
var dbOptions = config.getDbConfig(); | ||
var client = new Sequelize( | ||
dbOptions.database, | ||
dbOptions.username, | ||
dbOptions.password, { | ||
host: dbOptions.host, | ||
dialect: dbOptions.dialect, | ||
port: dbOptions.port, | ||
pool: dbOptions.pool, | ||
logging: true | ||
}); | ||
|
||
module.exports = client; |
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 @@ | ||
'use strict'; |
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,18 @@ | ||
/* | ||
简单演示数据库操作方法, 更多用法参见http://docs.sequelizejs.com | ||
*/ | ||
|
||
'use strict'; | ||
var db = require('../db'); | ||
var Sequelize = require('sequelize'); | ||
module.exports = { | ||
getUserById: function* (id) { | ||
return yield db.query('select name from user where id = :id', { | ||
replacements: { | ||
id: id | ||
}, | ||
type: Sequelize.QueryTypes.SELECT, | ||
raw: true | ||
}); | ||
} | ||
}; |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>demo page</title> | ||
</head> | ||
<body> | ||
<p>hi, <strong>\{{author}}</strong>, this is the index page of your project: <strong>\{{project}}</strong></p> | ||
<img src="/image/yes.jpeg"> | ||
</body> | ||
</html> |