pull/243/head
Josh Moore 1 year ago
parent 83c89a9e25
commit 352cb2b636

@ -10,10 +10,10 @@ import { ServerConfiguration } from 'ass';
* Custom middleware to attach the ass object (and construct the `host` property) * Custom middleware to attach the ass object (and construct the `host` property)
*/ */
function assMetaMiddleware(port: number, proxied: boolean): RequestHandler { function assMetaMiddleware(port: number, proxied: boolean): RequestHandler {
return (req: Request, _res: Response, next: NextFunction) => { return (req: Request, _res: Response, next: NextFunction) => {
req.ass = { host: `${req.protocol}://${req.hostname}${proxied ? '' : `:${port}`}` }; req.ass = { host: `${req.protocol}://${req.hostname}${proxied ? '' : `:${port}`}` };
next(); next();
} }
} }
/** /**
@ -22,77 +22,77 @@ function assMetaMiddleware(port: number, proxied: boolean): RequestHandler {
*/ */
async function main() { async function main() {
// Set default server configuration // Set default server configuration
const serverConfig: ServerConfiguration = { const serverConfig: ServerConfiguration = {
host: '0.0.0.0', host: '0.0.0.0',
port: 40115, port: 40115,
proxied: isProd() proxied: isProd()
}; };
// Replace with user details, if necessary // Replace with user details, if necessary
try { try {
const exists = await fs.pathExists(path.join('server.json')); const exists = await fs.pathExists(path.join('server.json'));
if (exists) { if (exists) {
// Read file // Read file
const { host, port, proxied } = await fs.readJson(path.join('server.json')) as { host?: string, port?: number, proxied?: boolean }; const { host, port, proxied } = await fs.readJson(path.join('server.json')) as { host?: string, port?: number, proxied?: boolean };
// Set details, if available // Set details, if available
if (host) serverConfig.host = host; if (host) serverConfig.host = host;
if (port) serverConfig.port = port; if (port) serverConfig.port = port;
if (proxied != undefined) serverConfig.proxied = proxied; if (proxied != undefined) serverConfig.proxied = proxied;
log.debug('server.json', `${host ? `host=${host},` : ''}${port ? `port=${port},` : ''}${proxied != undefined ? `proxied=${proxied},` : ''}`); log.debug('server.json', `${host ? `host=${host},` : ''}${port ? `port=${port},` : ''}${proxied != undefined ? `proxied=${proxied},` : ''}`);
} }
} catch (err) { } catch (err) {
log.error('Failed to read server.json'); log.error('Failed to read server.json');
console.error(err); console.error(err);
throw err; throw err;
} }
// Set up Express // Set up Express
const app = express(); const app = express();
app.enable('case sensitive routing'); app.enable('case sensitive routing');
app.disable('x-powered-by'); app.disable('x-powered-by');
app.set('trust proxy', serverConfig.proxied); app.set('trust proxy', serverConfig.proxied);
app.set('view engine', 'pug'); app.set('view engine', 'pug');
app.set('views', 'views2/'); app.set('views', 'views2/');
// Middleware // Middleware
app.use(log.express()); app.use(log.express());
app.use(BodyParserJson()); app.use(BodyParserJson());
app.use(assMetaMiddleware(serverConfig.port, serverConfig.proxied)); app.use(assMetaMiddleware(serverConfig.port, serverConfig.proxied));
// CSS // CSS
app.use('/.css', epcss({ app.use('/.css', epcss({
cssPath: path.join('tailwind2.css'), cssPath: path.join('tailwind2.css'),
plugins: [ plugins: [
tailwindcss, tailwindcss,
(await import('autoprefixer')).default(), (await import('autoprefixer')).default(),
(await import('cssnano')).default(), (await import('cssnano')).default(),
(await import('@tinycreek/postcss-font-magician')).default(), (await import('@tinycreek/postcss-font-magician')).default(),
], ],
warn: (warning: Error) => log.warn('PostCSS', warning.toString()) warn: (warning: Error) => log.warn('PostCSS', warning.toString())
})); }));
app.get('/.ass.host', (req, res) => res.send(req.ass.host)); app.get('/.ass.host', (req, res) => res.send(req.ass.host));
// Routing // Routing
app.use('/setup', (await import('./routers/setup')).router); app.use('/setup', (await import('./routers/setup')).router);
app.use('/', (await import('./routers/index')).router); app.use('/', (await import('./routers/index')).router);
// Host app // Host app
const userConfigExists = await fs.pathExists(path.join('userconfig.json')); 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[userConfigExists ? 'success' : 'warn']('Server listening', userConfigExists ? 'Ready for uploads' : 'Setup required', `click http://127.0.0.1:${serverConfig.port}`));
} }
// Launch log // Launch log
const pkg = fs.readJsonSync(path.join('package.json')) as { name: string, version: string }; const pkg = fs.readJsonSync(path.join('package.json')) as { name: string, version: string };
log.blank() log.blank()
.info(pkg.name, pkg.version) .info(pkg.name, pkg.version)
.blank(); .blank();
// Start program // Start program
main().catch(() => process.exit(1)); main().catch(() => process.exit(1));
@ -100,10 +100,10 @@ main().catch(() => process.exit(1));
// Exit tasks // Exit tasks
['SIGINT', 'SIGTERM'].forEach((signal) => process.addListener(signal as any, () => { ['SIGINT', 'SIGTERM'].forEach((signal) => process.addListener(signal as any, () => {
// Hide ^C in console output // Hide ^C in console output
process.stdout.write('\r'); process.stdout.write('\r');
// Log then exit // Log then exit
log.info('Exiting', `received ${signal}`); log.info('Exiting', `received ${signal}`);
process.exit(); process.exit();
})); }));

