generated from azures04/Base-REST-API
Compare commits
5 Commits
96d40456d5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 71fa04272c | |||
| 7e9496b35d | |||
| 5eaca1e948 | |||
| 1ddcf1eaf2 | |||
| a40a6d6ad8 |
@@ -1,2 +1,4 @@
|
||||
WEB_PORT=3000
|
||||
IS_PROD=FALSE
|
||||
IS_PROD=FALSE
|
||||
INSTANCE_NAME="Azures04"
|
||||
INSTANCE_LOGO="https://chibieditor.fr/assets/img/avatars/azures.png"
|
||||
14
routes/index.js
Normal file
14
routes/index.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const express = require("express")
|
||||
const metaService = require("../services/metaService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get(/.*/, async (req, res, next) => {
|
||||
try {
|
||||
const file = metaService.serveStaticFile(req.originalUrl)
|
||||
return res.status(200).sendFile(file)
|
||||
} catch (error) {
|
||||
next()
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
9
routes/metadata.js
Normal file
9
routes/metadata.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const express = require("express")
|
||||
const router = express.Router()
|
||||
const metaService = require("../services/metaService")
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
return res.status(200).json(metaService.getServerMetadata())
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,12 @@
|
||||
const express = require("express")
|
||||
const productsService = require("../../../../services/productsService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const { productName, productPlatform } = req.params
|
||||
await productsService.canAccess(productName)
|
||||
const configs = await productsService.getInstallerConfig(productName, productPlatform)
|
||||
return res.status(200).json(configs)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -1,11 +1,11 @@
|
||||
const express = require("express")
|
||||
const productsFileService = require("../../../../services/productsFileService")
|
||||
const productsService = require("../../../../services/productsService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get(/.*/, async (req, res, next) => {
|
||||
const { productName, productPlatform } = req.params
|
||||
await productsFileService.canAccess(productName)
|
||||
const file = await productsFileService.getFile(req.url, productName, productPlatform)
|
||||
await productsService.canAccess(productName)
|
||||
const file = await productsService.getFile(req.url, productName, productPlatform)
|
||||
return res.status(200).download(file.path)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
const express = require("express")
|
||||
const productsFileService = require("../../../../services/productsFileService")
|
||||
const productsService = require("../../../../services/productsService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const { productName, productPlatform } = req.params
|
||||
await productsFileService.canAccess(productName)
|
||||
const files = await productsFileService.getProductFiles(productName, productPlatform)
|
||||
await productsService.canAccess(productName)
|
||||
const files = await productsService.getProductFiles(productName, productPlatform)
|
||||
return res.status(200).json(files)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
const express = require("express")
|
||||
const productsFileService = require("../../../../services/productsFileService")
|
||||
const productsService = require("../../../../services/productsService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const { productName, productPlatform } = req.params
|
||||
await productsFileService.canAccess(productName)
|
||||
const files = await productsFileService.getRequirementsForPlatform(productName, productPlatform)
|
||||
return res.status(200).json(files)
|
||||
await productsService.canAccess(productName)
|
||||
const requirements = await productsService.getRequirementsForPlatform(productName, productPlatform)
|
||||
return res.status(200).json(requirements)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@@ -1,11 +1,11 @@
|
||||
const express = require("express")
|
||||
const productsFileService = require("../../../services/productsFileService")
|
||||
const productsService = require("../../../services/productsService")
|
||||
const router = express.Router({ mergeParams: true })
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const { productName } = req.params
|
||||
await productsFileService.canAccess(productName)
|
||||
const files = await productsFileService.getInstallerResources(productName)
|
||||
await productsService.canAccess(productName)
|
||||
const files = await productsService.getInstallerResources(productName)
|
||||
return res.status(200).json(files)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
const express = require("express")
|
||||
const router = express.Router({ mergeParams: false })
|
||||
const productsFileService = require("../../services/productsFileService")
|
||||
const productsService = require("../../services/productsService")
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const products = await productsFileService.getProducts()
|
||||
const products = await productsService.getProducts()
|
||||
return res.status(200).json(products)
|
||||
})
|
||||
|
||||
router.get("/product/:productName", async (req, res) => {
|
||||
const product = await productsFileService.getProduct(req.params.productName)
|
||||
const product = await productsService.getProduct(req.params.productName)
|
||||
return res.status(200).json(product)
|
||||
})
|
||||
|
||||
|
||||
23
services/metaService.js
Normal file
23
services/metaService.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
function getServerMetadata() {
|
||||
return {
|
||||
name: process.env["INSTANCE_NAME"],
|
||||
logo: process.env["INSTANCE_LOGO"]
|
||||
}
|
||||
}
|
||||
|
||||
function serveStaticFile(filePath) {
|
||||
const finalFilePath = path.join(process.cwd(), "data", "static", filePath)
|
||||
if (fs.existsSync(finalFilePath)) {
|
||||
return finalFilePath
|
||||
}
|
||||
throw new DefaultError(404, "File not found", "File moved/deleted or don't exists", "NotFoundException")
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
serveStaticFile,
|
||||
getServerMetadata
|
||||
}
|
||||
@@ -144,6 +144,18 @@ async function getInstallerResources(productName) {
|
||||
return config["installerResources"]
|
||||
}
|
||||
|
||||
async function getInstallerConfig(productName, productPlatform) {
|
||||
const fixedPath = path.join(productsDataPath, productName, "files", ".brikcfg")
|
||||
if (!(await fs.promises.exists(fixedPath))) return []
|
||||
|
||||
const config = JSON.parse(await fs.promises.readFile(fixedPath, "utf8"))
|
||||
|
||||
const globalReqs = config.configurations?.global || []
|
||||
const platformReqs = config.configurations?.[productPlatform] || []
|
||||
|
||||
return { ...globalReqs, ...platformReqs }
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFile,
|
||||
canAccess,
|
||||
@@ -151,6 +163,7 @@ module.exports = {
|
||||
getFileSha1,
|
||||
getProducts,
|
||||
getProductFiles,
|
||||
getInstallerConfig,
|
||||
getInstallerResources,
|
||||
getRequirementsForPlatform
|
||||
}
|
||||
Reference in New Issue
Block a user