diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3669f86..6a6f557 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,10 +52,17 @@ jobs: - name: Build Package run: | cd server + + echo '{}' > test.json + zip res.zip test.json + npx pkg . -t node18-x64-windows mkdir -p out/web - cp -r web/* out/web/ mkdir -p out/saves + cp config.json out/ + cp -r web/* out/web/ + cp -r res.zip out/saves/ + cd .. - uses: actions/upload-artifact@v3 diff --git a/package.json b/package.json index 6738033..bc1bada 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "maa-json-viewer", - "version": "1.0.0", + "version": "1.0.1", "private": true, "scripts": { "dev": "vite", diff --git a/server/config.json b/server/config.json new file mode 100644 index 0000000..80535e2 --- /dev/null +++ b/server/config.json @@ -0,0 +1,7 @@ +{ + "port": 8080, + "web": "./web", + "date": "yyyyMMdd-HHmmss", + "saves": "./saves/{date}.zip", + "active": "./saves/res.zip" +} diff --git a/server/package-lock.json b/server/package-lock.json index 40159dd..e1cee1b 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -13,6 +13,7 @@ "@types/express": "^4.17.17", "@types/multer": "^1.4.7", "@types/node": "^18.17.2", + "date-fns": "^2.30.0", "esbuild": "^0.18.17", "express": "^4.18.2", "multer": "^1.4.5-lts.1", @@ -64,6 +65,18 @@ "node": ">=6.0.0" } }, + "node_modules/@babel/runtime": { + "version": "7.22.6", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.22.6.tgz", + "integrity": "sha512-wDb5pWm4WDdF6LFUde3Jl8WzPA+3ZbxYqkC6xAXuD3irdEHN1k0NfTRrJD8ZD378SJ61miMLCqIOXYhd8x+AJQ==", + "dev": true, + "dependencies": { + "regenerator-runtime": "^0.13.11" + }, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/@babel/types": { "version": "7.19.0", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.19.0.tgz", @@ -983,6 +996,22 @@ "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", "dev": true }, + "node_modules/date-fns": { + "version": "2.30.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.30.0.tgz", + "integrity": "sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==", + "dev": true, + "dependencies": { + "@babel/runtime": "^7.21.0" + }, + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -2172,6 +2201,12 @@ "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", "dev": true }, + "node_modules/regenerator-runtime": { + "version": "0.13.11", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz", + "integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==", + "dev": true + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/server/package.json b/server/package.json index fd8f21a..5a3fdb6 100644 --- a/server/package.json +++ b/server/package.json @@ -9,6 +9,7 @@ "@types/express": "^4.17.17", "@types/multer": "^1.4.7", "@types/node": "^18.17.2", + "date-fns": "^2.30.0", "esbuild": "^0.18.17", "express": "^4.18.2", "multer": "^1.4.5-lts.1", diff --git a/server/src/index.ts b/server/src/index.ts index 85d61a7..b39abb4 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,43 +2,56 @@ import express, { json } from 'express' import multer, { memoryStorage } from 'multer' import path from 'path' import fs from 'fs/promises' +import { format } from 'date-fns' + +async function main() { + const config = JSON.parse(await fs.readFile('config.json', 'utf-8')) as { + port: number + web: string + date: string + saves: string + active: string + } -const app = express() - -const webDir = path.resolve('./web') -const port = 8080 - -app.use(express.static(webDir)) -app.use(json()) - -let activeZip = './saves/res.zip' - -app.post('/api/zip', async (req, res) => { - res.sendFile(path.resolve(activeZip)) -}) - -const cache = memoryStorage() -const multerInst = multer({ - storage: cache -}) - -app.post('/api/save', multerInst.single('file'), async (req, res) => { - const buffer = req.file?.buffer - if (buffer) { - const newFileName = `./saves/${Date.now()}.zip` - fs.writeFile(newFileName, buffer).then(() => { - activeZip = newFileName + const app = express() + + app.use(express.static(config.web)) + app.use(json()) + + app.post('/api/load', async (req, res) => { + res.sendFile(path.resolve(config.active)) + }) + + const cache = memoryStorage() + const multerInst = multer({ + storage: cache + }) + + app.post('/api/save', multerInst.single('file'), async (req, res) => { + const buffer = req.file?.buffer + if (buffer) { + const newFileName = config.saves.replaceAll( + '{date}', + format(new Date(), config.date) + ) + await fs.mkdir(path.dirname(newFileName), { recursive: true }) + await fs.writeFile(newFileName, buffer) + console.log('saved to', newFileName) + config.active = newFileName + fs.writeFile('config.json', JSON.stringify(config, null, 2)) res.send({ success: true }) - }) - } else { - res.send({ - success: false - }) - } -}) + } else { + res.send({ + success: false + }) + } + }) + + app.listen(config.port, () => { + console.log(`server started: http://localhost:${config.port}/`) + }) +} -app.listen(port, () => { - console.log(`server started: http://localhost:${port}/`) -}) +main() diff --git a/src/loader.ts b/src/loader.ts index 56b37fa..f1f6ab2 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -4,7 +4,7 @@ import { fs } from './data/fs' async function loadZip() { const res: ArrayBuffer = ( - await axios.post('/api/zip', null, { + await axios.post('/api/load', null, { responseType: 'arraybuffer' }) ).data