Made general rules for processing json db

This commit is contained in:
Kawanaao 2023-12-05 08:12:59 +02:00
parent c558565b11
commit 90a8766b15
No known key found for this signature in database
GPG key ID: 5B7A8DDCE861E7DC
10 changed files with 1809 additions and 1745 deletions

View file

@ -0,0 +1,7 @@
{
"POST": {
"constellation/devices": {},
"constellation/connect": {},
"constellation/block": {}
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,11 +1,87 @@
import { Request, Response } from "express"
import { resolve } from "path"
import { readFileSync } from "fs"
import jsonServer from "json-server"
import traverse from "traverse"
const PROXY_PORT = process.env.PROXY_PORT || 9000
const proxyServer = jsonServer.create()
const router = jsonServer.router("db.json")
const middlewares = jsonServer.defaults()
const definedDB: Record<string, any> = JSON.parse(readFileSync(resolve(__dirname, "db.json"), {
encoding: "utf8"
}))
const autoDB = traverse(definedDB)
.paths()
.map(pathArray => ({
path: pathArray.join("."),
data: pathArray.reduce((db, pathSegment) => db[pathSegment], definedDB)
}))
.map(dbEntry => (dbEntry.data = isObject(dbEntry.data) ? dbEntry.data : ["$return", dbEntry.data], dbEntry))
.reduce((acc, dbEntry) => (acc[dbEntry.path] = dbEntry.data, acc), {} as Record<string, any>)
const db = Object.assign({}, autoDB, definedDB)
const dbPaths = Object.keys(db)
// TODO: Reference types do not work at the ["$return", data] level
// A possible solution is to replace filter with map and custom behavior for get, post and put
// defining them to interact with the parent object
const definedRoutes: Record<string, string> = JSON.parse(readFileSync(resolve(__dirname, "routes.json"), {
encoding: "utf8"
}))
const autoRoutes = dbPaths
.filter(path => path.includes("."))
.map(path => ({
origin: "/" + path.replace(/\./g, "/"),
destination: "/" + path
}))
.reduce((acc, bind) => (acc[bind.origin] = bind.destination, acc), {} as Record<string, string>)
const routes = Object.assign({}, autoRoutes, definedRoutes)
const proxyServer = jsonServer.create()
const middlewares = jsonServer.defaults()
const rewriter = jsonServer.rewriter(routes)
const router = jsonServer.router(db, {
foreignKeySuffix: "Id"
})
// @ts-ignore
router.render = (req: Request, res: Response) => {
const reqPath = req.path.replace("/", "")
const matchedPath = dbPaths
.map(path => path.replace(/\./g, "/"))
.find(path => path !== reqPath && reqPath.startsWith(path))
console.log(matchedPath)
if (typeof res.locals.data["_comment"] === "string" && res.locals.data["_comment"].toLowerCase().includes("todo"))
return res.sendStatus(404)
else if (Array.isArray(res.locals.data.list))
res.locals.data = res.locals.data.list;
else if (res.locals.data[0] === "$return")
res.locals.data = res.locals.data[1]
switch (req.method) {
case "GET":
res.locals.result = res.locals.data
break
case "PUT": case "POST": default:
res.locals.result = req.body
break
}
return res.jsonp({
data: res.locals.result,
status: "ok"
})
}
proxyServer.use(jsonServer.bodyParser)
proxyServer.use(middlewares)
proxyServer.use("/api", rewriter)
proxyServer.use("/api", router)
proxyServer.listen(PROXY_PORT, () => console.info(`JSON proxy is running on ${PROXY_PORT} port`))
proxyServer.listen(PROXY_PORT, () => console.info(`JSON proxy is running on ${PROXY_PORT} port`))
function isObject(value: unknown) {
return typeof value === 'object' && value !== null
}

View file

@ -1,10 +1,9 @@
{
"/servapps/:container": "/servapps?data.Id=:container",
"/servapps/:container/logs": "/servapps/demo/logs",
"/volume/:name": "/volumes?data.Volumes.Name=:name",
"/network/:name": "/networks?data.Name=:name",
"/volume/:name": "/volumes/Volumes?Volumes.Name=:name",
"/network/:name": "/networks?Name=:name",
"/servapps/:id/secure/:res": "/servapps/demo/secure",
"/servapps/:id/manage/:action": "/servapps/demo/manage",
"/servapps/:id/update": "/servapps/:id",
"/servapps/:id/networks": "/servapps/:id?_embed=data.NetworkSettings.Networks"
"/servapps/:id/networks": "/servapps/:id?_embed=NetworkSettings.Networks",
"/servapps/:containerId/network/:networkId": "/servapps/:containerId/networks"
}

40
package-lock.json generated
View file

@ -51,6 +51,7 @@
"simplebar": "^5.3.8",
"simplebar-react": "^2.4.1",
"timeago.js": "^4.0.2",
"traverse": "^0.6.7",
"web-vitals": "^3.0.2",
"whiskers": "^0.4.0",
"yup": "^0.32.11"
@ -72,6 +73,7 @@
"@types/lodash.merge": "^4.6.9",
"@types/node": "^20.10.2",
"@types/react-color": "^3.0.10",
"@types/traverse": "^0.6.35",
"@types/webpack-bundle-analyzer": "^4.6.3",
"babel-loader": "^9.1.3",
"babel-plugin-direct-import": "^1.0.0",
@ -3726,6 +3728,12 @@
"@types/node": "*"
}
},
"node_modules/@types/traverse": {
"version": "0.6.35",
"resolved": "https://registry.npmjs.org/@types/traverse/-/traverse-0.6.35.tgz",
"integrity": "sha512-ZZBG4X4CTVqKLpPvDtqxyCO7VsVAfOsvbJAdbM90uYrXRv27fAjbV8eVUtdBLivC6x6Gcx6n6Kvjbai3IX1rWw==",
"dev": true
},
"node_modules/@types/unist": {
"version": "2.0.10",
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.10.tgz",
@ -4069,12 +4077,6 @@
"url": "https://github.com/sponsors/epoberezkin"
}
},
"node_modules/ajv-formats/node_modules/json-schema-traverse": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
"dev": true
},
"node_modules/ajv-keywords": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-3.5.2.tgz",
@ -4084,6 +4086,12 @@
"ajv": "^6.9.1"
}
},
"node_modules/ajv/node_modules/json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
"dev": true
},
"node_modules/ansi-html-community": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/ansi-html-community/-/ansi-html-community-0.0.8.tgz",
@ -11187,9 +11195,9 @@
}
},
"node_modules/json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
"dev": true
},
"node_modules/json-server": {
@ -14777,12 +14785,6 @@
"ajv": "^8.8.2"
}
},
"node_modules/schema-utils/node_modules/json-schema-traverse": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz",
"integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==",
"dev": true
},
"node_modules/seek-bzip": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/seek-bzip/-/seek-bzip-1.0.6.tgz",
@ -16152,6 +16154,14 @@
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
"node_modules/traverse": {
"version": "0.6.7",
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.7.tgz",
"integrity": "sha512-/y956gpUo9ZNCb99YjxG7OaslxZWHfCHAUUfshwqOXmxUIvqLjVO581BT+gM59+QV9tFe6/CGG53tsA1Y7RSdg==",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/trim-newlines": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/trim-newlines/-/trim-newlines-1.0.0.tgz",

View file

@ -51,6 +51,7 @@
"simplebar": "^5.3.8",
"simplebar-react": "^2.4.1",
"timeago.js": "^4.0.2",
"traverse": "^0.6.7",
"web-vitals": "^3.0.2",
"whiskers": "^0.4.0",
"yup": "^0.32.11"
@ -83,9 +84,9 @@
],
"scripts": {
"client": "vite",
"webpack:setup-env": "cross-env withReport=$npm_config_with_report analyzeDeps=$npm_config_analyze_deps",
"webpack:setup-env": "cross-env useProduction=$npm_config_use_production withReport=$npm_config_with_report analyzeDeps=$npm_config_analyze_deps",
"webpack:build": "npm run webpack:setup-env -- webpack --config webpack.prod.js --progress",
"webpack:start": "npm run webpack:setup-env -- webpack serve --open --config webpack.dev.js",
"webpack:serve": "npm run webpack:setup-env -- webpack serve --open --config webpack.dev.js",
"start": "env COSMOS_CONFIG_FOLDER=/mnt/e/work/Cosmos-Server/zz_test_config/ CONFIG_FILE=./config_dev.json EZ=UTC ACME_STAGING=true build/cosmos",
"build": "sh build.sh",
"dev": "npm run build && npm run start",
@ -124,6 +125,7 @@
"@types/lodash.merge": "^4.6.9",
"@types/node": "^20.10.2",
"@types/react-color": "^3.0.10",
"@types/traverse": "^0.6.35",
"@types/webpack-bundle-analyzer": "^4.6.3",
"babel-loader": "^9.1.3",
"babel-plugin-direct-import": "^1.0.0",

View file

@ -17,6 +17,11 @@
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
},
"include": ["client/src/**/*"],
"exclude": ["client/**/*.demo*"]
}

