mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 06:46:09 +08:00
增加订阅api
This commit is contained in:
parent
468ff9c1af
commit
419c5a7c5b
|
@ -8,6 +8,7 @@ import script from './script';
|
||||||
import open from './open';
|
import open from './open';
|
||||||
import dependence from './dependence';
|
import dependence from './dependence';
|
||||||
import system from './system';
|
import system from './system';
|
||||||
|
import subscription from './subscription';
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
const app = Router();
|
const app = Router();
|
||||||
|
@ -20,6 +21,7 @@ export default () => {
|
||||||
open(app);
|
open(app);
|
||||||
dependence(app);
|
dependence(app);
|
||||||
system(app);
|
system(app);
|
||||||
|
subscription(app);
|
||||||
|
|
||||||
return app;
|
return app;
|
||||||
};
|
};
|
||||||
|
|
278
back/api/subscription.ts
Normal file
278
back/api/subscription.ts
Normal file
|
@ -0,0 +1,278 @@
|
||||||
|
import { Router, Request, Response, NextFunction } from 'express';
|
||||||
|
import { Container } from 'typedi';
|
||||||
|
import { Logger } from 'winston';
|
||||||
|
import SubscriptionService from '../services/subscription';
|
||||||
|
import { celebrate, Joi } from 'celebrate';
|
||||||
|
import cron_parser from 'cron-parser';
|
||||||
|
const route = Router();
|
||||||
|
|
||||||
|
export default (app: Router) => {
|
||||||
|
app.use('/subscriptions', route);
|
||||||
|
|
||||||
|
route.get('/', async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.list(
|
||||||
|
req.query.searchValue as string,
|
||||||
|
);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
route.post(
|
||||||
|
'/',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.object({
|
||||||
|
type: Joi.string().required(),
|
||||||
|
schedule: Joi.string().required(),
|
||||||
|
name: Joi.string().optional(),
|
||||||
|
url: Joi.string().required(),
|
||||||
|
whitelist: Joi.string().optional(),
|
||||||
|
blacklist: Joi.string().optional(),
|
||||||
|
branch: Joi.string().optional(),
|
||||||
|
dependences: Joi.string().optional(),
|
||||||
|
status: Joi.number().optional(),
|
||||||
|
pull_type: Joi.string().optional(),
|
||||||
|
pull_option: Joi.object().optional(),
|
||||||
|
schedule_type: Joi.number().optional(),
|
||||||
|
alias: Joi.number().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
if (cron_parser.parseExpression(req.body.schedule).hasNext()) {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.create(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} else {
|
||||||
|
return res.send({ code: 400, message: 'param schedule error' });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/run',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.array().items(Joi.number().required()),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.run(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/stop',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.array().items(Joi.number().required()),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.stop(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/disable',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.array().items(Joi.number().required()),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.disabled(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/enable',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.array().items(Joi.number().required()),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.enabled(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.get(
|
||||||
|
'/:id/log',
|
||||||
|
celebrate({
|
||||||
|
params: Joi.object({
|
||||||
|
id: Joi.number().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.log(req.params.id);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.object({
|
||||||
|
type: Joi.string().required(),
|
||||||
|
schedule: Joi.string().required(),
|
||||||
|
name: Joi.string().optional(),
|
||||||
|
url: Joi.string().required(),
|
||||||
|
whitelist: Joi.string().optional(),
|
||||||
|
blacklist: Joi.string().optional(),
|
||||||
|
branch: Joi.string().optional(),
|
||||||
|
dependences: Joi.string().optional(),
|
||||||
|
status: Joi.number().optional(),
|
||||||
|
pull_type: Joi.string().optional(),
|
||||||
|
pull_option: Joi.object().optional(),
|
||||||
|
schedule_type: Joi.number().optional(),
|
||||||
|
alias: Joi.number().required(),
|
||||||
|
id: Joi.number().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
if (
|
||||||
|
!req.body.schedule ||
|
||||||
|
cron_parser.parseExpression(req.body.schedule).hasNext()
|
||||||
|
) {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.update(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} else {
|
||||||
|
return res.send({ code: 400, message: 'param schedule error' });
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.delete(
|
||||||
|
'/',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.array().items(Joi.number().required()),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.remove(req.body);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.get(
|
||||||
|
'/:id',
|
||||||
|
celebrate({
|
||||||
|
params: Joi.object({
|
||||||
|
id: Joi.number().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.getDb({ id: req.params.id });
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.put(
|
||||||
|
'/status',
|
||||||
|
celebrate({
|
||||||
|
body: Joi.object({
|
||||||
|
ids: Joi.array().items(Joi.number().required()),
|
||||||
|
status: Joi.string().required(),
|
||||||
|
pid: Joi.string().optional(),
|
||||||
|
log_path: Joi.string().optional(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.status({
|
||||||
|
...req.body,
|
||||||
|
status: parseInt(req.body.status),
|
||||||
|
pid: parseInt(req.body.pid) || '',
|
||||||
|
});
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
route.get(
|
||||||
|
'/:id/logs',
|
||||||
|
celebrate({
|
||||||
|
params: Joi.object({
|
||||||
|
id: Joi.number().required(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
async (req: Request<{ id: number }>, res: Response, next: NextFunction) => {
|
||||||
|
const logger: Logger = Container.get('logger');
|
||||||
|
try {
|
||||||
|
const subscriptionService = Container.get(SubscriptionService);
|
||||||
|
const data = await subscriptionService.logs(req.params.id);
|
||||||
|
return res.send({ code: 200, data });
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('🔥 error: %o', e);
|
||||||
|
return next(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
|
@ -8,6 +8,7 @@ import { AppModel } from '../data/open';
|
||||||
import { AuthModel } from '../data/auth';
|
import { AuthModel } from '../data/auth';
|
||||||
import { sequelize } from '../data';
|
import { sequelize } from '../data';
|
||||||
import { fileExist } from '../config/util';
|
import { fileExist } from '../config/util';
|
||||||
|
import { SubscriptionModel } from '../data/subscription';
|
||||||
import config from '../config';
|
import config from '../config';
|
||||||
|
|
||||||
export default async () => {
|
export default async () => {
|
||||||
|
@ -17,6 +18,8 @@ export default async () => {
|
||||||
await AppModel.sync();
|
await AppModel.sync();
|
||||||
await AuthModel.sync();
|
await AuthModel.sync();
|
||||||
await EnvModel.sync();
|
await EnvModel.sync();
|
||||||
|
await SubscriptionModel.sync();
|
||||||
|
|
||||||
await sequelize.sync();
|
await sequelize.sync();
|
||||||
|
|
||||||
// try {
|
// try {
|
||||||
|
|
|
@ -19,6 +19,62 @@ import path from 'path';
|
||||||
export default class SubscriptionService {
|
export default class SubscriptionService {
|
||||||
constructor(@Inject('logger') private logger: winston.Logger) {}
|
constructor(@Inject('logger') private logger: winston.Logger) {}
|
||||||
|
|
||||||
|
public async list(searchText?: string): Promise<Subscription[]> {
|
||||||
|
let query = {};
|
||||||
|
if (searchText) {
|
||||||
|
const textArray = searchText.split(':');
|
||||||
|
switch (textArray[0]) {
|
||||||
|
case 'name':
|
||||||
|
case 'command':
|
||||||
|
case 'schedule':
|
||||||
|
case 'label':
|
||||||
|
const column = textArray[0] === 'label' ? 'labels' : textArray[0];
|
||||||
|
query = {
|
||||||
|
[column]: {
|
||||||
|
[Op.or]: [
|
||||||
|
{ [Op.like]: `%${textArray[1]}%` },
|
||||||
|
{ [Op.like]: `%${encodeURIComponent(textArray[1])}%` },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
const reg = {
|
||||||
|
[Op.or]: [
|
||||||
|
{ [Op.like]: `%${searchText}%` },
|
||||||
|
{ [Op.like]: `%${encodeURIComponent(searchText)}%` },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
query = {
|
||||||
|
[Op.or]: [
|
||||||
|
{
|
||||||
|
name: reg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
command: reg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
schedule: reg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
labels: reg,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const result = await SubscriptionModel.findAll({
|
||||||
|
where: query,
|
||||||
|
order: [['createdAt', 'DESC']],
|
||||||
|
});
|
||||||
|
return result as any;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async create(payload: Subscription): Promise<Subscription> {
|
public async create(payload: Subscription): Promise<Subscription> {
|
||||||
const tab = new Subscription(payload);
|
const tab = new Subscription(payload);
|
||||||
const doc = await this.insert(tab);
|
const doc = await this.insert(tab);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user