feat: improved (?) setup flow

pull/243/head
Josh Moore 11 months ago
parent b9dff5865c
commit b37436c4f2

@ -29,25 +29,26 @@ const Checkers: UserConfigTypeChecker = {
}
export class UserConfig {
private static config: UserConfiguration;
private ready = false;
private static _config: UserConfiguration;
private static _ready = false;
public getConfig = () => UserConfig.config;
public getReady = () => this.ready;
constructor(config?: UserConfiguration) {
public static get config() { return UserConfig._config; }
public static get ready() { return UserConfig._ready; }
constructor(config?: any) {
// Typically this would only happen during first-time setup (for now)
if (config != null) {
UserConfig.config = this.parseConfig(config);
this.ready = true;
UserConfig._config = UserConfig.parseConfig(config);
UserConfig._ready = true;
}
}
/**
* Ensures that all config options are valid
*/
private parseConfig(config: UserConfiguration) {
private static parseConfig(c: any) {
const config = (typeof c === 'string' ? JSON.parse(c) : c) as UserConfiguration;
if (!Checkers.uploadsDir(config.uploadsDir)) throw new Error(`Unable to access uploads directory: ${config.uploadsDir}`);
if (!Checkers.idType(config.idType)) throw new Error('Invalid ID type');
if (!Checkers.idSize(config.idSize)) throw new Error('Invalid ID size');
@ -61,15 +62,15 @@ export class UserConfig {
/**
* Save the config file to disk
*/
public saveConfigFile(): Promise<void> {
public static saveConfigFile(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
// Only save is the config has been parsed
if (!this.ready) throw new Error('Config not ready to be saved!');
if (!UserConfig._ready) throw new Error('Config not ready to be saved!');
// Write to file
await fs.writeFile(path.join('userconfig.json'), JSON.stringify(UserConfig.config, null, '\t'));
await fs.writeFile(path.join('userconfig.json'), JSON.stringify(UserConfig._config, null, '\t'));
resolve(void 0);
} catch (err) {
@ -82,7 +83,7 @@ export class UserConfig {
/**
* Reads the config file from disk
*/
public readConfigFile(): Promise<void> {
public static readConfigFile(): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
@ -90,8 +91,8 @@ export class UserConfig {
const data = (await fs.readFile(path.join('userconfig.json'))).toString();
// Ensure the config is valid
UserConfig.config = this.parseConfig(data as unknown as UserConfiguration);
this.ready = true;
UserConfig._config = UserConfig.parseConfig(data);
UserConfig._ready = true;
resolve(void 0);
} catch (err) {

@ -4,6 +4,7 @@ import { path, isProd } from '@tycrek/joint';
import { epcss } from '@tycrek/express-postcss';
import tailwindcss from 'tailwindcss';
import { log } from './log';
import { UserConfig } from './UserConfig';
import { ServerConfiguration } from 'ass';
/**
@ -50,6 +51,11 @@ async function main() {
throw err;
}
// Attempt to load user configuration
const readUserConfig = (): Promise<void> => new Promise((resolve) =>
UserConfig.readConfigFile().then(() => resolve(void 0)).catch((err) => (console.error(err), resolve(void 0))));
await readUserConfig();
// Set up Express
const app = express();
@ -84,8 +90,7 @@ async function main() {
app.use('/', (await import('./routers/index')).router);
// Host app
const userConfigExists = await fs.pathExists(path.join('userconfig.json'));
app.listen(serverConfig.port, serverConfig.host, () => log[userConfigExists ? 'success' : 'warn']('Server listening', userConfigExists ? 'Ready for uploads' : 'Setup required', `click http://127.0.0.1:${serverConfig.port}`));
app.listen(serverConfig.port, serverConfig.host, () => log[UserConfig.ready ? 'success' : 'warn']('Server listening', UserConfig.ready ? 'Ready for uploads' : 'Setup required', `click http://127.0.0.1:${serverConfig.port}`));
}
// Launch log

@ -1,11 +1,9 @@
import fs from 'fs-extra';
import { path } from '@tycrek/joint';
import { Router, json as BodyParserJson } from 'express';
import { Router } from 'express';
import { log } from '../log';
import { UserConfig } from '../UserConfig';
const router = Router({ caseSensitive: true });
const userConfigExists = () => fs.pathExistsSync(path.join('userconfig.json'));
router.get('/', (req, res) => userConfigExists() ? res.render('index') : res.redirect('/setup'));
router.get('/', (req, res) => UserConfig.ready ? res.render('index') : res.redirect('/setup'));
export { router };

@ -1,30 +1,27 @@
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';
import { UserConfig } from '../UserConfig';
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')));
router.get('/', (req, res) => UserConfig.ready ? res.redirect('/') : res.render('setup'));
router.get('/ui.js', (req, res) => UserConfig.ready ? res.send('') : res.type('text').sendFile(path.join('dist-frontend/setup.mjs')));
// Setup route
router.post('/', BodyParserJson(), async (req, res) => {
if (userConfigExists())
if (UserConfig.ready)
return res.status(409).json({ success: false, message: 'User config already exists' });
log.debug('Setup initiated');
try {
// Parse body
const userConfig = new UserConfig(req.body as UserConfiguration);
new UserConfig(req.body);
// Save config
await userConfig.saveConfigFile();
await UserConfig.saveConfigFile();
return res.json({ success: true });
} catch (err: any) {

Loading…
Cancel
Save