View file

@ -3,6 +3,7 @@ const { DuplicatesPlugin } = require("inspectpack/plugin")
const { join } = require("path")
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const withReport = process.env.withReport ? true : false
const analyzeDeps = process.env.analyzeDeps ? true : false

View file

@ -2,10 +2,11 @@ const { DefinePlugin } = require("webpack")
const { merge } = require("webpack-merge")
const { resolve } = require("path")
const webpackCommon = require("./webpack.common.js")
const webpackProd = require("./webpack.prod.js")
module.exports = merge(webpackCommon, {
module.exports = merge(process.env.useProduction ? webpackProd : webpackCommon, {
mode: "development",
devtool: "inline-source-map",
devtool: !process.env.useProduction ? "inline-source-map" : undefined,
target: "web",
devServer: {
port: 3000,

View file

@ -28,11 +28,6 @@ const cacheGroups = {
module.exports = merge(webpackCommon, {
mode: "production",
plugins: [
new IgnorePlugin({
resourceRegExp: /[\d\D]*.demo[\d\D]*/
})
],
optimization: {
chunkIds: 'total-size',
moduleIds: 'size',
@ -53,10 +48,10 @@ module.exports = merge(webpackCommon, {
terserOptions: {
mangle: true,
compress: true,
sourceMap: false,
format: {
comments: false
},
sourceMap: false
}
}
}),
new ImageMinimizerPlugin({