diff --git a/backend/routers/setup.ts b/backend/routers/setup.ts new file mode 100644 index 0000000..d21919f --- /dev/null +++ b/backend/routers/setup.ts @@ -0,0 +1,34 @@ +import fs from 'fs-extra'; +import { path } from '@tycrek/joint'; +import { Router, json as BodyParserJson } from 'express'; +import { log } from '../log'; +import { UserConfiguration } from 'ass'; + +const router = Router({ caseSensitive: true }); +const userConfigExists = fs.pathExistsSync(path.join('userconfig.json')); + +// Static routes +router.get('/', (req, res) => userConfigExists ? res.redirect('/') : res.render('setup')); +router.get('/ui.js', (req, res) => userConfigExists ? res.send('') : res.type('text').sendFile(path.join('dist-frontend/setup.mjs'))); + +// Setup route +router.post('/', BodyParserJson(), (req, res) => { + if (userConfigExists) + return res.status(409).json({ success: false, message: 'User config already exists' }); + + log.debug('Running setup'); + + // Parse body + const body = req.body as UserConfiguration; + + // temp: print body for testing + log.debug('Uploads dir', body.uploadsDir); + log.debug('ID type', body.idType); + log.debug('ID size', body.idSize.toString()); + log.debug('Gfy size', body.gfySize.toString()); + log.debug('Max file size', body.maximumFileSize.toString()); + + return res.json({ success: true }); +}); + +export { router }; diff --git a/frontend/setup.mts b/frontend/setup.mts new file mode 100644 index 0000000..bb59fd7 --- /dev/null +++ b/frontend/setup.mts @@ -0,0 +1,43 @@ +import { SlInput, SlButton } from '@shoelace-style/shoelace'; +import { IdType, UserConfiguration } from 'ass'; + +const genericErrorAlert = () => alert('An error occured, please check the console for details'); +const errAlert = (logTitle: string, err: any, stream: 'error' | 'warn' = 'error') => (console[stream](logTitle, err), genericErrorAlert()); + +// * Wait for the document to be ready +document.addEventListener('DOMContentLoaded', () => { + + const dirInputElm = document.querySelector('#dir') as SlInput; + const idTypeInputElm = document.querySelector('#idtype') as SlInput; + const idSizeInputElm = document.querySelector('#idsize') as SlInput; + const gfySizeInputElm = document.querySelector('#gfysize') as SlInput; + const fileSizeInputElm = document.querySelector('#filesize') as SlInput; + const submitButtonElm = document.querySelector('#submit') as SlButton; + + // * Setup button click handler + submitButtonElm.addEventListener('click', async () => { + + const config: UserConfiguration = { + uploadsDir: dirInputElm.value, + idType: idTypeInputElm.value as IdType, + idSize: parseInt(idSizeInputElm.value), + gfySize: parseInt(gfySizeInputElm.value), + maximumFileSize: parseInt(fileSizeInputElm.value), + }; + + fetch('/setup', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(config) + }) + .then((res) => res.json()) + .then((data: { + success: boolean, + message: string + }) => { + if (!data.success) throw new Error(data.message); + else alert('good?'); + }) + .catch((err) => errAlert('POST to /setup failed!', err)); + }); +});