前言
- 由于我司私服需要对某些包删除使用,但又不想误删,所以要整个黑名单,在该名单上的包无法下载和搜索。
- 大概看了下,只要维护个黑名单表即可,另外再改点逻辑,对表写个添加,删除,查询接口即可。
- 首先梳理下路由,会走那些控制器,再建个表,写入增删改查。
路由梳理
- 下载包实际上是先查询,查到后走json的tar地址,所以在搜索时先查黑名单,黑名单里的直接返回404
页面搜索
controllers:web:package:search
页面详情
controllers:web:package:show
下载(查询地址后)
controllers:registry:download
controllers:registry:package:save
查询(下载前)
controllers:registry:package:list
建表
CREATE TABLE IF NOT EXISTS `black_list` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`gmt_create` datetime NOT NULL COMMENT 'create time',
`gmt_modified` datetime NOT NULL COMMENT 'modified time',
`name` varchar(214) NOT NULL COMMENT 'pkg name',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`name`),
KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='black list info';
BlackList: load("black_list"),
"use strict";
module.exports = function (sequelize, DataTypes) {
return sequelize.define(
"BlackList",
{
name: {
type: DataTypes.STRING(214),
allowNull: false,
comment: "module name",
},
},
{
tableName: "black_list",
comment: "module black list",
indexes: [
{
unique: true,
fields: ["name"],
},
],
classMethods: {},
}
);
};
"use strict";
var blackListService = require("../../../services/black_list");
exports.addBlackList = function* () {
var name = this.request.body.name;
const res = yield blackListService.addBlackList(name);
this.body = res;
};
exports.getBlackList = function* () {
var name = this.request.body.name;
const res = yield blackListService.getBlackList(name);
if (!res){
this.body = {
err: "not found",
};
} else {
this.body = res;
}
};
exports.deleteBlackList = function* () {
var name = this.request.body.name;
const res = yield blackListService.deleteBlackList(name);
if (res === 1) {
this.body = {
success: true,
};
} else {
this.body = {
success: false,
};
}
};
"use strict";
var models = require("../models");
var blacklist = models.BlackList;
exports.getBlackList = function* (name) {
var rows = yield blacklist.find({
where: {
name: name,
},
});
return rows;
};
exports.addBlackList = function* (name) {
var item = blacklist.build({
name: name,
});
yield item.save();
return item;
};
exports.deleteBlackList = function* (name) {
var item = yield blacklist.destroy({
where: {
name: name,
},
});
return item;
};
app.post("/blacklist/addpkg", blackList.addBlackList);
app.post("/blacklist/deletpkg", blackList.deleteBlackList);
app.post("/blacklist/getpkg", blackList.getBlackList);
- 此时增删查已经可以了,下面黑名单的直接不给下载就在list查询处返回404即可:
const blackres = yield blackListService.getBlackList(name);
if (blackres) {
this.status = 404;
return;
}
- 在搜索时搜到不影响,在详情页面web/package/show增加废弃提示:
const blackres = yield blackListService.getBlackList(pkg.name);
if (blackres) {
pkg.name = pkg.name + "-@已废弃";
}
- 改造就基本完成了。
- 另外有人提意改写unpublish命令,顺便写一下。
- unpublish在controllers/registry/package/remove.js里加入
var blackListService = require("../../../services/black_list");
...
yield blackListService.addBlackList(name);
debug("add blacklist");
return yield next;