feat: settings v2

This commit is contained in:
Isaac
2023-03-10 23:47:43 +00:00
parent cd71843bb0
commit 96b5c92743
7 changed files with 106 additions and 505 deletions

View File

@@ -36,12 +36,6 @@ const env = {
OVERRIDE_ARCHIVE: () => true, // optional
PUBLIC_BOT: () => true, // optional
PUBLISH_COMMANDS: () => true, // optional
SETTINGS_HOST: v =>
(!!v && !v.startsWith('http')) ||
new Error('is required and must be an address, not a URL'),
SETTINGS_PORT: v =>
!!v ||
new Error('is required'),
SUPER: () => true, // optional
};

View File

@@ -6,16 +6,8 @@ const { join } = require('path');
const { files } = require('node-dir');
const { PermissionsBitField } = require('discord.js');
process.env.PUBLIC_HOST = process.env.HTTP_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST`
module.exports = async client => {
// cors plugin
fastify.register(require('@fastify/cors'), {
credentials: true,
methods: ['DELETE', 'GET', 'PATCH', 'PUT', 'POST'],
origin: true,
});
// oauth2 plugin
fastify.states = new Map();
fastify.register(oauth, {
@@ -56,20 +48,6 @@ module.exports = async client => {
secret: process.env.ENCRYPTION_KEY,
});
// proxy `/settings` to express
fastify.register(require('@fastify/http-proxy'), {
http2: false,
prefix: '/settings',
replyOptions: {
rewriteRequestHeaders: (req, headers) => ({
...headers,
'host': domain,
}),
},
rewritePrefix: '/settings',
upstream: `http://${process.env.SETTINGS_HOST}:${process.env.SETTINGS_PORT}`,
});
// auth
fastify.decorate('authenticate', async (req, res) => {
try {
@@ -176,24 +154,10 @@ module.exports = async client => {
})); // register route
});
// express server for settings
const express = require('express')();
const { handler } = await import('@discord-tickets/settings/build/handler.js');
process.on('sveltekit:error', ({
error,
errorId,
}) => {
client.log.error.http(`Express ${errorId} ${error}`);
});
express.set('trust proxy', true);
express.use((req, res, next) => {
next();
client.log.verbose.http(short(`Express ${req.ip} ${req.method} ${req.route?.path ?? req.path}`));
});
express.use(handler); // let SvelteKit handle everything
express.listen(process.env.SETTINGS_PORT, process.env.SETTINGS_HOST, () => { // start the express server
client.log.verbose.http(`Express listening on port ${process.env.SETTINGS_PORT}`);
});
// https://stackoverflow.com/questions/72317071/how-to-set-up-fastify-correctly-so-that-sveltekit-works-fine
fastify.all('/*', {}, (req, res) => handler(req.raw, res.raw, () => { }));
// start the fastify server
fastify.listen({
@@ -203,4 +167,11 @@ module.exports = async client => {
if (err) client.log.error.http(err);
else client.log.success.http(`Listening at ${addr}`);
});
process.on('sveltekit:error', ({
error,
errorId,
}) => {
client.log.error.http(`SvelteKit ${errorId} ${error}`);
});
};

View File

@@ -3,7 +3,7 @@ const { PermissionsBitField } = require('discord.js');
module.exports.get = fastify => ({
handler: async (req, res) => {
const { client } = res.context.config;
const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.payload.access_token}` } })).json();
const guilds = await (await fetch('https://discordapp.com/api/users/@me/guilds', { headers: { 'Authorization': `Bearer ${req.user.payload.accessToken}` } })).json();
res.send(
guilds
.filter(guild => guild.owner || new PermissionsBitField(guild.permissions.toString()).has(PermissionsBitField.Flags.ManageGuild))

View File

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