Skip to content

Commit

Permalink
feat: optional email config
Browse files Browse the repository at this point in the history
  • Loading branch information
Youjingyu committed Mar 27, 2019
1 parent 20a1941 commit 0f3ef84
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/built_in/glues/daruk_nodemailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Daruk } from '../../typings/daruk';
import { createTransport } from 'nodemailer';

export default function(app: Daruk.DarukCore) {
// 如果用户没有添加邮件配置
if (!app.options.nodemailer) return null;
const mail = createTransport(app.options.nodemailer);
mail.verify(function handleMailVerificationResult(err: any, success: any) {
if (err) {
Expand Down
22 changes: 13 additions & 9 deletions src/built_in/glues/daruk_shutdown_notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import os = require('os');
import { Daruk } from '../../typings/daruk';
import { parallelWithNoBreak } from '../../utils';

const noop = () => {};

export default function(app: Daruk.DarukCore) {
let alertAccounts = app.options.alertAccounts;
assert(
alertAccounts && alertAccounts.length > 0,
'daruk.options.alertAccounts with email address required'
);
return () => {
// 如果没有配置邮箱服务
if (!app.options.nodemailer) return noop;
return function shutDownNotify() {
let alertAccounts = app.options.alertAccounts;
assert(
alertAccounts && alertAccounts.length > 0,
'daruk.options.alertAccounts with email address required'
);
app.exitHook.addHook(function handleShutdownNotify(err: Error, cb: Function) {
// 为了避免频繁的邮件通知,只有报错重启时,才做通知
if (!err) return cb();
shutDownNotify(err, app).then(() => {
doNotify(err, app).then(() => {
cb();
});
});
};
}

function shutDownNotify(err: Error, app: Daruk.DarukCore) {
function doNotify(err: Error, app: Daruk.DarukCore) {
const tos = app.options.alertAccounts;
const reason = (err && (err.stack || err.message)) || 'no error message';
return new Promise((resolve) => {
Expand All @@ -34,7 +38,7 @@ function shutDownNotify(err: Error, app: Daruk.DarukCore) {
app.name
} server is shutdown in ${os.hostname()} at ${new Date().toLocaleString()} - ${reason}`;

const { daruk_nodemailer, daruk_sina_watch } = app.glue;
const { daruk_nodemailer } = app.glue;
const mailOptions = daruk_nodemailer.options;
const from = `${mailOptions.auth.user}@${mailOptions.domain}`;

Expand Down
10 changes: 0 additions & 10 deletions src/core/daruk_default_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ export default function getDefaultOptions(rootPath: string, name: string, debug:
loggerMiddleware: {},
requestId: {
inject: true
},
nodemailer: {
debug,
host: 'smtp.sina.com',
secureConnection: true,
auth: {
user: 'fedvip',
pass: '1234qwerasdfzxcv'
},
domain: 'sina.com'
}
};
}
2 changes: 1 addition & 1 deletion types/daruk_options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface Options {
timeout: number;
};
requestId: any;
nodemailer: any;
nodemailer?: any;
[key: string]: any;
}

Expand Down

0 comments on commit 0f3ef84

Please sign in to comment.