'use strict'; const { Pool } = require('pg'); const moment = require('moment') const latestMetadata = require('../../controllers/latestMetadata'); module.exports = function (app, router, opts) { app.fs.api.logAttr['GET/resource-catalog'] = { content: '获取资源目录', visible: false }; router.get('/resource-catalog', latestMetadata.getResourceCatalog); app.fs.api.logAttr['POST/resource-catalog'] = { content: '新建资源目录', visible: true }; router.post('/resource-catalog', latestMetadata.postResourceCatalog); app.fs.api.logAttr['PUT/resource-catalog/:id'] = { content: '修改资源目录', visible: true }; router.put('/resource-catalog/:id', latestMetadata.putResourceCatalog); app.fs.api.logAttr['DEL/resource-catalog/:id'] = { content: '删除资源目录', visible: true }; router.delete('/resource-catalog/:id', latestMetadata.delResourceCatalog); app.fs.api.logAttr['GET/metadata/databases'] = { content: '获取库表元数据列表', visible: false }; router.get('/metadata/databases', latestMetadata.getMetadataDatabases); app.fs.api.logAttr['GET/metadata/files'] = { content: '获取文件元数据列表', visible: false }; router.get('/metadata/files', latestMetadata.getMetadataFiles); app.fs.api.logAttr['GET/metadata/restapis'] = { content: '获取接口元数据列表', visible: false }; router.get('/metadata/restapis', latestMetadata.getMetadataRestapis); app.fs.api.logAttr['GET/metadata/models'] = { content: '获取元数据模型', visible: false }; router.get('/metadata/models', latestMetadata.getMetadataModels); app.fs.api.logAttr['POST/metadata/databases'] = { content: '新建库表元数据', visible: true }; router.post('/metadata/databases', latestMetadata.postMetadataDatabases); app.fs.api.logAttr['PUT/metadata/databases/:id'] = { content: '修改库表元数据', visible: true }; router.put('/metadata/databases/:id', latestMetadata.putMetadataDatabases); app.fs.api.logAttr['DEL/metadata/databases/:id'] = { content: '删除库表元数据', visible: true }; router.delete('/metadata/databases/:id', latestMetadata.delMetadataDatabases); app.fs.api.logAttr['GET/metadata/databases/:id'] = { content: '获取库表元数据基本信息', visible: false }; router.get('/metadata/databases/:id', latestMetadata.getMetadataDatabasesById); app.fs.api.logAttr['POST/tag/metadata'] = { content: '打标元数据', visible: true }; router.post('/tag/metadata', latestMetadata.postTagMetadata); app.fs.api.logAttr['GET/tag/metadata/:id'] = { content: '获取元数据已打标数据', visible: true }; router.get('/tag/metadata/:id', latestMetadata.getTagMetadata); app.fs.api.logAttr['POST/resource-consumption/applications'] = { content: '申请资源', visible: true }; router.post('/resource-consumption/applications', latestMetadata.postMetadataResourceApplications); app.fs.api.logAttr['GET/resource-consumption/applications'] = { content: '获取元数据资源申请记录', visible: true }; router.get('/resource-consumption/applications', latestMetadata.getMetadataResourceApplications); app.fs.api.logAttr['POST/metadata/files'] = { content: '新建文件元数据', visible: true }; router.post('/metadata/files', latestMetadata.postMetadataFiles); app.fs.api.logAttr['PUT/metadata/files/:id'] = { content: '修改文件元数据', visible: true }; router.put('/metadata/files/:id', latestMetadata.putMetadataFiles); app.fs.api.logAttr['DEL/metadata/files/:id'] = { content: '删除文件元数据', visible: true }; router.delete('/metadata/files/:id', latestMetadata.delMetadataFiles); app.fs.api.logAttr['POST/metadata/restapis'] = { content: '新建接口元数据', visible: true }; router.post('/metadata/restapis', latestMetadata.postMetadataRestapis); app.fs.api.logAttr['PUT/metadata/restapis/:id'] = { content: '修改接口元数据', visible: true }; router.put('/metadata/restapis/:id', latestMetadata.putMetadataRestapis); app.fs.api.logAttr['DEL/metadata/restapis/:id'] = { content: '删除接口元数据', visible: true }; router.delete('/metadata/restapis/:id', latestMetadata.delMetadataRestapis); app.fs.api.logAttr['GET/listStructuredData'] = { content: '获取对表的库与字段信息', visible: true }; router.get('/listStructuredData', latestMetadata.listStructuredData); app.fs.api.logAttr['POST/publishing/services'] = { content: '发布REST服务', visible: true }; router.post('/publishing/services', async (ctx) => { const data = ctx.request.body; opts.exclude.push({ p: data.url, o: 'GET' }); router.get(data.url, async (ctx) => { let message = "获取库表元数据列表失败" try { const models = ctx.fs.dc.models; const { token } = ctx.query; if (!token) { message = '缺少访问令牌' throw '' } else { let tokens let findOne = await models.RestfulApi.findOne({ where: { url: data.url, method: data.method }, order: [["id", "desc"]], include: [{ model: models.ResourceConsumption, }], distinct: true }); findOne && findOne.resourceConsumptions.map(s => { if (!tokens && s.token) { tokens = s.token } }) if (tokens && tokens == token) { if (findOne.enabled) { const pool = new Pool({ user: ctx.fs.dc.orm.config.username, host: ctx.fs.dc.orm.config.host, database: findOne.table, password: ctx.fs.dc.orm.config.password, port: ctx.fs.dc.orm.config.port, }) const client = await pool.connect() const res = await client.query(findOne.conditions, []) await models.RestfulApiRecord.create({ restServiceId: findOne.id, visitTime: moment().format('YYYY-MM-DD HH:mm:ss'), token: tokens }) ctx.status = 200; ctx.body = res; } else { message = '该REST服务被禁用' throw '' } } else { message = '该服务令牌错误' throw '' } } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": message } } }); let message = "发布REST服务失败" try { const models = ctx.fs.dc.models; const data = ctx.request.body; const { method, url } = data const postOne = await models.RestfulApi.findOne({ where: { method: method, url: url } }); if (postOne) { message = '路由和请求方式重复' throw '' } await models.RestfulApi.create(data) ctx.body = { message: '发布REST服务成功' } ctx.status = 200; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": message } } }); async function release (apps, opts) { const models = apps.fs.dc.models; const list = await models.RestfulApi.findAll() || [] list.map(v => { opts.exclude.push({ p: v.url, o: 'GET' }); router.get(v.url, async (ctx) => { let message = "获取库表元数据列表失败" try { const { token } = ctx.query; if (!token) { message = '缺少访问令牌' throw '' } else { let tokens let findOne = await models.RestfulApi.findOne({ where: { url: v.url, method: v.method }, order: [["id", "desc"]], include: [{ model: models.ResourceConsumption, }], distinct: true }); findOne && findOne.resourceConsumptions.map(s => { if (!tokens && s.token) { tokens = s.token } }) if (tokens && tokens == token) { if (findOne.enabled) { const pool = new Pool({ user: ctx.fs.dc.orm.config.username, host: ctx.fs.dc.orm.config.host, database: v.table, password: ctx.fs.dc.orm.config.password, port: ctx.fs.dc.orm.config.port, }) const client = await pool.connect() const res = await client.query(v.conditions, []) await models.RestfulApiRecord.create({ restServiceId: v.id, visitTime: moment().format('YYYY-MM-DD HH:mm:ss'), token: tokens }) ctx.status = 200; ctx.body = res; } else { message = '该REST服务被禁用' throw '' } } else { message = '该服务令牌错误' throw '' } } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { "message": message } } }); }) } release(app, opts) };