mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-04 18:35:17 +02:00
Merge branch 'master' of https://github.com/pawelmalak/flame into merge_upstream_2020-12-06
This commit is contained in:
commit
021bd4e85a
266 changed files with 13470 additions and 7067 deletions
|
@ -1,351 +0,0 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const App = require('../models/App');
|
||||
const Config = require('../models/Config');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const axios = require('axios');
|
||||
const Logger = require('../utils/Logger');
|
||||
const Category = require('../models/Category');
|
||||
const { dockerDefaultCategory, kubernetesDefaultCategory } = require('./category');
|
||||
const logger = new Logger();
|
||||
const k8s = require('@kubernetes/client-node');
|
||||
|
||||
// @desc Create new app
|
||||
// @route POST /api/apps
|
||||
// @access Public
|
||||
exports.createApp = asyncWrapper(async (req, res, next) => {
|
||||
// Get config from database
|
||||
const pinApps = await Config.findOne({
|
||||
where: { key: 'pinAppsByDefault' },
|
||||
});
|
||||
|
||||
let app;
|
||||
|
||||
let _body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
isPinned: (pinApps && parseInt(pinApps.value)),
|
||||
};
|
||||
|
||||
if (req.file) {
|
||||
_body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
app = await App.create(_body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get all apps
|
||||
// @route GET /api/apps
|
||||
// @access Public
|
||||
exports.getApps = asyncWrapper(async (req, res, next) => {
|
||||
// Get config from database
|
||||
const useOrdering = await Config.findOne({
|
||||
where: { key: 'useOrdering' },
|
||||
});
|
||||
const useDockerApi = await Config.findOne({
|
||||
where: { key: 'dockerApps' },
|
||||
});
|
||||
const useKubernetesApi = await Config.findOne({
|
||||
where: { key: 'kubernetesApps' },
|
||||
});
|
||||
const unpinStoppedApps = await Config.findOne({
|
||||
where: { key: 'unpinStoppedApps' },
|
||||
});
|
||||
|
||||
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
||||
let apps;
|
||||
|
||||
if (useDockerApi && useDockerApi.value == 1) {
|
||||
apps = await retrieveDockerApps(apps, orderType, unpinStoppedApps);
|
||||
}
|
||||
|
||||
if (useKubernetesApi && useKubernetesApi.value == 1) {
|
||||
apps = await retrieveKubernetesApps(apps, orderType, unpinStoppedApps);
|
||||
}
|
||||
|
||||
if (orderType == 'name') {
|
||||
apps = await App.findAll({
|
||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
||||
});
|
||||
} else {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
});
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// Set header to fetch containers info every time
|
||||
res.status(200).setHeader('Cache-Control', 'no-store').json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get single app
|
||||
// @route GET /api/apps/:id
|
||||
// @access Public
|
||||
exports.getApp = asyncWrapper(async (req, res, next) => {
|
||||
const app = await App.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(`App with id of ${req.params.id} was not found`, 404)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update app
|
||||
// @route PUT /api/apps/:id
|
||||
// @access Public
|
||||
exports.updateApp = asyncWrapper(async (req, res, next) => {
|
||||
let app = await App.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(`App with id of ${req.params.id} was not found`, 404)
|
||||
);
|
||||
}
|
||||
|
||||
let _body = { ...req.body };
|
||||
|
||||
if (req.file) {
|
||||
_body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
app = await app.update(_body);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Delete app
|
||||
// @route DELETE /api/apps/:id
|
||||
// @access Public
|
||||
exports.deleteApp = asyncWrapper(async (req, res, next) => {
|
||||
await App.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Reorder apps
|
||||
// @route PUT /api/apps/0/reorder
|
||||
// @access Public
|
||||
exports.reorderApps = asyncWrapper(async (req, res, next) => {
|
||||
req.body.apps.forEach(async ({ id, orderId }) => {
|
||||
await App.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
async function retrieveDockerApps(apps, orderType, unpinStoppedApps) {
|
||||
let containers = null;
|
||||
|
||||
try {
|
||||
let { data } = await axios.get(
|
||||
'http://localhost/containers/json?{"status":["running"]}',
|
||||
{
|
||||
socketPath: '/var/run/docker.sock'
|
||||
}
|
||||
);
|
||||
containers = data;
|
||||
} catch {
|
||||
logger.log("Can't connect to the docker socket", 'ERROR');
|
||||
}
|
||||
|
||||
if (containers) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']]
|
||||
});
|
||||
|
||||
const categories = await Category.findAll({
|
||||
where: {
|
||||
type: 'apps'
|
||||
},
|
||||
order: [[orderType, 'ASC']]
|
||||
});
|
||||
|
||||
containers = containers.filter((e) => Object.keys(e.Labels).length !== 0);
|
||||
const dockerApps = [];
|
||||
for (const container of containers) {
|
||||
const labels = container.Labels;
|
||||
|
||||
if ('flame.name' in labels &&
|
||||
'flame.url' in labels &&
|
||||
/^app/.test(labels['flame.type'])) {
|
||||
const names = labels['flame.name'].split(';');
|
||||
const urls = labels['flame.url'].split(';');
|
||||
const categoriesLabels = labels['flame.category'] ? labels['flame.category'].split(';') : [];
|
||||
const orders = labels['flame.order'] ? labels['flame.order'].split(';') : [];
|
||||
const icons = labels['flame.icon'] ? labels['flame.icon'].split(';') : [];
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
}
|
||||
}
|
||||
|
||||
dockerApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'docker',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps && unpinStoppedApps.value == 1) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of dockerApps) {
|
||||
if (apps.some(app => app.name === item.name)) {
|
||||
const app = apps.filter(e => e.name === item.name)[0];
|
||||
await app.update({ ...item, isPinned: true });
|
||||
} else {
|
||||
await App.create({
|
||||
...item,
|
||||
isPinned: true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return apps;
|
||||
}
|
||||
|
||||
async function createNewCategory(newCategoryName) {
|
||||
return await Category.create({
|
||||
name: newCategoryName,
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||
});
|
||||
}
|
||||
|
||||
async function retrieveKubernetesApps(apps, orderType, unpinStoppedApps) {
|
||||
let ingresses = null;
|
||||
|
||||
try {
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromCluster();
|
||||
const k8sNetworkingV1Api = kc.makeApiClient(k8s.NetworkingV1Api);
|
||||
await k8sNetworkingV1Api.listIngressForAllNamespaces()
|
||||
.then((res) => {
|
||||
ingresses = res.body.items;
|
||||
});
|
||||
} catch {
|
||||
logger.log("Can't connect to the kubernetes api", 'ERROR');
|
||||
}
|
||||
|
||||
if (ingresses) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']]
|
||||
});
|
||||
|
||||
|
||||
const categories = await Category.findAll({
|
||||
where: {
|
||||
type: 'apps'
|
||||
},
|
||||
order: [[orderType, 'ASC']]
|
||||
});
|
||||
|
||||
ingresses = ingresses.filter((e) => Object.keys(e.metadata.annotations).length !== 0);
|
||||
const kubernetesApps = [];
|
||||
for (const ingress of ingresses) {
|
||||
const annotations = ingress.metadata.annotations;
|
||||
|
||||
if ('flame.pawelmalak/name' in annotations &&
|
||||
'flame.pawelmalak/url' in annotations &&
|
||||
/^app/.test(annotations['flame.pawelmalak/type'])) {
|
||||
|
||||
const names = annotations['flame.pawelmalak/.name'].split(';');
|
||||
const urls = annotations['flame.pawelmalak/url'].split(';');
|
||||
const categoriesLabels = annotations['flame.pawelmalak/category'] ? annotations['flame.pawelmalak/category'].split(';') : [];
|
||||
const orders = annotations['flame.pawelmalak/order'] ? annotations['flame.pawelmalak/order'].split(';') : [];
|
||||
const icons = annotations['flame.pawelmalak/icon'] ? annotations['flame.pawelmalak/icon'].split(';') : [];;
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
}
|
||||
}
|
||||
|
||||
kubernetesApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'docker',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps && unpinStoppedApps.value == 1) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of kubernetesApps) {
|
||||
if (apps.some(app => app.name === item.name)) {
|
||||
const app = apps.filter(e => e.name === item.name)[0];
|
||||
await app.update({ ...item, isPinned: true });
|
||||
} else {
|
||||
await App.create({
|
||||
...item,
|
||||
isPinned: true
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return apps;
|
||||
}
|
||||
|
33
controllers/apps/createApp.js
Normal file
33
controllers/apps/createApp.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Create new app
|
||||
// @route POST /api/apps
|
||||
// @access Public
|
||||
const createApp = asyncWrapper(async (req, res, next) => {
|
||||
const { pinAppsByDefault } = await loadConfig();
|
||||
|
||||
let body = { ...req.body };
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
const app = await App.create({
|
||||
...body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
isPinned: pinAppsByDefault,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = createApp;
|
18
controllers/apps/deleteApp.js
Normal file
18
controllers/apps/deleteApp.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Delete app
|
||||
// @route DELETE /api/apps/:id
|
||||
// @access Public
|
||||
const deleteApp = asyncWrapper(async (req, res, next) => {
|
||||
await App.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteApp;
|
4
controllers/apps/docker/index.js
Normal file
4
controllers/apps/docker/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
useKubernetes: require('./useKubernetes'),
|
||||
useDocker: require('./useDocker'),
|
||||
};
|
176
controllers/apps/docker/useDocker.js
Normal file
176
controllers/apps/docker/useDocker.js
Normal file
|
@ -0,0 +1,176 @@
|
|||
const App = require('../../../models/App');
|
||||
const axios = require('axios');
|
||||
const Logger = require('../../../utils/Logger');
|
||||
const logger = new Logger();
|
||||
const loadConfig = require('../../../utils/loadConfig');
|
||||
|
||||
dockerDefaultCategory = {
|
||||
id: -2,
|
||||
name: 'Docker',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 998,
|
||||
};
|
||||
|
||||
const useDocker = async (apps) => {
|
||||
const {
|
||||
useOrdering: orderType,
|
||||
unpinStoppedApps,
|
||||
dockerHost: host,
|
||||
} = await loadConfig();
|
||||
|
||||
let containers = null;
|
||||
|
||||
// Get list of containers
|
||||
try {
|
||||
if (host.includes('localhost')) {
|
||||
// Use default host
|
||||
let { data } = await axios.get(
|
||||
`http://${host}/containers/json?{"status":["running"]}`,
|
||||
{
|
||||
socketPath: '/var/run/docker.sock',
|
||||
}
|
||||
);
|
||||
|
||||
containers = data;
|
||||
} else {
|
||||
// Use custom host
|
||||
let { data } = await axios.get(
|
||||
`http://${host}/containers/json?{"status":["running"]}`
|
||||
);
|
||||
|
||||
containers = data;
|
||||
}
|
||||
} catch {
|
||||
logger.log(`Can't connect to the Docker API on ${host}`, 'ERROR');
|
||||
}
|
||||
|
||||
if (containers) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
});
|
||||
|
||||
// Filter out containers without any annotations
|
||||
containers = containers.filter((e) => Object.keys(e.Labels).length !== 0);
|
||||
|
||||
const dockerApps = [];
|
||||
|
||||
for (const container of containers) {
|
||||
let labels = container.Labels;
|
||||
|
||||
// Traefik labels for URL configuration
|
||||
if (!('flame.url' in labels)) {
|
||||
for (const label of Object.keys(labels)) {
|
||||
if (/^traefik.*.frontend.rule/.test(label)) {
|
||||
// Traefik 1.x
|
||||
let value = labels[label];
|
||||
|
||||
if (value.indexOf('Host') !== -1) {
|
||||
value = value.split('Host:')[1];
|
||||
labels['flame.url'] =
|
||||
'https://' + value.split(',').join(';https://');
|
||||
}
|
||||
} else if (/^traefik.*?\.rule/.test(label)) {
|
||||
// Traefik 2.x
|
||||
const value = labels[label];
|
||||
|
||||
if (value.indexOf('Host') !== -1) {
|
||||
const regex = /\`([a-zA-Z0-9\.\-]+)\`/g;
|
||||
const domains = [];
|
||||
|
||||
while ((match = regex.exec(value)) != null) {
|
||||
domains.push('http://' + match[1]);
|
||||
}
|
||||
|
||||
if (domains.length > 0) {
|
||||
labels['flame.url'] = domains.join(';');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add each container as flame formatted app
|
||||
if (
|
||||
'flame.name' in labels &&
|
||||
'flame.url' in labels &&
|
||||
/^app/.test(labels['flame.type'])
|
||||
) {
|
||||
const names = labels['flame.name'].split(';');
|
||||
const urls = labels['flame.url'].split(';');
|
||||
const categoriesLabels = labels['flame.category'] ? labels['flame.category'].split(';') : [];
|
||||
const orders = labels['flame.order'] ? labels['flame.order'].split(';') : [];
|
||||
const icons = labels['flame.icon'] ? labels['flame.icon'].split(';') : [];
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : dockerDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
} else {
|
||||
category = dockerDefaultCategory;
|
||||
}
|
||||
}
|
||||
|
||||
dockerApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'docker',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of dockerApps) {
|
||||
// If app already exists, update it
|
||||
if (apps.some((app) => app.name === item.name)) {
|
||||
const app = apps.find((a) => a.name === item.name);
|
||||
|
||||
if (
|
||||
item.icon === 'custom' ||
|
||||
(item.icon === 'docker' && app.icon != 'docker')
|
||||
) {
|
||||
// update without overriding icon
|
||||
await app.update({
|
||||
name: item.name,
|
||||
url: item.url,
|
||||
isPinned: true,
|
||||
});
|
||||
} else {
|
||||
await app.update({
|
||||
...item,
|
||||
isPinned: true,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// else create new app
|
||||
await App.create({
|
||||
...item,
|
||||
icon: item.icon === 'custom' ? 'docker' : item.icon,
|
||||
isPinned: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// TODO : Move somewhere else ?
|
||||
async function createNewCategory(newCategoryName) {
|
||||
return await Category.create({
|
||||
name: newCategoryName,
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = useDocker;
|
115
controllers/apps/docker/useKubernetes.js
Normal file
115
controllers/apps/docker/useKubernetes.js
Normal file
|
@ -0,0 +1,115 @@
|
|||
const App = require('../../../models/App');
|
||||
const k8s = require('@kubernetes/client-node');
|
||||
const Logger = require('../../../utils/Logger');
|
||||
const logger = new Logger();
|
||||
const loadConfig = require('../../../utils/loadConfig');
|
||||
|
||||
const kubernetesDefaultCategory = {
|
||||
id: -3,
|
||||
name: 'Kubernetes',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 999,
|
||||
};
|
||||
|
||||
const useKubernetes = async (apps) => {
|
||||
const { useOrdering: orderType, unpinStoppedApps } = await loadConfig();
|
||||
|
||||
let ingresses = null;
|
||||
|
||||
try {
|
||||
const kc = new k8s.KubeConfig();
|
||||
kc.loadFromCluster();
|
||||
const k8sNetworkingV1Api = kc.makeApiClient(k8s.NetworkingV1Api);
|
||||
await k8sNetworkingV1Api.listIngressForAllNamespaces().then((res) => {
|
||||
ingresses = res.body.items;
|
||||
});
|
||||
} catch {
|
||||
logger.log("Can't connect to the Kubernetes API", 'ERROR');
|
||||
}
|
||||
|
||||
if (ingresses) {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
});
|
||||
|
||||
ingresses = ingresses.filter(
|
||||
(e) => Object.keys(e.metadata.annotations).length !== 0
|
||||
);
|
||||
|
||||
const kubernetesApps = [];
|
||||
|
||||
for (const ingress of ingresses) {
|
||||
const annotations = ingress.metadata.annotations;
|
||||
|
||||
if (
|
||||
'flame.pawelmalak/name' in annotations &&
|
||||
'flame.pawelmalak/url' in annotations &&
|
||||
/^app/.test(annotations['flame.pawelmalak/type'])
|
||||
) {
|
||||
|
||||
const names = annotations['flame.pawelmalak/.name'].split(';');
|
||||
const urls = annotations['flame.pawelmalak/url'].split(';');
|
||||
const categoriesLabels = annotations['flame.pawelmalak/category'] ? annotations['flame.pawelmalak/category'].split(';') : [];
|
||||
const orders = annotations['flame.pawelmalak/order'] ? annotations['flame.pawelmalak/order'].split(';') : [];
|
||||
const icons = annotations['flame.pawelmalak/icon'] ? annotations['flame.pawelmalak/icon'].split(';') : [];
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const category = categoriesLabels[i] ? categories.find(category => category.name.toUpperCase() === categoriesLabels[i].toUpperCase()) : kubernetesDefaultCategory;
|
||||
if (!category) {
|
||||
category = await createNewCategory(categoriesLabels[i]);
|
||||
if (category) {
|
||||
categories.push(category);
|
||||
} else {
|
||||
category = kubernetesDefaultCategory;
|
||||
}
|
||||
}
|
||||
|
||||
kubernetesApps.push({
|
||||
name: names[i] || names[0],
|
||||
url: urls[i] || urls[0],
|
||||
icon: icons[i] || 'kubernetes',
|
||||
category: category.id,
|
||||
orderId: orders[i] || 500,
|
||||
});
|
||||
}
|
||||
|
||||
kubernetesApps.push({
|
||||
name: annotations['flame.pawelmalak/name'],
|
||||
url: annotations['flame.pawelmalak/url'],
|
||||
icon: annotations['flame.pawelmalak/icon'] || 'kubernetes',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (unpinStoppedApps) {
|
||||
for (const app of apps) {
|
||||
await app.update({ isPinned: false });
|
||||
}
|
||||
}
|
||||
|
||||
for (const item of kubernetesApps) {
|
||||
if (apps.some((app) => app.name === item.name)) {
|
||||
const app = apps.find((a) => a.name === item.name);
|
||||
await app.update({ ...item, isPinned: true });
|
||||
} else {
|
||||
await App.create({
|
||||
...item,
|
||||
isPinned: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// TODO : Move somewhere else ?
|
||||
async function createNewCategory(newCategoryName) {
|
||||
return await Category.create({
|
||||
name: newCategoryName,
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: Number.MAX_SAFE_INTEGER //New category will always be last and can then be re-ordered manually by user
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = useKubernetes;
|
55
controllers/apps/getAllApps.js
Normal file
55
controllers/apps/getAllApps.js
Normal file
|
@ -0,0 +1,55 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
const { useKubernetes, useDocker } = require('./docker');
|
||||
|
||||
// @desc Get all apps
|
||||
// @route GET /api/apps
|
||||
// @access Public
|
||||
const getAllApps = asyncWrapper(async (req, res, next) => {
|
||||
const {
|
||||
useOrdering: orderType,
|
||||
dockerApps: useDockerAPI,
|
||||
kubernetesApps: useKubernetesAPI,
|
||||
} = await loadConfig();
|
||||
|
||||
let apps;
|
||||
|
||||
if (useDockerAPI) {
|
||||
await useDocker(apps);
|
||||
}
|
||||
|
||||
if (useKubernetesAPI) {
|
||||
await useKubernetes(apps);
|
||||
}
|
||||
|
||||
// apps visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||
: [[orderType, 'ASC']];
|
||||
|
||||
apps = await App.findAll({
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// Set header to fetch containers info every time
|
||||
return res.status(200).setHeader('Cache-Control', 'no-store').json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: apps,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllApps;
|
30
controllers/apps/getSingleApp.js
Normal file
30
controllers/apps/getSingleApp.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
|
||||
// @desc Get single app
|
||||
// @route GET /api/apps/:id
|
||||
// @access Public
|
||||
const getSingleApp = asyncWrapper(async (req, res, next) => {
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const app = await App.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`App with the id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleApp;
|
8
controllers/apps/index.js
Normal file
8
controllers/apps/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
createApp: require('./createApp'),
|
||||
getSingleApp: require('./getSingleApp'),
|
||||
deleteApp: require('./deleteApp'),
|
||||
updateApp: require('./updateApp'),
|
||||
reorderApps: require('./reorderApps'),
|
||||
getAllApps: require('./getAllApps'),
|
||||
};
|
23
controllers/apps/reorderApps.js
Normal file
23
controllers/apps/reorderApps.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Reorder apps
|
||||
// @route PUT /api/apps/0/reorder
|
||||
// @access Public
|
||||
const reorderApps = asyncWrapper(async (req, res, next) => {
|
||||
req.body.apps.forEach(async ({ id, orderId }) => {
|
||||
await App.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderApps;
|
39
controllers/apps/updateApp.js
Normal file
39
controllers/apps/updateApp.js
Normal file
|
@ -0,0 +1,39 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const App = require('../../models/App');
|
||||
|
||||
// @desc Update app
|
||||
// @route PUT /api/apps/:id
|
||||
// @access Public
|
||||
const updateApp = asyncWrapper(async (req, res, next) => {
|
||||
let app = await App.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`App with the id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let body = { ...req.body };
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
app = await app.update(body);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: app,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateApp;
|
4
controllers/auth/index.js
Normal file
4
controllers/auth/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
login: require('./login'),
|
||||
validate: require('./validate'),
|
||||
};
|
25
controllers/auth/login.js
Normal file
25
controllers/auth/login.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const signToken = require('../../utils/signToken');
|
||||
|
||||
// @desc Login user
|
||||
// @route POST /api/auth/
|
||||
// @access Public
|
||||
const login = asyncWrapper(async (req, res, next) => {
|
||||
const { password, duration } = req.body;
|
||||
|
||||
const isMatch = process.env.PASSWORD == password;
|
||||
|
||||
if (!isMatch) {
|
||||
return next(new ErrorResponse('Invalid credentials', 401));
|
||||
}
|
||||
|
||||
const token = signToken(duration);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: { token },
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = login;
|
21
controllers/auth/validate.js
Normal file
21
controllers/auth/validate.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
// @desc Verify token
|
||||
// @route POST /api/auth/verify
|
||||
// @access Public
|
||||
const validate = asyncWrapper(async (req, res, next) => {
|
||||
try {
|
||||
jwt.verify(req.body.token, process.env.SECRET);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: { token: { isValid: true } },
|
||||
});
|
||||
} catch (err) {
|
||||
return next(new ErrorResponse('Token expired', 401));
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = validate;
|
|
@ -1,148 +0,0 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Bookmark = require('../models/Bookmark');
|
||||
const Config = require('../models/Config');
|
||||
const { Sequelize } = require('sequelize');
|
||||
|
||||
// @desc Create new bookmark
|
||||
// @route POST /api/bookmarks
|
||||
// @access Public
|
||||
exports.createBookmark = asyncWrapper(async (req, res, next) => {
|
||||
const pinBookmarks = await Config.findOne({
|
||||
where: { key: 'pinBookmarksByDefault' }
|
||||
});
|
||||
|
||||
let bookmark;
|
||||
|
||||
let _body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
isPinned: (pinBookmarks && parseInt(pinBookmarks.value)),
|
||||
};
|
||||
|
||||
if (req.file) {
|
||||
_body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
bookmark = await Bookmark.create(_body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get all bookmarks
|
||||
// @route GET /api/bookmarks
|
||||
// @access Public
|
||||
exports.getBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
// Get config from database
|
||||
const useOrdering = await Config.findOne({
|
||||
where: { key: 'useOrdering' }
|
||||
});
|
||||
|
||||
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
||||
let bookmarks;
|
||||
|
||||
if (orderType == 'name') {
|
||||
bookmarks = await Bookmark.findAll({
|
||||
order: [[ Sequelize.fn('lower', Sequelize.col('name')), 'ASC' ]]
|
||||
});
|
||||
} else {
|
||||
bookmarks = await Bookmark.findAll({
|
||||
order: [[ orderType, 'ASC' ]]
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmarks,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get single bookmark
|
||||
// @route GET /api/bookmarks/:id
|
||||
// @access Public
|
||||
exports.getBookmark = asyncWrapper(async (req, res, next) => {
|
||||
const bookmark = await Bookmark.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!bookmark) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Bookmark with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update bookmark
|
||||
// @route PUT /api/bookmarks/:id
|
||||
// @access Public
|
||||
exports.updateBookmark = asyncWrapper(async (req, res, next) => {
|
||||
let bookmark = await Bookmark.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!bookmark) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Bookmark with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let _body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
};
|
||||
|
||||
if (req.file) {
|
||||
_body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
bookmark = await bookmark.update(_body);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Delete bookmark
|
||||
// @route DELETE /api/bookmarks/:id
|
||||
// @access Public
|
||||
exports.deleteBookmark = asyncWrapper(async (req, res, next) => {
|
||||
await Bookmark.destroy({
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {}
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Reorder bookmarks
|
||||
// @route PUT /api/bookmarks/0/reorder
|
||||
// @access Public
|
||||
exports.reorderBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
req.body.bookmarks.forEach(async ({ id, orderId }) => {
|
||||
await Bookmark.update({ orderId }, {
|
||||
where: { id }
|
||||
})
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
31
controllers/bookmarks/createBookmark.js
Normal file
31
controllers/bookmarks/createBookmark.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Create new bookmark
|
||||
// @route POST /api/bookmarks
|
||||
// @access Public
|
||||
const createBookmark = asyncWrapper(async (req, res, next) => {
|
||||
let bookmark;
|
||||
|
||||
let body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
};
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
bookmark = await Bookmark.create(body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = createBookmark;
|
18
controllers/bookmarks/deleteBookmark.js
Normal file
18
controllers/bookmarks/deleteBookmark.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Delete bookmark
|
||||
// @route DELETE /api/bookmarks/:id
|
||||
// @access Public
|
||||
const deleteBookmark = asyncWrapper(async (req, res, next) => {
|
||||
await Bookmark.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteBookmark;
|
31
controllers/bookmarks/getAllBookmarks.js
Normal file
31
controllers/bookmarks/getAllBookmarks.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get all bookmarks
|
||||
// @route GET /api/bookmarks
|
||||
// @access Public
|
||||
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
// bookmarks visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||
: [[orderType, 'ASC']];
|
||||
|
||||
const bookmarks = await Bookmark.findAll({
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmarks,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllBookmarks;
|
30
controllers/bookmarks/getSingleBookmark.js
Normal file
30
controllers/bookmarks/getSingleBookmark.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Get single bookmark
|
||||
// @route GET /api/bookmarks/:id
|
||||
// @access Public
|
||||
const getSingleBookmark = asyncWrapper(async (req, res, next) => {
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const bookmark = await Bookmark.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
});
|
||||
|
||||
if (!bookmark) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Bookmark with the id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleBookmark;
|
8
controllers/bookmarks/index.js
Normal file
8
controllers/bookmarks/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
createBookmark: require('./createBookmark'),
|
||||
getAllBookmarks: require('./getAllBookmarks'),
|
||||
getSingleBookmark: require('./getSingleBookmark'),
|
||||
updateBookmark: require('./updateBookmark'),
|
||||
deleteBookmark: require('./deleteBookmark'),
|
||||
reorderBookmarks: require('./reorderBookmarks'),
|
||||
};
|
23
controllers/bookmarks/reorderBookmarks.js
Normal file
23
controllers/bookmarks/reorderBookmarks.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Reorder bookmarks
|
||||
// @route PUT /api/bookmarks/0/reorder
|
||||
// @access Public
|
||||
const reorderBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
req.body.bookmarks.forEach(async ({ id, orderId }) => {
|
||||
await Bookmark.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderBookmarks;
|
43
controllers/bookmarks/updateBookmark.js
Normal file
43
controllers/bookmarks/updateBookmark.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Update bookmark
|
||||
// @route PUT /api/bookmarks/:id
|
||||
// @access Public
|
||||
const updateBookmark = asyncWrapper(async (req, res, next) => {
|
||||
let bookmark = await Bookmark.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!bookmark) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Bookmark with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
let body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
};
|
||||
|
||||
if (body.icon) {
|
||||
body.icon = body.icon.trim();
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
body.icon = req.file.filename;
|
||||
}
|
||||
|
||||
bookmark = await bookmark.update(body);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmark,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateBookmark;
|
22
controllers/categories/createCategory.js
Normal file
22
controllers/categories/createCategory.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Create new category
|
||||
// @route POST /api/categories
|
||||
// @access Public
|
||||
const createCategory = asyncWrapper(async (req, res, next) => {
|
||||
const { pinCategoriesByDefault: pinCategories } = await loadConfig();
|
||||
|
||||
const category = await Category.create({
|
||||
...req.body,
|
||||
isPinned: pinCategories,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = createCategory;
|
56
controllers/categories/deleteCategory.js
Normal file
56
controllers/categories/deleteCategory.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Delete category
|
||||
// @route DELETE /api/categories/:id
|
||||
// @access Public
|
||||
const deleteCategory = asyncWrapper(async (req, res, next) => {
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
category.apps.forEach(async (app) => {
|
||||
await App.destroy({
|
||||
where: { id: app.id },
|
||||
});
|
||||
});
|
||||
|
||||
category.bookmarks.forEach(async (bookmark) => {
|
||||
await Bookmark.destroy({
|
||||
where: { id: bookmark.id },
|
||||
});
|
||||
});
|
||||
|
||||
await Category.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteCategory;
|
66
controllers/categories/getAllCategories.js
Normal file
66
controllers/categories/getAllCategories.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get all categories
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
const getAllCategories = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
let categories;
|
||||
let output;
|
||||
|
||||
// categories visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC'],
|
||||
]
|
||||
: [
|
||||
[orderType, 'ASC'],
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC'],
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC'],
|
||||
];
|
||||
|
||||
categories = categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (req.isAuthenticated) {
|
||||
output = categories;
|
||||
} else {
|
||||
// filter out private apps/bookmarks
|
||||
output = categories.map((c) => c.get({ plain: true }));
|
||||
output = output.map((c) => ({
|
||||
...c,
|
||||
apps: c.apps.filter((b) => b.isPublic),
|
||||
bookmarks: c.bookmarks.filter((b) => b.isPublic),
|
||||
}));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: output,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllCategories;
|
60
controllers/categories/getSingleCategory.js
Normal file
60
controllers/categories/getSingleCategory.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get single category
|
||||
// @route GET /api/categories/:id
|
||||
// @access Public
|
||||
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']
|
||||
]
|
||||
: [
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC']
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']
|
||||
];
|
||||
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
where: visibility,
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
where: visibility,
|
||||
},
|
||||
],
|
||||
order,
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleCategory;
|
8
controllers/categories/index.js
Normal file
8
controllers/categories/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
createCategory: require('./createCategory'),
|
||||
getAllCategories: require('./getAllCategories'),
|
||||
getSingleCategory: require('./getSingleCategory'),
|
||||
updateCategory: require('./updateCategory'),
|
||||
deleteCategory: require('./deleteCategory'),
|
||||
reorderCategories: require('./reorderCategories'),
|
||||
};
|
23
controllers/categories/reorderCategories.js
Normal file
23
controllers/categories/reorderCategories.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
|
||||
// @desc Reorder categories
|
||||
// @route PUT /api/categories/0/reorder
|
||||
// @access Public
|
||||
const reorderCategories = asyncWrapper(async (req, res, next) => {
|
||||
req.body.categories.forEach(async ({ id, orderId }) => {
|
||||
await Category.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderCategories;
|
30
controllers/categories/updateCategory.js
Normal file
30
controllers/categories/updateCategory.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
|
||||
// @desc Update category
|
||||
// @route PUT /api/categories/:id
|
||||
// @access Public
|
||||
const updateCategory = asyncWrapper(async (req, res, next) => {
|
||||
let category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
category = await category.update({ ...req.body });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateCategory;
|
|
@ -1,235 +0,0 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Category = require('../models/Category');
|
||||
const App = require('../models/App');
|
||||
const Bookmark = require('../models/Bookmark');
|
||||
const Config = require('../models/Config');
|
||||
const { Sequelize } = require('sequelize');
|
||||
|
||||
exports.dockerDefaultCategory = {
|
||||
id: -2,
|
||||
name: 'Docker',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 998,
|
||||
};
|
||||
|
||||
exports.kubernetesDefaultCategory = {
|
||||
id: -3,
|
||||
name: 'Kubernetes',
|
||||
type: 'apps',
|
||||
isPinned: true,
|
||||
orderId: 999,
|
||||
};
|
||||
|
||||
const defaultCategories = [
|
||||
exports.dockerDefaultCategory,
|
||||
exports.kubernetesDefaultCategory,
|
||||
];
|
||||
|
||||
// @desc Create new category
|
||||
// @route POST /api/categories
|
||||
// @access Public
|
||||
exports.createCategory = asyncWrapper(async (req, res, next) => {
|
||||
// Get config from database
|
||||
const pinCategories = await Config.findOne({
|
||||
where: { key: 'pinCategoriesByDefault' },
|
||||
});
|
||||
|
||||
let category;
|
||||
|
||||
if (pinCategories) {
|
||||
if (parseInt(pinCategories.value)) {
|
||||
category = await Category.create({
|
||||
...req.body,
|
||||
isPinned: true,
|
||||
});
|
||||
} else {
|
||||
category = await Category.create(req.body);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get all categories
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
exports.getCategories = asyncWrapper(async (req, res, next) => {
|
||||
// Get config from database
|
||||
const useOrdering = await Config.findOne({
|
||||
where: { key: 'useOrdering' },
|
||||
});
|
||||
|
||||
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
||||
let categories;
|
||||
|
||||
const query = {
|
||||
include: [],
|
||||
order: [],
|
||||
};
|
||||
const requestedTypes = [];
|
||||
if (!req.params.type || req.params.type === 'apps') {
|
||||
query.include.push({ model: App, as: 'apps' });
|
||||
requestedTypes.push('apps');
|
||||
}
|
||||
if (!req.params.type || req.params.type === 'bookmarks') {
|
||||
query.include.push({ model: Bookmark, as: 'bookmarks' });
|
||||
requestedTypes.push('bookmarks');
|
||||
}
|
||||
query.where = { type: requestedTypes };
|
||||
|
||||
if (orderType == 'name') {
|
||||
query.order.push([
|
||||
Sequelize.fn('lower', Sequelize.col('Category.name')),
|
||||
'ASC',
|
||||
]);
|
||||
} else {
|
||||
query.order.push([orderType, 'ASC']);
|
||||
}
|
||||
|
||||
categories = await Category.findAll(query);
|
||||
defaultCategories
|
||||
.filter((category) => requestedTypes.indexOf(category.type) > -1)
|
||||
.forEach((category) => categories.push(category));
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: categories,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get single category
|
||||
// @route GET /api/categories/:id
|
||||
// @access Public
|
||||
exports.getCategory = asyncWrapper(async (req, res, next) => {
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
if (req.params.id === exports.dockerDefaultCategory.id) {
|
||||
category = exports.dockerDefaultCategory;
|
||||
} else if (req.params.id === exports.kubernetesDefaultCategory.id) {
|
||||
category = exports.kubernetesDefaultCategory;
|
||||
} else {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update category
|
||||
// @route PUT /api/categories/:id
|
||||
// @access Public
|
||||
exports.updateCategory = asyncWrapper(async (req, res, next) => {
|
||||
let category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
category = await category.update({ ...req.body });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Delete category
|
||||
// @route DELETE /api/categories/:id
|
||||
// @access Public
|
||||
exports.deleteCategory = asyncWrapper(async (req, res, next) => {
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
category.apps.forEach(async (app) => {
|
||||
await App.destroy({
|
||||
where: { id: app.id },
|
||||
});
|
||||
});
|
||||
|
||||
category.bookmarks.forEach(async (bookmark) => {
|
||||
await Bookmark.destroy({
|
||||
where: { id: bookmark.id },
|
||||
});
|
||||
});
|
||||
|
||||
await Category.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Reorder categories
|
||||
// @route PUT /api/categories/0/reorder
|
||||
// @access Public
|
||||
exports.reorderCategories = asyncWrapper(async (req, res, next) => {
|
||||
req.body.categories.forEach(async ({ id, orderId }) => {
|
||||
await Category.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
|
@ -1,177 +0,0 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Config = require('../models/Config');
|
||||
const { Op } = require('sequelize');
|
||||
const File = require('../utils/File');
|
||||
const { join } = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// @desc Insert new key:value pair
|
||||
// @route POST /api/config
|
||||
// @access Public
|
||||
exports.createPair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.create(req.body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: pair,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get all key:value pairs
|
||||
// @route GET /api/config
|
||||
// @route GET /api/config?keys=foo,bar,baz
|
||||
// @access Public
|
||||
exports.getAllPairs = asyncWrapper(async (req, res, next) => {
|
||||
let pairs;
|
||||
|
||||
if (req.query.keys) {
|
||||
// Check for specific keys to get in a single query
|
||||
const keys = req.query.keys.split(',').map((key) => {
|
||||
return { key };
|
||||
});
|
||||
|
||||
pairs = await Config.findAll({
|
||||
where: {
|
||||
[Op.or]: keys,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Else get all
|
||||
pairs = await Config.findAll();
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: pairs,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get single key:value pair
|
||||
// @route GET /api/config/:key
|
||||
// @access Public
|
||||
exports.getSinglePair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.findOne({
|
||||
where: { key: req.params.key },
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: pair,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update value
|
||||
// @route PUT /api/config/:key
|
||||
// @access Public
|
||||
exports.updateValue = asyncWrapper(async (req, res, next) => {
|
||||
let pair = await Config.findOne({
|
||||
where: { key: req.params.key },
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
if (pair.isLocked) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Value of key ${req.params.key} is locked and can not be changed`,
|
||||
400
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
pair = await pair.update({ ...req.body });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: pair,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update multiple values
|
||||
// @route PUT /api/config/
|
||||
// @access Public
|
||||
exports.updateValues = asyncWrapper(async (req, res, next) => {
|
||||
Object.entries(req.body).forEach(async ([key, value]) => {
|
||||
await Config.update(
|
||||
{ value },
|
||||
{
|
||||
where: { key },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
const config = await Config.findAll();
|
||||
|
||||
res.status(200).send({
|
||||
success: true,
|
||||
data: config,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Delete key:value pair
|
||||
// @route DELETE /api/config/:key
|
||||
// @access Public
|
||||
exports.deletePair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.findOne({
|
||||
where: { key: req.params.key },
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
if (pair.isLocked) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Value of key ${req.params.key} is locked and can not be deleted`,
|
||||
400
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
await pair.destroy();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get custom CSS file
|
||||
// @route GET /api/config/0/css
|
||||
// @access Public
|
||||
exports.getCss = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(join(__dirname, '../public/flame.css'));
|
||||
const content = file.read();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update custom CSS file
|
||||
// @route PUT /api/config/0/css
|
||||
// @access Public
|
||||
exports.updateCss = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(join(__dirname, '../public/flame.css'));
|
||||
file.write(req.body.styles, false);
|
||||
|
||||
// Copy file to docker volume
|
||||
fs.copyFileSync(
|
||||
join(__dirname, '../public/flame.css'),
|
||||
join(__dirname, '../data/flame.css')
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
18
controllers/config/getCSS.js
Normal file
18
controllers/config/getCSS.js
Normal file
|
@ -0,0 +1,18 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
const { join } = require('path');
|
||||
|
||||
// @desc Get custom CSS file
|
||||
// @route GET /api/config/0/css
|
||||
// @access Public
|
||||
const getCSS = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(join(__dirname, '../../public/flame.css'));
|
||||
const content = file.read();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getCSS;
|
16
controllers/config/getConfig.js
Normal file
16
controllers/config/getConfig.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get config
|
||||
// @route GET /api/config
|
||||
// @access Public
|
||||
const getConfig = asyncWrapper(async (req, res, next) => {
|
||||
const config = await loadConfig();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: config,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getConfig;
|
6
controllers/config/index.js
Normal file
6
controllers/config/index.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
module.exports = {
|
||||
getCSS: require('./getCSS'),
|
||||
updateCSS: require('./updateCSS'),
|
||||
getConfig: require('./getConfig'),
|
||||
updateConfig: require('./updateConfig'),
|
||||
};
|
25
controllers/config/updateCSS.js
Normal file
25
controllers/config/updateCSS.js
Normal file
|
@ -0,0 +1,25 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
const { join } = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
// @desc Update custom CSS file
|
||||
// @route PUT /api/config/0/css
|
||||
// @access Public
|
||||
const updateCSS = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(join(__dirname, '../../public/flame.css'));
|
||||
file.write(req.body.styles, false);
|
||||
|
||||
// Copy file to docker volume
|
||||
fs.copyFileSync(
|
||||
join(__dirname, '../../public/flame.css'),
|
||||
join(__dirname, '../../data/flame.css')
|
||||
);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateCSS;
|
24
controllers/config/updateConfig.js
Normal file
24
controllers/config/updateConfig.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
const { writeFile } = require('fs/promises');
|
||||
|
||||
// @desc Update config
|
||||
// @route PUT /api/config/
|
||||
// @access Public
|
||||
const updateConfig = asyncWrapper(async (req, res, next) => {
|
||||
const existingConfig = await loadConfig();
|
||||
|
||||
const newConfig = {
|
||||
...existingConfig,
|
||||
...req.body,
|
||||
};
|
||||
|
||||
await writeFile('data/config.json', JSON.stringify(newConfig));
|
||||
|
||||
res.status(200).send({
|
||||
success: true,
|
||||
data: newConfig,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateConfig;
|
21
controllers/queries/addQuery.js
Normal file
21
controllers/queries/addQuery.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Add custom search query
|
||||
// @route POST /api/queries
|
||||
// @access Public
|
||||
const addQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File('data/customQueries.json');
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
// Add new query
|
||||
content.queries.push(req.body);
|
||||
file.write(content, true);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: req.body,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = addQuery;
|
22
controllers/queries/deleteQuery.js
Normal file
22
controllers/queries/deleteQuery.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Delete query
|
||||
// @route DELETE /api/queries/:prefix
|
||||
// @access Public
|
||||
const deleteQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File('data/customQueries.json');
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
content.queries = content.queries.filter(
|
||||
(q) => q.prefix != req.params.prefix
|
||||
);
|
||||
file.write(content, true);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteQuery;
|
17
controllers/queries/getQueries.js
Normal file
17
controllers/queries/getQueries.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Get custom queries file
|
||||
// @route GET /api/queries
|
||||
// @access Public
|
||||
const getQueries = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File('data/customQueries.json');
|
||||
const content = JSON.parse(file.read());
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getQueries;
|
|
@ -1,81 +1,6 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
const { join } = require('path');
|
||||
|
||||
const QUERIES_PATH = join(__dirname, '../../data/customQueries.json');
|
||||
|
||||
// @desc Add custom search query
|
||||
// @route POST /api/queries
|
||||
// @access Public
|
||||
exports.addQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(QUERIES_PATH);
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
// Add new query
|
||||
content.queries.push(req.body);
|
||||
file.write(content, true);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: req.body,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Get custom queries file
|
||||
// @route GET /api/queries
|
||||
// @access Public
|
||||
exports.getQueries = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(QUERIES_PATH);
|
||||
const content = JSON.parse(file.read());
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update query
|
||||
// @route PUT /api/queries/:prefix
|
||||
// @access Public
|
||||
exports.updateQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(QUERIES_PATH);
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
let queryIdx = content.queries.findIndex(
|
||||
(q) => q.prefix == req.params.prefix
|
||||
);
|
||||
|
||||
// query found
|
||||
if (queryIdx > -1) {
|
||||
content.queries = [
|
||||
...content.queries.slice(0, queryIdx),
|
||||
req.body,
|
||||
...content.queries.slice(queryIdx + 1),
|
||||
];
|
||||
}
|
||||
|
||||
file.write(content, true);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Delete query
|
||||
// @route DELETE /api/queries/:prefix
|
||||
// @access Public
|
||||
exports.deleteQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File(QUERIES_PATH);
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
content.queries = content.queries.filter(
|
||||
(q) => q.prefix != req.params.prefix
|
||||
);
|
||||
file.write(content, true);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
module.exports = {
|
||||
addQuery: require('./addQuery'),
|
||||
getQueries: require('./getQueries'),
|
||||
updateQuery: require('./updateQuery'),
|
||||
deleteQuery: require('./deleteQuery'),
|
||||
};
|
||||
|
|
32
controllers/queries/updateQuery.js
Normal file
32
controllers/queries/updateQuery.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Update query
|
||||
// @route PUT /api/queries/:prefix
|
||||
// @access Public
|
||||
const updateQuery = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File('data/customQueries.json');
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
let queryIdx = content.queries.findIndex(
|
||||
(q) => q.prefix == req.params.prefix
|
||||
);
|
||||
|
||||
// query found
|
||||
if (queryIdx > -1) {
|
||||
content.queries = [
|
||||
...content.queries.slice(0, queryIdx),
|
||||
req.body,
|
||||
...content.queries.slice(queryIdx + 1),
|
||||
];
|
||||
}
|
||||
|
||||
file.write(content, true);
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: content.queries,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateQuery;
|
|
@ -1,31 +0,0 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Weather = require('../models/Weather');
|
||||
const getExternalWeather = require('../utils/getExternalWeather');
|
||||
|
||||
// @desc Get latest weather status
|
||||
// @route GET /api/weather
|
||||
// @access Public
|
||||
exports.getWeather = asyncWrapper(async (req, res, next) => {
|
||||
const weather = await Weather.findAll({
|
||||
order: [['createdAt', 'DESC']],
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: weather,
|
||||
});
|
||||
});
|
||||
|
||||
// @desc Update weather
|
||||
// @route GET /api/weather/update
|
||||
// @access Public
|
||||
exports.updateWeather = asyncWrapper(async (req, res, next) => {
|
||||
const weather = await getExternalWeather();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: weather,
|
||||
});
|
||||
});
|
19
controllers/weather/getWather.js
Normal file
19
controllers/weather/getWather.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Weather = require('../../models/Weather');
|
||||
|
||||
// @desc Get latest weather status
|
||||
// @route GET /api/weather
|
||||
// @access Public
|
||||
const getWeather = asyncWrapper(async (req, res, next) => {
|
||||
const weather = await Weather.findAll({
|
||||
order: [['createdAt', 'DESC']],
|
||||
limit: 1,
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: weather,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getWeather;
|
4
controllers/weather/index.js
Normal file
4
controllers/weather/index.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
getWeather: require('./getWather'),
|
||||
updateWeather: require('./updateWeather'),
|
||||
};
|
16
controllers/weather/updateWeather.js
Normal file
16
controllers/weather/updateWeather.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const getExternalWeather = require('../../utils/getExternalWeather');
|
||||
|
||||
// @desc Update weather
|
||||
// @route GET /api/weather/update
|
||||
// @access Public
|
||||
const updateWeather = asyncWrapper(async (req, res, next) => {
|
||||
const weather = await getExternalWeather();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: weather,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateWeather;
|
Loading…
Add table
Add a link
Reference in a new issue