@ -13,22 +13,22 @@ router.get('/ui.js', (req, res) => userConfigExists() ? res.send('') : res.type(
// Setup route // Setup route
router.post('/', BodyParserJson(), (req, res) => { router.post('/', BodyParserJson(), (req, res) => {
if (userConfigExists()) if (userConfigExists())
return res.status(409).json({ success: false, message: 'User config already exists' }); return res.status(409).json({ success: false, message: 'User config already exists' });
log.debug('Running setup'); log.debug('Running setup');
// Parse body // Parse body
const body = req.body as UserConfiguration; const body = req.body as UserConfiguration;
// temp: print body for testing // temp: print body for testing
log.debug('Uploads dir', body.uploadsDir); log.debug('Uploads dir', body.uploadsDir);
log.debug('ID type', body.idType); log.debug('ID type', body.idType);
log.debug('ID size', body.idSize.toString()); log.debug('ID size', body.idSize.toString());
log.debug('Gfy size', body.gfySize.toString()); log.debug('Gfy size', body.gfySize.toString());
log.debug('Max file size', body.maximumFileSize.toString()); log.debug('Max file size', body.maximumFileSize.toString());
return res.json({ success: true }); return res.json({ success: true });
}); });
export { router }; export { router };

26
common/global.d.ts vendored

@ -1,19 +1,19 @@
import { Request, Response } from 'express'; import { Request, Response } from 'express';
declare global { declare global {
namespace Express { namespace Express {
interface Request { interface Request {
/** /**
* ass-specific request items * ass-specific request items
*/ */
ass: { ass: {
/** /**
* Combination of {protocol}://{hostname} * Combination of {protocol}://{hostname}
*/ */
host: string host: string
} }
} }
} }
} }

44
common/types.d.ts vendored

@ -1,24 +1,24 @@
declare module 'ass' { declare module 'ass' {
type IdType = 'random' | 'original' | 'gfycat' | 'timestamp' | 'zws' type IdType = 'random' | 'original' | 'gfycat' | 'timestamp' | 'zws'
/** /**
* Core Express server config. * Core Express server config.
* This is separate from the user configuration starting in 0.15.0 * This is separate from the user configuration starting in 0.15.0
*/ */
interface ServerConfiguration { interface ServerConfiguration {
host: string, host: string,
port: number, port: number,
proxied: boolean proxied: boolean
} }
interface UserConfiguration { interface UserConfiguration {
uploadsDir: string; uploadsDir: string;
idType: IdType; idType: IdType;
idSize: number; idSize: number;
gfySize: number; gfySize: number;
maximumFileSize: number; maximumFileSize: number;
} }
} }
//#region Dummy modules //#region Dummy modules
@ -27,9 +27,9 @@ declare module '@tinycreek/postcss-font-magician';
// don't commit // don't commit
/* future UserConfig options: /* future UserConfig options:
mediaStrict: boolean; mediaStrict: boolean;
viewDirect: boolean; viewDirect: boolean;
viewDirectDiscord: boolean; viewDirectDiscord: boolean;
adminWebhook: {} adminWebhook: {}
s3: {} s3: {}
*/ */

@ -7,37 +7,37 @@ const errAlert = (logTitle: string, err: any, stream: 'error' | 'warn' = 'error'
// * Wait for the document to be ready // * Wait for the document to be ready
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const dirInputElm = document.querySelector('#dir') as SlInput; const dirInputElm = document.querySelector('#dir') as SlInput;
const idTypeInputElm = document.querySelector('#idtype') as SlInput; const idTypeInputElm = document.querySelector('#idtype') as SlInput;
const idSizeInputElm = document.querySelector('#idsize') as SlInput; const idSizeInputElm = document.querySelector('#idsize') as SlInput;
const gfySizeInputElm = document.querySelector('#gfysize') as SlInput; const gfySizeInputElm = document.querySelector('#gfysize') as SlInput;
const fileSizeInputElm = document.querySelector('#filesize') as SlInput; const fileSizeInputElm = document.querySelector('#filesize') as SlInput;
const submitButtonElm = document.querySelector('#submit') as SlButton; const submitButtonElm = document.querySelector('#submit') as SlButton;
// * Setup button click handler // * Setup button click handler
submitButtonElm.addEventListener('click', async () => { submitButtonElm.addEventListener('click', async () => {
const config: UserConfiguration = { const config: UserConfiguration = {
uploadsDir: dirInputElm.value, uploadsDir: dirInputElm.value,
idType: idTypeInputElm.value as IdType, idType: idTypeInputElm.value as IdType,
idSize: parseInt(idSizeInputElm.value), idSize: parseInt(idSizeInputElm.value),
gfySize: parseInt(gfySizeInputElm.value), gfySize: parseInt(gfySizeInputElm.value),
maximumFileSize: parseInt(fileSizeInputElm.value), maximumFileSize: parseInt(fileSizeInputElm.value),
}; };
fetch('/setup', { fetch('/setup', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(config) body: JSON.stringify(config)
}) })
.then((res) => res.json()) .then((res) => res.json())
.then((data: { .then((data: {
success: boolean, success: boolean,
message: string message: string
}) => { }) => {
if (!data.success) throw new Error(data.message); if (!data.success) throw new Error(data.message);
else alert('good?'); else alert('good?');
}) })
.catch((err) => errAlert('POST to /setup failed!', err)); .catch((err) => errAlert('POST to /setup failed!', err));
}); });
}); });

Loading…
Cancel
Save