Settings reverse proxy (doesn't work)

This commit is contained in:
Isaac 2022-09-07 21:24:16 +01:00
parent 742dedd635
commit be0795cde0
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
9 changed files with 48 additions and 26 deletions

View File

@ -34,12 +34,12 @@
"node": ">=18.0"
},
"dependencies": {
"@discord-tickets/settings": "^1.0.0",
"@eartharoid/dbf": "^0.3.3",
"@eartharoid/dtf": "^2.0.1",
"@eartharoid/i18n": "^1.0.4",
"@fastify/cookie": "^6.0.0",
"@fastify/cors": "^8.1.0",
"@fastify/http-proxy": "^8.2.2",
"@fastify/jwt": "^5.0.1",
"@fastify/oauth2": "^5.1.0",
"@prisma/client": "^4.3.1",

View File

@ -2,7 +2,6 @@ require('dotenv').config();
const fs = require('fs-extra');
const { spawnSync } = require('child_process');
const providers = ['mysql', 'postgresql', 'sqlite'];
const provider = process.env.DB_PROVIDER;

View File

@ -2,16 +2,16 @@ const { randomBytes } = require('crypto');
const fs = require('fs');
const env = {
API_BIND: 8080,
API_EXTERNAL: 'http://localhost:8080',
DB_CONNECTION_URL: '',
DB_PROVIDER: '', // don't default to sqlite, postinstall checks if empty
DISCORD_SECRET: '',
DISCORD_TOKEN: '',
ENCRYPTION_KEY: randomBytes(24).toString('hex'),
HTTP_BIND: 8080,
HTTP_EXTERNAL: 'http://localhost',
PORTAL: '',
PUBLIC_BOT: false,
SETTINGS_BIND: 80,
SETTINGS_BIND: 8888,
SUPER: '319467558166069248',
};

View File

@ -4,7 +4,7 @@ const { short } = require('leeks.js');
const { join } = require('path');
const { files } = require('node-dir');
process.env.PUBLIC_HOST = process.env.API_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST`
process.env.PUBLIC_HOST = process.env.HTTP_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST`
module.exports = async client => {
// cors plugin
@ -16,7 +16,7 @@ module.exports = async client => {
// oauth2 plugin
fastify.register(oauth, {
callbackUri: `${process.env.API_EXTERNAL}/auth/callback`,
callbackUri: `${process.env.HTTP_EXTERNAL}/auth/callback`,
credentials: {
auth: oauth.DISCORD_CONFIGURATION,
client: {
@ -41,6 +41,14 @@ module.exports = async client => {
secret: process.env.ENCRYPTION_KEY,
});
// proxy /settings to express
fastify.register(require('@fastify/http-proxy'), {
http2: false,
prefix: '/settings',
rewritePrefix: '/settings',
upstream: `http://127.0.0.1:${process.env.SETTINGS_BIND}`,
});
// auth
fastify.decorate('authenticate', async (req, res) => {
try {
@ -54,6 +62,8 @@ module.exports = async client => {
});
}
} catch (err) {
console.log(req);
console.log(req.cookies);
res.send(err);
}
});
@ -116,15 +126,15 @@ module.exports = async client => {
: responseTime >= 5
? '&e'
: '&a') + responseTime + 'ms';
client.log.info.http(short(`API ${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`));
client.log.info.http(short(`${req.id} ${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`));
if (!req.routerPath) client.log.verbose.http(`${req.id} ${req.method} ${req.url}`);
done();
});
fastify.addHook('onError', async (req, res, err) => client.log.error.http(err));
fastify.addHook('onError', async (req, res, err) => client.log.error.http(req.id, err));
// route loading
const dir = join(__dirname, '/routes');
files(dir, {
exclude: /^\./,
match: /.js$/,
@ -146,24 +156,21 @@ module.exports = async client => {
})); // register route
});
// start server
fastify.listen({ port: process.env.API_BIND }, (err, addr) => {
if (err) client.log.error.http(err);
else client.log.success.http(`API Listening at ${addr}`);
});
// express server for settings
const express = require('express')();
const { handler } = await import('@discord-tickets/settings/build/handler.js');
express.get('/api/client', (req, res) => {
res.end('ok');
});
express.use((req, res, next) => {
next();
// req.route?.path ?? '*'
client.log.verbose.http(short(`SETTINGS ${req.ip} ${req.method} ${req.path}`)); // verbose because little information & SvelteKit is very spammy (lots of routes)
client.log.verbose.http(short(`Express ${req.ip} ${req.method} ${req.route?.path ?? req.path}`));
});
express.use(handler);
express.listen(process.env.SETTINGS_BIND, () => {
client.log.success.http(`SETTINGS Listening at ${process.env.SETTINGS_BIND}`);
express.use(handler); // let SvelteKit handle everything
express.listen(process.env.SETTINGS_BIND, () => { // start the express server
client.log.verbose.http(`Express listening on port ${process.env.SETTINGS_BIND}`);
});
// start the fastify server
fastify.listen({ port: process.env.HTTP_BIND }, (err, addr) => {
if (err) client.log.error.http(err);
else client.log.success.http(`Listening at ${addr}`);
});
};

View File

@ -1 +1 @@
module.exports.domain = process.env.API_EXTERNAL.match(/http(s?):\/\/(?<domain>[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain;
module.exports.domain = process.env.HTTP_EXTERNAL.match(/http(s?):\/\/(?<domain>[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain;

View File

@ -10,6 +10,7 @@ const colours = {
info: ['&3', '&b'],
notice: ['&!6&0', '&!6&0'],
success: ['&2', '&a'],
verbose: ['&7', '&f'],
warn: ['&6', '&e'],
};

View File

@ -23,6 +23,6 @@ module.exports.get = () => ({
sameSite: true,
secure: false,
})
.redirect('/');
.redirect('/settings');
},
});

View File

@ -2,5 +2,6 @@ module.exports.get = () => ({
handler: (req, res) => {
const { client } = res.context.config;
return `Hello, I am ${client.user.username}!`;
// res.redirect(process.env.SETTINGS_EXTERNAL);
},
});

14
src/stdin/settings.js Normal file
View File

@ -0,0 +1,14 @@
const { StdinCommand } = require('@eartharoid/dbf');
module.exports = class extends StdinCommand {
constructor(client, options) {
super(client, {
...options,
id: 'settings',
});
}
async run() {
this.client.log.info.settings(process.env.HTTP_EXTERNAL + '/settings');
}
};