Skip to content

Commit

Permalink
feat: support custom web middlewares (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Apr 10, 2020
1 parent 3cb3fe0 commit e97835f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/index.js
Expand Up @@ -62,6 +62,7 @@ var config = {
viewDir: path.join(root, 'view', 'web'),

customRegistryMiddlewares: [],
customWebMiddlewares: [],

// config for koa-limit middleware
// for limit download rates
Expand Down Expand Up @@ -302,6 +303,13 @@ if (process.env.NODE_ENV === 'test') {
yield next;
};
});

config.customWebMiddlewares.push(() => {
return function* (next) {
this.set('x-custom-web-middleware', 'true');
yield next;
};
});
}

if (process.env.NODE_ENV !== 'test') {
Expand Down
4 changes: 4 additions & 0 deletions servers/web.js
Expand Up @@ -42,6 +42,10 @@ if (config.pagemock) {
}));
}

for (const mw of config.customWebMiddlewares) {
app.use(mw(app));
}

app.use(opensearch);
app.keys = ['todokey', config.sessionSecret];
app.proxy = true;
Expand Down
1 change: 1 addition & 0 deletions test/controllers/web/user/show.test.js
Expand Up @@ -81,6 +81,7 @@ describe('controllers/web/user/show.test.js', function () {

// he.enclde('fengmk2@gmail.com') ↓
assert(res.text.includes('fengmk2@gmail.com'));
assert(res.headers['x-custom-web-middleware'] === 'true');
done()
});
});
Expand Down

0 comments on commit e97835f

Please sign in to comment.