2023-11-25 11:56:14 +03:00
|
|
|
const express = require('express')
|
2023-11-25 11:55:44 +03:00
|
|
|
const router = express.Router()
|
2023-11-25 11:56:14 +03:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const BASE_PATH = __dirname;
|
|
|
|
const STATIC_PATH = `${BASE_PATH}/static`;
|
|
|
|
// Serve static files
|
|
|
|
router.use(express.static(path.join(__dirname, './assets/')))
|
|
|
|
// Add the required directories
|
|
|
|
router.use((req, res, next) => {
|
|
|
|
const directories = ['/static', '/profiles']
|
|
|
|
directories.forEach((dir) => {
|
|
|
|
if (!fs.existsSync(BASE_PATH + dir)) {
|
|
|
|
fs.mkdirSync(BASE_PATH + dir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
// Serve Static generated SVGs
|
|
|
|
router.get('/static/:name', async (req, res, next) => {
|
|
|
|
const fileName = req.params.name
|
|
|
|
const filePath = `${STATIC_PATH}/${fileName}`
|
|
|
|
|
|
|
|
const file = await fs.readFileSync(filePath)
|
|
|
|
res.setHeader('Content-Type', 'image/svg+xml')
|
|
|
|
res.send(file)
|
|
|
|
})
|
|
|
|
router.use('/api', require('./routes/api').default)
|
2023-11-25 11:55:44 +03:00
|
|
|
|
|
|
|
router.get('/info', (req, res) => {
|
2023-11-25 11:56:14 +03:00
|
|
|
res.send('Pen-Plotter backend')
|
2023-11-25 11:55:44 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
module.exports = router
|