Skip to content

Commit d34120e

Browse files
authored
Merge pull request #129 from leancloud/cloud-function
feat: 增加 AV.Cloud.start() 和 AV.Cloud.stop() API
2 parents df86826 + 92e7e6b commit d34120e

4 files changed

Lines changed: 63 additions & 6 deletions

File tree

.eslintrc.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = {
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"extends": "eslint:recommended",
7+
"rules": {
8+
"indent": [
9+
"error",
10+
2
11+
],
12+
"linebreak-style": [
13+
"error",
14+
"unix"
15+
],
16+
"quotes": [
17+
"error",
18+
"single"
19+
],
20+
"semi": [
21+
"error",
22+
"always"
23+
],
24+
"no-console": 0,
25+
}
26+
};

API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ LeanEngine 中间件会为这些 Hook 函数检查「Hook 签名」,确保调
166166

167167
这些 Hook 函数签名是 `function(func: function)`,其中 `func` 是接受一个 Request 对象作为参数,返回 Promise 的函数,详见文档 [实时通信概览:云引擎 Hook](https://leancloud.cn/docs/realtime_v2.html#云引擎_Hook)
168168

169+
### 启动和停止
170+
171+
单独运行云函数时,可以使用 `AV.Cloud.start()` 启动应用。如果该方法调用时 AV 对象尚未初始化,则 LeanEngine 中间件会使用 `LEANCLOUD_APP_ID` 等环境变量进行初始化。
172+
173+
在需要的时候,可以调用 `AV.Cloud.stop()` 来停止新链接的创建,但是已有链接不会主动断开。
174+
169175
## Middlewares
170176

171177
因为 Node SDK 同时支持多种路由框架,需要你在创建中间件时指定类型,默认为 express:

leanengine.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,7 @@ export namespace Cloud {
145145
export function LeanCloudHeaders(options?: MiddlewareOptions): RequestHandler;
146146
export function CookieSession(options?: CookieSessionOptions): RequestHandler;
147147
export function HttpsRedirect(options?: MiddlewareOptions): RequestHandler;
148+
149+
export function start(): void;
150+
export function stop(): void;
148151
}

lib/cloud.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Cloud.enqueue = function(name, params, options) {
106106
}, options),
107107
authOptions: {useMasterKey: true}
108108
});
109-
}
109+
};
110110

111111
Cloud.beforeSave = function(nameOrClass, func) {
112112
defineClassHook(className(nameOrClass), '__before_save_for_', func);
@@ -154,9 +154,9 @@ AV.Insight.on = function(action, func) {
154154

155155
Cloud.Error = class CloudError extends Error {
156156
constructor(message, extra) {
157-
super()
157+
super();
158158

159-
extra = extra || {}
159+
extra = extra || {};
160160

161161
if (!extra.status) {
162162
extra.status = 400;
@@ -165,11 +165,11 @@ Cloud.Error = class CloudError extends Error {
165165
_.extend(this, {
166166
name: 'CloudError',
167167
message: message
168-
}, extra)
168+
}, extra);
169169

170-
Error.captureStackTrace(this, this.constructor)
170+
Error.captureStackTrace(this, this.constructor);
171171
}
172-
}
172+
};
173173

174174
Cloud.logInByIdAndSessionToken = function(uid, sessionToken, fetchUser, cb) {
175175
if (fetchUser) {
@@ -208,3 +208,25 @@ function className(clazz) {
208208
throw new Error('Unknown class:' + clazz);
209209
}
210210
}
211+
212+
const PORT = parseInt(process.env.LEANCLOUD_APP_PORT || 3000);
213+
let server;
214+
215+
Cloud.start = function() {
216+
if (!AV.applicationId) {
217+
AV.init({
218+
appId: process.env.LEANCLOUD_APP_ID,
219+
appKey: process.env.LEANCLOUD_APP_KEY,
220+
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY,
221+
});
222+
}
223+
server = AV.express().listen(PORT, function() {
224+
console.log('LeanEngine Cloud Functions app is running, port:', PORT);
225+
});
226+
};
227+
228+
Cloud.stop = function() {
229+
if (server) {
230+
server.close();
231+
}
232+
};

0 commit comments

Comments
 (0)